scripts

misc scripts and tools
git clone git://git.2f30.org/scripts
Log | Files | Refs

commit b488df2fd8e62e67360404ac8bdf2af647934257
parent 349b0a2660be259803da88a83a094e465dd40005
Author: sin <sin@2f30.org>
Date:   Fri,  2 Aug 2013 13:52:48 +0100

Implement memalign(), aligned_alloc(), valloc() and pvalloc()

Diffstat:
Mrandom/rballoc.c | 83+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 47 insertions(+), 36 deletions(-)

diff --git a/random/rballoc.c b/random/rballoc.c @@ -235,17 +235,16 @@ free(void *p) unlock(&rblock); } -int -posix_memalign(void **memptr, size_t align, size_t siz) +void * +memalign(size_t align, size_t siz) { struct node *an; void *p; - *memptr = NULL; if (((align - 1) & align)) - return EINVAL; + return NULL; if (align < sizeof(void *)) - return EINVAL; + return NULL; if (!siz) return 0; /* Just allocate a new block, we don't care to look @@ -256,16 +255,56 @@ posix_memalign(void **memptr, size_t align, size_t siz) * not really matter. */ an = alloc_object(sizeof(*an)); if (!an) - return ENOMEM; + return NULL; p = mmap_aligned(align, siz); if (!p) - return ENOMEM; + return NULL; an->buf = p; an->siz = siz; lock(&rblock); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); - *memptr = p; + return p; +} + +void * +aligned_alloc(size_t align, size_t siz) +{ + if (siz % align) + return NULL; + return memalign(align, siz); +} + +void * +valloc(size_t siz) +{ + return memalign(sysconf(_SC_PAGESIZE), siz); +} + +void * +pvalloc(size_t siz) +{ + long pagesize = sysconf(_SC_PAGESIZE); + siz = pagesize * ((siz + pagesize - 1) / pagesize); + return valloc(siz); +} + +int +posix_memalign(void **memptr, size_t align, size_t siz) +{ + struct node *an; + void *p; + + *memptr = NULL; + if (((align - 1) & align)) + return EINVAL; + if (align < sizeof(void *)) + return EINVAL; + if (!siz) + return 0; + *memptr = memalign(align, siz); + if (!*memptr) + return ENOMEM; return 0; } @@ -292,31 +331,3 @@ malloc_size(void *p) { return malloc_usable_size(p); } - -void * -memalign(size_t align, size_t siz) -{ - fprintf(stderr, "%s: not implemented!\n", __func__); - abort(); -} - -void * -pvalloc(size_t siz) -{ - fprintf(stderr, "%s: not implemented!\n", __func__); - abort(); -} - -void * -aligned_alloc(size_t align, size_t siz) -{ - fprintf(stderr, "%s: not implemented!\n", __func__); - abort(); -} - -void * -valloc(size_t siz) -{ - fprintf(stderr, "%s: not implemented!\n", __func__); - abort(); -}