lemoncake

rbtree based memory allocator
git clone git://git.2f30.org/lemoncake.git
Log | Files | Refs | README | LICENSE

commit 547dd133564d893f8047312f2ea4d5e4d023514e
parent 0869b50c4cbbe5418448859562942afa0b34981a
Author: sin <sin@2f30.org>
Date:   Fri Aug  2 16:34:45 +0100

Minimize debug output

Diffstat:
lemoncake.c | 40++++++++++++----------------------------
1 file changed, 12 insertions(+), 28 deletions(-)
diff --git a/lemoncake.c b/lemoncake.c @@ -16,10 +16,10 @@ enum { ALIGN = 4 * sizeof(size_t) }; struct lemoncake_ctx { bool init; - unsigned long mmaps; - unsigned long mallocs; - unsigned long reallocs; - unsigned long frees; + unsigned long nr_mmap; + unsigned long nr_malloc; + unsigned long nr_realloc; + unsigned long nr_free; }; static struct lemoncake_ctx ctx; @@ -67,31 +67,15 @@ at_cmp(struct node *a, struct node *b) static void dump_trees(void) { - struct node *n; char buf[BUFSIZ]; int fd; fd = open("lemoncake.out", O_WRONLY | O_CREAT | O_APPEND, 0644); if (fd != -1) { snprintf(buf, sizeof(buf), - "*** Dumping lemoncake stats ***\n"); + "nr_mmap: %lu, nr_malloc: %lu, nr_realloc: %lu, nr_free: %lu\n", + ctx.nr_mmap, ctx.nr_malloc, ctx.nr_realloc, ctx.nr_free); write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), - "mmaps: %lu, mallocs: %lu, reallocs: %lu, frees: %lu\n", - ctx.mmaps, ctx.mallocs, ctx.reallocs, ctx.frees); - write(fd, buf, strlen(buf)); - RB_FOREACH(n, alloc_tree, &at) { - snprintf(buf, sizeof(buf), - "alloc: buf: %p, size: %zu\n", - n->buf, n->siz); - write(fd, buf, strlen(buf)); - } - RB_FOREACH(n, free_tree, &ft) { - snprintf(buf, sizeof(buf), - "free: buf: %p, size: %zu\n", - n->buf, n->siz); - write(fd, buf, strlen(buf)); - } close(fd); } } @@ -122,7 +106,7 @@ mmap_aligned(size_t align, size_t siz) if (p == MAP_FAILED) return NULL; p = (void *)(((uintptr_t)p + align) & ~(align - 1)); - ctx.mmaps++; + ctx.nr_mmap++; return p; } @@ -178,13 +162,13 @@ malloc(size_t siz) an->siz = siz; RB_INSERT(alloc_tree, &at, an); unlock(&rblock); - ctx.mallocs++; + ctx.nr_malloc++; return an->buf; } an = RB_REMOVE(free_tree, &ft, res); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); - ctx.mallocs++; + ctx.nr_malloc++; return an->buf; } @@ -212,7 +196,7 @@ realloc(void *oldp, size_t siz) * just re-use it */ if (res->siz >= siz) { unlock(&rblock); - ctx.reallocs++; + ctx.nr_realloc++; return res->buf; } oldan = res; @@ -252,7 +236,7 @@ realloc(void *oldp, size_t siz) RB_INSERT(free_tree, &ft, fn); } unlock(&rblock); - ctx.reallocs++; + ctx.nr_realloc++; return newan->buf; } unlock(&rblock); @@ -290,7 +274,7 @@ free(void *p) RB_INSERT(free_tree, &ft, fn); } unlock(&rblock); - ctx.frees++; + ctx.nr_free++; } void