forked from dbmail/dbmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_mempool.c
131 lines (111 loc) · 3.13 KB
/
dm_mempool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "dbmail.h"
#include "dm_mempool.h"
#include "mpool/mpool.h"
#define THIS_MODULE "mempool"
#define M Mempool_T
#define fail_unless(a) assert(a)
struct M {
pthread_mutex_t lock;
mpool_t *pool;
};
M mempool_open(void)
{
M MP;
mpool_t *pool = NULL;
static gboolean env_mpool = FALSE;
static gboolean use_mpool = FALSE;
if (! env_mpool) {
char *dm_pool = getenv("DM_POOL");
if (MATCH(dm_pool, "yes"))
use_mpool = TRUE;
env_mpool = TRUE;
}
if (use_mpool)
pool = mpool_open(0,0,0,NULL);
else
pool = NULL;
MP = mpool_alloc(pool, sizeof(*MP), NULL);
if (pthread_mutex_init(&MP->lock, NULL)) {
perror("pthread_mutex_init failed");
if (pool)
mpool_close(pool);
return NULL;
}
MP->pool = pool;
return MP;
}
void * mempool_pop(M MP, size_t blocksize)
{
int error;
PLOCK(MP->lock);
void *block = mpool_calloc(MP->pool, 1, blocksize, &error);
PUNLOCK(MP->lock);
if (error != MPOOL_ERROR_NONE)
TRACE(TRACE_ERR, "%s", mpool_strerror(error));
return block;
}
void * mempool_resize(M MP, void *block, size_t oldsize, size_t newsize)
{
int error;
PLOCK(MP->lock);
void *newblock = mpool_resize(MP->pool, block, oldsize, newsize, &error);
PUNLOCK(MP->lock);
if (error != MPOOL_ERROR_NONE)
TRACE(TRACE_ERR, "%s", mpool_strerror(error));
assert (error == MPOOL_ERROR_NONE);
return newblock;
}
void mempool_push(M MP, void *block, size_t blocksize)
{
int error;
PLOCK(MP->lock);
if ((error = mpool_free(MP->pool, block, blocksize)) != MPOOL_ERROR_NONE)
TRACE(TRACE_ERR, "%s", mpool_strerror(error));
fail_unless(error == MPOOL_ERROR_NONE);
PUNLOCK(MP->lock);
}
void mempool_stats(M MP)
{
unsigned int page_size;
unsigned long num_alloced, user_alloced, max_alloced, tot_alloced;
mpool_stats(MP->pool, &page_size, &num_alloced, &user_alloced,
&max_alloced, &tot_alloced);
TRACE(TRACE_DEBUG, "[%p] page_size: %u num: %" PRIu64 " user: %" PRIu64 " "
"max: %" PRIu64 " tot: %" PRIu64 "", MP->pool,
page_size, (uint64_t)num_alloced, (uint64_t)user_alloced,
(uint64_t)max_alloced, (uint64_t)tot_alloced);
}
void mempool_close(M *MP)
{
int error;
M mp = *MP;
pthread_mutex_t lock = mp->lock;
PLOCK(lock);
mpool_t *pool = mp->pool;
if (pool) {
mempool_stats(mp);
if ((error = mpool_close(pool)) != MPOOL_ERROR_NONE)
TRACE(TRACE_ERR, "%s", mpool_strerror(error));
} else {
free(mp);
}
PUNLOCK(lock);
pthread_mutex_destroy(&lock);
mp = NULL;
}
#undef M