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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/mman.h>
#include <linux/mman.h>
#include "triton_p.h"
#include "memdebug.h"
#ifdef VALGRIND
#include <valgrind/memcheck.h>
#define DELAY 5
#endif
//#define MEMPOOL_DISABLE
#define MMAP_PAGE_SIZE 16
#define MAGIC1 0x2233445566778899llu
struct _mempool_t
{
struct list_head entry;
int size;
struct list_head items;
#ifdef MEMDEBUG
struct list_head ditems;
#endif
spinlock_t lock;
uint64_t magic;
int mmap:1;
};
struct _item_t
{
struct list_head entry;
#ifdef VALGRIND
time_t timestamp;
#endif
struct _mempool_t *owner;
#ifdef MEMDEBUG
const char *fname;
int line;
#endif
uint64_t magic2;
uint64_t magic1;
char ptr[0];
};
static LIST_HEAD(pools);
static spinlock_t pools_lock = SPINLOCK_INITIALIZER;
mempool_t __export *mempool_create(int size)
{
struct _mempool_t *p = _malloc(sizeof(*p));
memset(p, 0, sizeof(*p));
INIT_LIST_HEAD(&p->items);
#ifdef MEMDEBUG
INIT_LIST_HEAD(&p->ditems);
#endif
spinlock_init(&p->lock);
p->size = size;
p->magic = (uint64_t)random() * (uint64_t)random();
spin_lock(&pools_lock);
list_add_tail(&p->entry, &pools);
spin_unlock(&pools_lock);
return (mempool_t *)p;
}
mempool_t __export *mempool_create2(int size)
{
struct _mempool_t *p = (struct _mempool_t *)mempool_create(size);
p->mmap = 1;
return (mempool_t *)p;
}
#ifndef MEMDEBUG
void __export *mempool_alloc(mempool_t *pool)
{
struct _mempool_t *p = (struct _mempool_t *)pool;
struct _item_t *it;
uint32_t size = sizeof(*it) + p->size + 8;
spin_lock(&p->lock);
if (!list_empty(&p->items)) {
it = list_entry(p->items.next, typeof(*it), entry);
list_del(&it->entry);
spin_unlock(&p->lock);
__sync_sub_and_fetch(&triton_stat.mempool_available, size);
it->magic1 = MAGIC1;
return it->ptr;
}
spin_unlock(&p->lock);
if (p->mmap)
it = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_32BIT, -1, 0);
else
it = _malloc(size);
if (!it) {
triton_log_error("mempool: out of memory\n");
return NULL;
}
it->owner = p;
it->magic1 = MAGIC1;
it->magic2 = p->magic;
*(uint64_t*)(it->data + p->size) = it->magic2;
__sync_add_and_fetch(&triton_stat.mempool_allocated, size);
return it->ptr;
}
#endif
void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line)
{
struct _mempool_t *p = (struct _mempool_t *)pool;
struct _item_t *it;
uint32_t size = sizeof(*it) + p->size + 8;
int i;
spin_lock(&p->lock);
if (!list_empty(&p->items)) {
it = list_entry(p->items.next, typeof(*it), entry);
#ifdef VALGRIND
if (it->timestamp + DELAY < time(NULL)) {
VALGRIND_MAKE_MEM_DEFINED(&it->owner, size - sizeof(it->entry) - sizeof(it->timestamp));
VALGRIND_MAKE_MEM_UNDEFINED(it->ptr, p->size);
#endif
list_del(&it->entry);
list_add(&it->entry, &p->ditems);
spin_unlock(&p->lock);
it->fname = fname;
it->line = line;
__sync_sub_and_fetch(&triton_stat.mempool_available, size);
it->magic1 = MAGIC1;
return it->ptr;
#ifdef VALGRIND
}
#endif
}
spin_unlock(&p->lock);
if (p->mmap) {
it = mmap(NULL, size * MMAP_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_32BIT, -1, 0);
__sync_add_and_fetch(&triton_stat.mempool_allocated, size * (MMAP_PAGE_SIZE - 1));
__sync_add_and_fetch(&triton_stat.mempool_available, size * (MMAP_PAGE_SIZE - 1));
spin_lock(&p->lock);
for (i = 0; i < MMAP_PAGE_SIZE - 1; i++, it) {
it->owner = p;
it->magic2 = p->magic;
it->magic1 = MAGIC1;
*(uint64_t*)(it->ptr + p->size) = it->magic2;
list_add_tail(&it->entry,&p->items);
#ifdef VALGRIND
it->timestamp = 0;
VALGRIND_MAKE_MEM_NOACCESS(&it->owner, size - sizeof(it->entry) - sizeof(it->timestamp));
#endif
it = (struct _item_t *)((char *)it + size);
}
spin_unlock(&p->lock);
#ifdef VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED(it, size);
#endif
} else
it = md_malloc(size, fname, line);
if (!it) {
triton_log_error("mempool: out of memory\n");
return NULL;
}
it->owner = p;
it->magic2 = p->magic;
it->magic1 = MAGIC1;
it->fname = fname;
it->line = line;
*(uint64_t*)(it->ptr + p->size) = it->magic2;
spin_lock(&p->lock);
list_add(&it->entry, &p->ditems);
spin_unlock(&p->lock);
__sync_add_and_fetch(&triton_stat.mempool_allocated, size);
return it->ptr;
}
void __export mempool_free(void *ptr)
{
struct _item_t *it = container_of(ptr, typeof(*it), ptr);
struct _mempool_t *p = it->owner;
uint32_t size = sizeof(*it) + it->owner->size + 8;
#ifdef MEMDEBUG
if (it->magic1 != MAGIC1) {
triton_log_error("mempool: memory corruption detected");
abort();
}
if (it->magic2 != it->owner->magic) {
triton_log_error("mempool: memory corruption detected");
abort();
}
if (it->magic2 != *(uint64_t*)(it->ptr + it->owner->size)) {
triton_log_error("mempool: memory corruption detected");
abort();
}
it->magic1 = 0;
#endif
spin_lock(&p->lock);
#ifdef MEMDEBUG
list_del(&it->entry);
#endif
#ifndef MEMPOOL_DISABLE
list_add_tail(&it->entry,&it->owner->items);
#endif
#ifdef VALGRIND
time(&it->timestamp);
VALGRIND_MAKE_MEM_NOACCESS(&it->owner, size - sizeof(it->entry) - sizeof(it->timestamp));
#endif
spin_unlock(&p->lock);
#ifdef MEMPOOL_DISABLE
if (it->owner->mmap)
munmap(it, size);
else
_free(it);
#endif
__sync_add_and_fetch(&triton_stat.mempool_available, size);
}
void __export mempool_clean(mempool_t *pool)
{
struct _mempool_t *p = (struct _mempool_t *)pool;
struct _item_t *it;
uint32_t size = sizeof(*it) + p->size + 8;
spin_lock(&p->lock);
while (!list_empty(&p->items)) {
it = list_entry(p->items.next, typeof(*it), entry);
#ifdef VALGRIND
if (it->timestamp + DELAY < time(NULL)) {
VALGRIND_MAKE_MEM_DEFINED(&it->owner, size - sizeof(it->entry) - sizeof(it->timestamp));
#endif
list_del(&it->entry);
if (p->mmap)
munmap(it, size);
else
_free(it);
__sync_sub_and_fetch(&triton_stat.mempool_allocated, size);
__sync_sub_and_fetch(&triton_stat.mempool_available, size);
#ifdef VALGRIND
} else
break;
#endif
}
spin_unlock(&p->lock);
}
#ifdef MEMDEBUG
void __export mempool_show(mempool_t *pool)
{
struct _mempool_t *p = (struct _mempool_t *)pool;
struct _item_t *it;
spin_lock(&p->lock);
list_for_each_entry(it, &p->ditems, entry)
triton_log_error("%s:%i %p\n", it->fname, it->line, it->ptr);
spin_unlock(&p->lock);
}
#endif
void sigclean(int num)
{
struct _mempool_t *p;
struct _item_t *it;
uint32_t size;
triton_log_error("mempool: clean\n");
spin_lock(&pools_lock);
list_for_each_entry(p, &pools, entry) {
size = sizeof(*it) + p->size + 8;
spin_lock(&p->lock);
while (!list_empty(&p->items)) {
it = list_entry(p->items.next, typeof(*it), entry);
#ifdef VALGRIND
if (it->timestamp + DELAY < time(NULL)) {
VALGRIND_MAKE_MEM_DEFINED(&it->owner, size - sizeof(it->entry) - sizeof(it->timestamp));
#endif
list_del(&it->entry);
if (p->mmap)
munmap(it, size);
else
_free(it);
__sync_sub_and_fetch(&triton_stat.mempool_allocated, size);
__sync_sub_and_fetch(&triton_stat.mempool_available, size);
#ifdef VALGRIND
} else
break;
#endif
}
spin_unlock(&p->lock);
}
spin_unlock(&pools_lock);
}
static void __init init(void)
{
sigset_t set;
sigfillset(&set);
struct sigaction sa = {
.sa_handler = sigclean,
.sa_mask = set,
};
sigaction(35, &sa, NULL);
}
|