lemoncake

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

commit f37195e6c9843cd5fe2c3070af3c3d4bddda51c0
parent 1f477047c116ecb91998d66d65dfa797b265aa5b
Author: sin <sin@2f30.org>
Date:   Wed Aug 28 11:16:11 +0100

Rename `siz' to `sz'

Diffstat:
lemoncake.c | 100++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 50 insertions(+), 50 deletions(-)
diff --git a/lemoncake.c b/lemoncake.c @@ -50,7 +50,7 @@ static bool strict = false; struct node { void *buf; - size_t siz; + size_t sz; RB_ENTRY(node) entry; }; @@ -65,13 +65,13 @@ static int at_cmp(struct node *a, struct node *b); RB_PROTOTYPE(alloc_tree, node, entry, at_cmp) RB_GENERATE(free_tree, node, entry, ft_cmp) -/* These are ordered by `siz' */ +/* These are ordered by `sz' */ static int ft_cmp(struct node *a, struct node *b) { - if (a->siz < b->siz) + if (a->sz < b->sz) return -1; - if (a->siz > b->siz) + if (a->sz > b->sz) return 1; return 0; } @@ -95,11 +95,11 @@ align_pointer(size_t align, void *p) } static inline void * -alloc_object(size_t siz) +alloc_object(size_t sz) { void *base, *p; - base = sbrk(siz + ALIGN); + base = sbrk(sz + ALIGN); if (base == (void *)-1) return NULL; p = base; @@ -108,14 +108,14 @@ alloc_object(size_t siz) } static inline void * -mmap_aligned(size_t align, size_t siz) +mmap_aligned(size_t align, size_t sz) { void *p; /* align should be a power of two */ if ((align - 1) & align) return NULL; - p = mmap(0, siz + align, PROT_READ | PROT_WRITE, + p = mmap(0, sz + align, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (p == MAP_FAILED) return NULL; @@ -138,21 +138,21 @@ lemon_init(void) } void * -malloc(size_t siz) +malloc(size_t sz) { struct node n, *an, *res; void *p; if (lemon_init()) return NULL; - if (!siz) + if (!sz) return NULL; - if (siz > SIZE_MAX / 2) + if (sz > SIZE_MAX / 2) return NULL; lock(&rblock); /* Lookup in the free tree for a block greater - * than or equal to `siz' bytes */ - n.siz = siz; + * than or equal to `sz' bytes */ + n.sz = sz; res = RB_NFIND(free_tree, &ft, &n); if (!res) { /* No available free block, create a new block @@ -162,13 +162,13 @@ malloc(size_t siz) unlock(&rblock); return NULL; } - p = mmap_aligned(ALIGN, siz); + p = mmap_aligned(ALIGN, sz); if (!p) { unlock(&rblock); return NULL; } an->buf = p; - an->siz = siz; + an->sz = sz; RB_INSERT(alloc_tree, &at, an); unlock(&rblock); st.nr_malloc++; @@ -188,14 +188,14 @@ align_node(struct node *n) { void *p = n->buf; - if (n->siz < ALIGN) + if (n->sz < ALIGN) return; n->buf = align_pointer(ALIGN, n->buf); - n->siz = n->siz - ((uintptr_t)n->buf - (uintptr_t)p); + n->sz = n->sz - ((uintptr_t)n->buf - (uintptr_t)p); } void * -realloc(void *oldp, size_t siz) +realloc(void *oldp, size_t sz) { struct node n, *res; struct node *oldan, *newan; @@ -204,25 +204,25 @@ realloc(void *oldp, size_t siz) if (lemon_init()) return NULL; if (!oldp) - return malloc(siz); - if (!siz) { + return malloc(sz); + if (!sz) { if (oldp) free(oldp); return NULL; } - if (siz > SIZE_MAX / 2) + if (sz > SIZE_MAX / 2) return NULL; lock(&rblock); n.buf = oldp; res = RB_FIND(alloc_tree, &at, &n); if (res) { - if (res->siz == siz) { + if (res->sz == sz) { unlock(&rblock); st.nr_realloc++; return res->buf; } - if (res->siz > siz) { - size_t diff = res->siz - siz; + if (res->sz > sz) { + size_t diff = res->sz - sz; /* We cannot ensure the alignment of the free * block if its size is less than ALIGN - so just * don't split the node in that case */ @@ -236,9 +236,9 @@ realloc(void *oldp, size_t siz) unlock(&rblock); return NULL; } - res->siz = siz; - newfn->buf = res->buf + siz; - newfn->siz = diff; + res->sz = sz; + newfn->buf = res->buf + sz; + newfn->sz = diff; align_node(newfn); RB_INSERT(free_tree, &ft, newfn); unlock(&rblock); @@ -248,8 +248,8 @@ realloc(void *oldp, size_t siz) } oldan = res; /* Lookup in the free tree for a block greater - * than or equal to `siz' bytes */ - n.siz = siz; + * than or equal to `sz' bytes */ + n.sz = sz; res = RB_NFIND(free_tree, &ft, &n); if (!res) { /* No available free block, create a new block @@ -259,12 +259,12 @@ realloc(void *oldp, size_t siz) unlock(&rblock); return NULL; } - newan->buf = mmap_aligned(ALIGN, siz); + newan->buf = mmap_aligned(ALIGN, sz); if (!newan->buf) { unlock(&rblock); return NULL; } - newan->siz = siz; + newan->sz = sz; RB_INSERT(alloc_tree, &at, newan); st.nr_alloc_new++; } else { @@ -276,7 +276,7 @@ realloc(void *oldp, size_t siz) /* Copy over the contents from `oldp' to the * new memory block */ memcpy(newan->buf, oldan->buf, - siz < oldan->siz ? siz : oldan->siz); + sz < oldan->sz ? sz : oldan->sz); /* Return `oldp' to the free tree */ fn = RB_REMOVE(alloc_tree, &at, oldan); RB_INSERT(free_tree, &ft, fn); @@ -289,16 +289,16 @@ realloc(void *oldp, size_t siz) } void * -calloc(size_t nmemb, size_t siz) +calloc(size_t nmemb, size_t sz) { void *p; if (lemon_init()) return NULL; - p = malloc(nmemb * siz); + p = malloc(nmemb * sz); if (!p) return NULL; - memset(p, 0, nmemb * siz); + memset(p, 0, nmemb * sz); return p; } @@ -335,7 +335,7 @@ cfree(void *p) } void * -memalign(size_t align, size_t siz) +memalign(size_t align, size_t sz) { struct node *an; void *p; @@ -357,11 +357,11 @@ memalign(size_t align, size_t siz) an = alloc_object(sizeof(*an)); if (!an) return NULL; - p = mmap_aligned(align, siz); + p = mmap_aligned(align, sz); if (!p) return NULL; an->buf = p; - an->siz = siz; + an->sz = sz; lock(&rblock); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); @@ -370,38 +370,38 @@ memalign(size_t align, size_t siz) } void * -aligned_alloc(size_t align, size_t siz) +aligned_alloc(size_t align, size_t sz) { - if (siz % align) + if (sz % align) return NULL; - return memalign(align, siz); + return memalign(align, sz); } void * -valloc(size_t siz) +valloc(size_t sz) { - return memalign(sysconf(_SC_PAGESIZE), siz); + return memalign(sysconf(_SC_PAGESIZE), sz); } void * -pvalloc(size_t siz) +pvalloc(size_t sz) { long pagesize = sysconf(_SC_PAGESIZE); - siz = pagesize * ((siz + pagesize - 1) / pagesize); - return valloc(siz); + sz = pagesize * ((sz + pagesize - 1) / pagesize); + return valloc(sz); } int -posix_memalign(void **memptr, size_t align, size_t siz) +posix_memalign(void **memptr, size_t align, size_t sz) { *memptr = NULL; if (((align - 1) & align)) return EINVAL; if (align < sizeof(void *)) return EINVAL; - if (siz > SIZE_MAX - align) + if (sz > SIZE_MAX - align) return ENOMEM; - *memptr = memalign(align, siz); + *memptr = memalign(align, sz); if (!*memptr) return ENOMEM; return 0; @@ -421,7 +421,7 @@ malloc_usable_size(void *p) res = RB_FIND(alloc_tree, &at, &n); if (res) { unlock(&rblock); - return res->siz; + return res->sz; } unlock(&rblock); return 0;