lemoncake

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

commit 306e4a301a59328975d8d4229b324c2d90128566
parent 07ababd20495678f216364753f59536c20adb0a6
Author: sin <sin@2f30.org>
Date:   Sun Aug  4 17:06:39 +0100

Simplify internal structures + naming

Diffstat:
lemoncake.c | 74+++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 37 insertions(+), 37 deletions(-)
diff --git a/lemoncake.c b/lemoncake.c @@ -15,8 +15,7 @@ enum { ALIGN = 4 * sizeof(size_t) }; -struct lemoncake_ctx { - bool init; +struct lemon_stats { /* # of mmap calls */ unsigned long nr_mmap; /* # of malloc calls */ @@ -39,7 +38,8 @@ struct lemoncake_ctx { unsigned long nr_invalid_free; }; -static struct lemoncake_ctx ctx; +static bool init; +static struct lemon_stats st; struct node { void *buf; @@ -105,24 +105,24 @@ dumpstats(void) p = getenv("_"); if (p) writelog(fd, "*** Lemoncake stats for process: %s ***\n", p); - writelog(fd, "Number of mmap calls: %lu\n", ctx.nr_mmap); - writelog(fd, "Number of malloc calls: %lu\n", ctx.nr_malloc); - writelog(fd, "Number of realloc calls: %lu\n", ctx.nr_realloc); + writelog(fd, "Number of mmap calls: %lu\n", st.nr_mmap); + writelog(fd, "Number of malloc calls: %lu\n", st.nr_malloc); + writelog(fd, "Number of realloc calls: %lu\n", st.nr_realloc); writelog(fd, "Number of shrinked realloc calls (no memcpy() required): %lu\n", - ctx.nr_shrink_realloc); - writelog(fd, "Number of free calls: %lu\n", ctx.nr_free); - writelog(fd, "Number of new allocations: %lu\n", ctx.nr_alloc_new); + st.nr_shrink_realloc); + writelog(fd, "Number of free calls: %lu\n", st.nr_free); + writelog(fd, "Number of new allocations: %lu\n", st.nr_alloc_new); writelog(fd, "Number of allocations from the free tree: %lu\n", - ctx.nr_alloc_free); - writelog(fd, "Number of invalid free calls: %lu\n", ctx.nr_invalid_free); + st.nr_alloc_free); + writelog(fd, "Number of invalid free calls: %lu\n", st.nr_invalid_free); RB_FOREACH(n, alloc_tree, &at) - ctx.nr_at_nodes++; + st.nr_at_nodes++; RB_FOREACH(n, free_tree, &ft) - ctx.nr_ft_nodes++; + st.nr_ft_nodes++; writelog(fd, "Number of nodes in the allocation tree: %lu\n", - ctx.nr_at_nodes); + st.nr_at_nodes); writelog(fd, "Number of nodes in the free tree: %lu\n", - ctx.nr_ft_nodes); + st.nr_ft_nodes); close(fd); } } @@ -153,21 +153,21 @@ mmap_aligned(size_t align, size_t siz) if (p == MAP_FAILED) return NULL; p = (void *)(((uintptr_t)p + align) & ~(align - 1)); - ctx.nr_mmap++; + st.nr_mmap++; return p; } static int -lemoncake_init(void) +lemon_init(void) { char *p; - if (ctx.init) + if (init) return 0; p = getenv("LEMONCAKE_DEBUG"); if (p) atexit(dumpstats); - ctx.init = true; + init = true; return 0; } @@ -177,7 +177,7 @@ malloc(size_t siz) struct node n, *an, *res; void *p; - if (lemoncake_init()) + if (lemon_init()) return NULL; if (!siz) return NULL; @@ -203,15 +203,15 @@ malloc(size_t siz) an->siz = siz; RB_INSERT(alloc_tree, &at, an); unlock(&rblock); - ctx.nr_malloc++; - ctx.nr_alloc_new++; + st.nr_malloc++; + st.nr_alloc_new++; return an->buf; } an = RB_REMOVE(free_tree, &ft, res); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); - ctx.nr_malloc++; - ctx.nr_alloc_free++; + st.nr_malloc++; + st.nr_alloc_free++; return an->buf; } @@ -222,7 +222,7 @@ realloc(void *oldp, size_t siz) struct node *oldan, *newan; struct node *fn; - if (lemoncake_init()) + if (lemon_init()) return NULL; if (!oldp) return malloc(siz); @@ -239,8 +239,8 @@ realloc(void *oldp, size_t siz) * just re-use it */ if (res->siz >= siz) { unlock(&rblock); - ctx.nr_realloc++; - ctx.nr_shrink_realloc++; + st.nr_realloc++; + st.nr_shrink_realloc++; return res->buf; } oldan = res; @@ -263,12 +263,12 @@ realloc(void *oldp, size_t siz) } newan->siz = siz; RB_INSERT(alloc_tree, &at, newan); - ctx.nr_alloc_new++; + st.nr_alloc_new++; } else { /* Grab the block from the free tree instead */ newan = RB_REMOVE(free_tree, &ft, res); RB_INSERT(alloc_tree, &at, newan); - ctx.nr_alloc_free++; + st.nr_alloc_free++; } /* Copy over the contents from `oldp' to the * new memory block */ @@ -282,7 +282,7 @@ realloc(void *oldp, size_t siz) RB_INSERT(free_tree, &ft, fn); } unlock(&rblock); - ctx.nr_realloc++; + st.nr_realloc++; return newan->buf; } unlock(&rblock); @@ -294,7 +294,7 @@ calloc(size_t nmemb, size_t siz) { void *p; - if (lemoncake_init()) + if (lemon_init()) return NULL; p = malloc(nmemb * siz); if (!p) @@ -308,7 +308,7 @@ free(void *p) { struct node n, *fn, *res; - if (lemoncake_init()) + if (lemon_init()) return; if (!p) return; @@ -318,9 +318,9 @@ free(void *p) if (res) { fn = RB_REMOVE(alloc_tree, &at, res); RB_INSERT(free_tree, &ft, fn); - ctx.nr_free++; + st.nr_free++; } else { - ctx.nr_invalid_free++; + st.nr_invalid_free++; } unlock(&rblock); } @@ -337,7 +337,7 @@ memalign(size_t align, size_t siz) struct node *an; void *p; - if (lemoncake_init()) + if (lemon_init()) return NULL; if (((align - 1) & align)) return NULL; @@ -362,7 +362,7 @@ memalign(size_t align, size_t siz) lock(&rblock); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); - ctx.nr_alloc_new++; + st.nr_alloc_new++; return p; } @@ -409,7 +409,7 @@ malloc_usable_size(void *p) { struct node n, *res; - if (lemoncake_init()) + if (lemon_init()) return 0; if (!p) return 0;