lemoncake

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

commit 0869b50c4cbbe5418448859562942afa0b34981a
parent dc4dfa2c8743816c2c7bff77da46172a3ed64226
Author: sin <sin@2f30.org>
Date:   Fri,  2 Aug 2013 16:18:09 +0100

Dump more stats

Diffstat:
Mlemoncake.c | 16+++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/lemoncake.c b/lemoncake.c @@ -16,6 +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; }; static struct lemoncake_ctx ctx; @@ -70,7 +74,11 @@ dump_trees(void) fd = open("lemoncake.out", O_WRONLY | O_CREAT | O_APPEND, 0644); if (fd != -1) { snprintf(buf, sizeof(buf), - "*** Dumping alloc + free trees ***\n"); + "*** Dumping lemoncake stats ***\n"); + 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), @@ -114,6 +122,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++; return p; } @@ -169,11 +178,13 @@ malloc(size_t siz) an->siz = siz; RB_INSERT(alloc_tree, &at, an); unlock(&rblock); + ctx.mallocs++; return an->buf; } an = RB_REMOVE(free_tree, &ft, res); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); + ctx.mallocs++; return an->buf; } @@ -201,6 +212,7 @@ realloc(void *oldp, size_t siz) * just re-use it */ if (res->siz >= siz) { unlock(&rblock); + ctx.reallocs++; return res->buf; } oldan = res; @@ -240,6 +252,7 @@ realloc(void *oldp, size_t siz) RB_INSERT(free_tree, &ft, fn); } unlock(&rblock); + ctx.reallocs++; return newan->buf; } unlock(&rblock); @@ -277,6 +290,7 @@ free(void *p) RB_INSERT(free_tree, &ft, fn); } unlock(&rblock); + ctx.frees++; } void