lemoncake

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

commit 3baae49ec28d9c310049d1d00b269a57647750cd
parent 93a0b753f23a4823c90cc6bab76478b638c21ae0
Author: sin <sin@2f30.org>
Date:   Mon,  5 Aug 2013 11:32:54 +0100

Implement align_node()

Factor out the code for aligning the base memory in a node to
make the code more readable.

Diffstat:
Mlemoncake.c | 15++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/lemoncake.c b/lemoncake.c @@ -181,6 +181,17 @@ malloc(size_t siz) return an->buf; } +static inline void +align_node(struct node *n) +{ + void *p = n->buf; + + if (n->siz < ALIGN) + return; + n->buf = align_pointer(ALIGN, n->buf); + n->siz = n->siz - ((uintptr_t)n->buf - (uintptr_t)p); +} + void * realloc(void *oldp, size_t siz) { @@ -222,9 +233,7 @@ realloc(void *oldp, size_t siz) res->siz = siz; newfn->buf = res->buf + siz; newfn->siz = diff; - newfn->siz -= (uintptr_t)align_pointer(ALIGN, newfn->buf); - newfn->siz += (uintptr_t)newfn->buf; - newfn->buf = align_pointer(ALIGN, newfn->buf); + align_node(newfn); RB_INSERT(free_tree, &ft, newfn); unlock(&rblock); st.nr_realloc++;