git-svn: trunk@4899
aCaB authored on 2009/03/06 00:39:37... | ... |
@@ -41,14 +41,19 @@ |
41 | 41 |
#include "others.h" |
42 | 42 |
#include "str.h" |
43 | 43 |
#include "readdb.h" |
44 |
-#include <assert.h> |
|
45 | 44 |
|
45 |
+#ifdef CL_DEBUG |
|
46 |
+#include <assert.h> |
|
46 | 47 |
#define MPOOLMAGIC 0x5adeada5 |
48 |
+#define ALLOCPOISON 0x5a |
|
49 |
+#define FREEPOISON 0xde |
|
50 |
+#endif |
|
51 |
+ |
|
47 | 52 |
/* #define DEBUGMPOOL /\* DO NOT define *\/ */ |
48 | 53 |
#ifdef DEBUGMPOOL |
49 | 54 |
#define spam(...) cli_warnmsg( __VA_ARGS__) |
50 | 55 |
#else |
51 |
-#define spam |
|
56 |
+static inline void spam(const char *fmt, ...) { fmt = fmt; } /* gcc STFU */ |
|
52 | 57 |
#endif |
53 | 58 |
|
54 | 59 |
#include "mpool.h" |
... | ... |
@@ -363,6 +368,9 @@ struct MP *mpool_create() { |
363 | 363 |
mp.mpm.size = sz - align_to_voidptr(sizeof(mp)); |
364 | 364 |
if ((mpool_p = (struct MP *)mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE|ANONYMOUS_MAP, -1, 0)) == MAP_FAILED) |
365 | 365 |
return NULL; |
366 |
+#ifdef CL_DEBUG |
|
367 |
+ memset(mpool_p, ALLOCPOISON, sz); |
|
368 |
+#endif |
|
366 | 369 |
memcpy(mpool_p, &mp, sizeof(mp)); |
367 | 370 |
spam("Map created @ %p->%p - size %u out of %u\n", mpool_p, (char *)mpool_p + mp.mpm.size, mp.mpm.usize, mp.mpm.size); |
368 | 371 |
return mpool_p; |
... | ... |
@@ -370,27 +378,44 @@ struct MP *mpool_create() { |
370 | 370 |
|
371 | 371 |
void mpool_destroy(struct MP *mp) { |
372 | 372 |
struct MPMAP *mpm_next = mp->mpm.next, *mpm; |
373 |
+ unsigned int mpmsize; |
|
374 |
+ |
|
373 | 375 |
while((mpm = mpm_next)) { |
376 |
+ mpmsize = mpm->size; |
|
374 | 377 |
mpm_next = mpm->next; |
375 |
- munmap((void *)mpm, mpm->size); |
|
378 |
+#ifdef CL_DEBUG |
|
379 |
+ memset(mpm, FREEPOISON, mpmsize); |
|
380 |
+#endif |
|
381 |
+ munmap((void *)mpm, mpmsize); |
|
376 | 382 |
} |
377 |
- munmap((void *)mp, mp->mpm.size + align_to_voidptr(sizeof(*mp))); |
|
383 |
+ mpmsize = mp->mpm.size; |
|
384 |
+#ifdef CL_DEBUG |
|
385 |
+ memset(mp, FREEPOISON, mpmsize + align_to_voidptr(sizeof(*mp))); |
|
386 |
+#endif |
|
387 |
+ munmap((void *)mp, mpmsize + align_to_voidptr(sizeof(*mp))); |
|
378 | 388 |
spam("Map destroyed @ %p\n", mp); |
379 | 389 |
} |
380 | 390 |
|
381 | 391 |
void mpool_flush(struct MP *mp) { |
382 | 392 |
size_t used = 0, mused; |
383 | 393 |
struct MPMAP *mpm_next = mp->mpm.next, *mpm; |
394 |
+ |
|
384 | 395 |
while((mpm = mpm_next)) { |
385 | 396 |
mpm_next = mpm->next; |
397 |
+#ifdef CL_DEBUG |
|
398 |
+ memset((char *)mpm + align_to_pagesize(mp, mpm->usize), FREEPOISON, mpm->size - align_to_pagesize(mp, mpm->usize)); |
|
399 |
+#endif |
|
386 | 400 |
munmap((char *)mpm + align_to_pagesize(mp, mpm->usize), mpm->size - align_to_pagesize(mp, mpm->usize)); |
387 | 401 |
mpm->size = align_to_pagesize(mp, mpm->usize); |
388 | 402 |
used += mpm->size; |
389 | 403 |
} |
390 | 404 |
mused = align_to_pagesize(mp, mp->mpm.usize + align_to_voidptr(sizeof(*mp))); |
391 | 405 |
if (mused < mp->mpm.size) { |
392 |
- munmap(&mp->mpm + mused, mp->mpm.size - mused); |
|
393 |
- mp->mpm.size = mused; |
|
406 |
+#ifdef CL_DEBUG |
|
407 |
+ memset((char *)&mp->mpm + mused, FREEPOISON, mp->mpm.size - mused); |
|
408 |
+#endif |
|
409 |
+ munmap((char *)&mp->mpm + mused, mp->mpm.size - mused); |
|
410 |
+ mp->mpm.size = mused; |
|
394 | 411 |
} |
395 | 412 |
used += mp->mpm.size; |
396 | 413 |
spam("Map flushed @ %p, in use: %lu\n", mp, used); |
... | ... |
@@ -398,26 +423,27 @@ void mpool_flush(struct MP *mp) { |
398 | 398 |
|
399 | 399 |
int mpool_getstats(const struct cl_engine *eng, size_t *used, size_t *total) |
400 | 400 |
{ |
401 |
- size_t sum_used = 0, sum_total = 0; |
|
402 |
- const struct MPMAP *mpm; |
|
403 |
- const mpool_t *mp; |
|
404 |
- /* checking refcount is not necessary, but safer */ |
|
405 |
- if (!eng || !eng->refcount) |
|
406 |
- return -1; |
|
407 |
- mp = eng->mempool; |
|
408 |
- if (!mp) |
|
409 |
- return -1; |
|
410 |
- for(mpm = &mp->mpm; mpm; mpm = mpm->next) { |
|
411 |
- sum_used += mpm->usize; |
|
412 |
- sum_total += mpm->size; |
|
413 |
- } |
|
414 |
- *used = sum_used; |
|
415 |
- *total = sum_total; |
|
416 |
- return 0; |
|
401 |
+ size_t sum_used = 0, sum_total = 0; |
|
402 |
+ const struct MPMAP *mpm; |
|
403 |
+ const mpool_t *mp; |
|
404 |
+ |
|
405 |
+ /* checking refcount is not necessary, but safer */ |
|
406 |
+ if (!eng || !eng->refcount) |
|
407 |
+ return -1; |
|
408 |
+ mp = eng->mempool; |
|
409 |
+ if (!mp) |
|
410 |
+ return -1; |
|
411 |
+ for(mpm = &mp->mpm; mpm; mpm = mpm->next) { |
|
412 |
+ sum_used += mpm->usize; |
|
413 |
+ sum_total += mpm->size; |
|
414 |
+ } |
|
415 |
+ *used = sum_used; |
|
416 |
+ *total = sum_total; |
|
417 |
+ return 0; |
|
417 | 418 |
} |
418 | 419 |
|
419 | 420 |
void *mpool_malloc(struct MP *mp, size_t size) { |
420 |
- unsigned int i, j, needed = align_to_voidptr(size + FRAG_OVERHEAD); |
|
421 |
+ unsigned int i, needed = align_to_voidptr(size + FRAG_OVERHEAD); |
|
421 | 422 |
const unsigned int sbits = to_bits(needed); |
422 | 423 |
struct FRAG *f = NULL; |
423 | 424 |
struct MPMAP *mpm = &mp->mpm; |
... | ... |
@@ -435,6 +461,7 @@ void *mpool_malloc(struct MP *mp, size_t size) { |
435 | 435 |
f->u.sbits = sbits; |
436 | 436 |
#ifdef CL_DEBUG |
437 | 437 |
f->magic = MPOOLMAGIC; |
438 |
+ memset(&f->fake, ALLOCPOISON, size); |
|
438 | 439 |
#endif |
439 | 440 |
return &f->fake; |
440 | 441 |
} |
... | ... |
@@ -453,6 +480,7 @@ void *mpool_malloc(struct MP *mp, size_t size) { |
453 | 453 |
f->u.sbits = sbits; |
454 | 454 |
#ifdef CL_DEBUG |
455 | 455 |
f->magic = MPOOLMAGIC; |
456 |
+ memset(&f->fake, ALLOCPOISON, size); |
|
456 | 457 |
#endif |
457 | 458 |
return &f->fake; |
458 | 459 |
} |
... | ... |
@@ -470,6 +498,9 @@ void *mpool_malloc(struct MP *mp, size_t size) { |
470 | 470 |
spam("failed to alloc %u bytes (%u requested)\n", i, size); |
471 | 471 |
return NULL; |
472 | 472 |
} |
473 |
+#ifdef CL_DEBUG |
|
474 |
+ memset(mpm, ALLOCPOISON, i); |
|
475 |
+#endif |
|
473 | 476 |
mpm->size = i; |
474 | 477 |
mpm->usize = needed + align_to_voidptr(sizeof(*mpm)); |
475 | 478 |
mpm->next = mp->mpm.next; |
... | ... |
@@ -478,7 +509,7 @@ void *mpool_malloc(struct MP *mp, size_t size) { |
478 | 478 |
spam("malloc %p size %u (new map)\n", f, mpool_roundup(size)); |
479 | 479 |
f->u.sbits = sbits; |
480 | 480 |
#ifdef CL_DEBUG |
481 |
- f->magic = MPOOLMAGIC; |
|
481 |
+ f->magic = MPOOLMAGIC; |
|
482 | 482 |
#endif |
483 | 483 |
return &f->fake; |
484 | 484 |
} |
... | ... |
@@ -490,6 +521,8 @@ void mpool_free(struct MP *mp, void *ptr) { |
490 | 490 |
|
491 | 491 |
#ifdef CL_DEBUG |
492 | 492 |
assert(f->magic == MPOOLMAGIC && "Attempt to mpool_free a pointer we did not allocate!"); |
493 |
+ /* FIXME: this fux it up */ |
|
494 |
+ /* memset(&f->fake, FREEPOISON, from_bits(f->u.sbits)); */ |
|
493 | 495 |
#endif |
494 | 496 |
|
495 | 497 |
sbits = f->u.sbits; |
... | ... |
@@ -510,7 +543,7 @@ void *mpool_calloc(struct MP *mp, size_t nmemb, size_t size) { |
510 | 510 |
|
511 | 511 |
void *mpool_realloc(struct MP *mp, void *ptr, size_t size) { |
512 | 512 |
struct FRAG *f = (struct FRAG *)((char *)ptr - FRAG_OVERHEAD); |
513 |
- unsigned int csize, sbits; |
|
513 |
+ unsigned int csize; |
|
514 | 514 |
void *new_ptr; |
515 | 515 |
if (!ptr) return mpool_malloc(mp, size); |
516 | 516 |
|
... | ... |
@@ -533,6 +566,7 @@ void *mpool_realloc2(struct MP *mp, void *ptr, size_t size) { |
533 | 533 |
struct FRAG *f = (struct FRAG *)((char *)ptr - FRAG_OVERHEAD); |
534 | 534 |
unsigned int csize; |
535 | 535 |
void *new_ptr; |
536 |
+ |
|
536 | 537 |
if (!ptr) return mpool_malloc(mp, size); |
537 | 538 |
|
538 | 539 |
spam("realloc @ %p (size %u -> %u))\n", f, from_bits(f->u.sbits), size); |
... | ... |
@@ -550,18 +584,18 @@ void *mpool_realloc2(struct MP *mp, void *ptr, size_t size) { |
550 | 550 |
return new_ptr; |
551 | 551 |
} |
552 | 552 |
|
553 |
-unsigned char *cli_mpool_hex2str(mpool_t *mp, const unsigned char *str) |
|
554 |
-{ |
|
555 |
- unsigned char *tmp = cli_hex2str(str); |
|
556 |
- if(tmp) { |
|
557 |
- unsigned char *res; |
|
558 |
- unsigned int tmpsz = strlen(str) / 2 + 1; |
|
559 |
- if((res = mpool_malloc(mp, tmpsz))) |
|
560 |
- memcpy(res, tmp, tmpsz); |
|
561 |
- free(tmp); |
|
562 |
- return res; |
|
563 |
- } |
|
564 |
- return NULL; |
|
553 |
+unsigned char *cli_mpool_hex2str(mpool_t *mp, const unsigned char *str) { |
|
554 |
+ unsigned char *tmp = (unsigned char *)cli_hex2str((char *)str); |
|
555 |
+ |
|
556 |
+ if(tmp) { |
|
557 |
+ unsigned char *res; |
|
558 |
+ unsigned int tmpsz = strlen((char *)str) / 2 + 1; |
|
559 |
+ if((res = mpool_malloc(mp, tmpsz))) |
|
560 |
+ memcpy(res, tmp, tmpsz); |
|
561 |
+ free(tmp); |
|
562 |
+ return res; |
|
563 |
+ } |
|
564 |
+ return NULL; |
|
565 | 565 |
} |
566 | 566 |
|
567 | 567 |
char *cli_mpool_strdup(mpool_t *mp, const char *s) { |