liballoc-c

C generic allocator interface
git clone https://git.sinitax.com/sinitax/liballoc-c
Log | Files | Refs | LICENSE | sfeed.txt

commit 29f7aaa6ed8baa6820cb7fe1481c15ebe1e6065b
parent 6907e8708e6aaa9c037f771b9f0dde2fbd419cd4
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sat, 13 May 2023 16:49:40 +0200

Change allocator interface to prevent void** cast

Diffstat:
Minclude/allocator.h | 4++--
Mliballoc.lds | 2+-
Msrc/allocator.c | 95+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/test.c | 2+-
4 files changed, 56 insertions(+), 47 deletions(-)

diff --git a/include/allocator.h b/include/allocator.h @@ -3,8 +3,8 @@ #include <stddef.h> struct allocator { - int (*alloc)(void **p, size_t size); - int (*realloc)(void **p, size_t size); + void *(*alloc)(size_t size, int *rc); + void *(*realloc)(void *p, size_t size, int *rc); int (*free)(void *p); }; diff --git a/liballoc.lds b/liballoc.lds @@ -1,4 +1,4 @@ -LIBALLOC_1.1 { +LIBALLOC_2.0 { global: stdlib_stack_allocator; stdlib_strict_stack_allocator; diff --git a/src/allocator.c b/src/allocator.c @@ -4,18 +4,20 @@ #include <stdlib.h> #include <alloca.h> -static int stdlib_stack_alloc(void **p, size_t size); -static int stdlib_stack_realloc(void **p, size_t size); +static void *stdlib_stack_alloc(size_t size, int *rc); +static void *stdlib_stack_realloc(void *p, size_t size, int *rc); static int stdlib_stack_free(void *p); -static int stdlib_strict_stack_alloc(void **p, size_t size); -static int stdlib_strict_stack_realloc(void **p, size_t size); + +static void *stdlib_strict_stack_alloc(size_t size, int *rc); +static void *stdlib_strict_stack_realloc(void *p, size_t size, int *rc); static int stdlib_strict_stack_free(void *p); -static int stdlib_heap_alloc(void **p, size_t size); -static int stdlib_heap_realloc(void **p, size_t size); +static void *stdlib_heap_alloc(size_t size, int *rc); +static void *stdlib_heap_realloc(void *p, size_t size, int *rc); static int stdlib_heap_free(void *p); -static int stdlib_strict_heap_alloc(void **p, size_t size); -static int stdlib_strict_heap_realloc(void **p, size_t size); + +static void *stdlib_strict_heap_alloc(size_t size, int *rc); +static void *stdlib_strict_heap_realloc(void *p, size_t size, int *rc); static int stdlib_strict_heap_free(void *p); struct allocator stdlib_stack_allocator = { @@ -42,20 +44,22 @@ struct allocator stdlib_strict_heap_allocator = { .free = stdlib_strict_heap_free }; -int -stdlib_stack_alloc(void **p, size_t size) +void * +stdlib_stack_alloc(size_t size, int *rc) { - *p = alloca(size); + void *p; - return *p ? 0 : ENOMEM; + p = alloca(size); + + return p; } -int -stdlib_stack_realloc(void **p, size_t size) +void * +stdlib_stack_realloc(void *p, size_t size, int *rc) { - *p = NULL; + *rc = ENOTSUP; - return ENOTSUP; + return NULL; } int @@ -64,17 +68,19 @@ stdlib_stack_free(void *p) return 0; } -int -stdlib_strict_stack_alloc(void **p, size_t size) +void * +stdlib_strict_stack_alloc(size_t size, int *rc) { - *p = alloca(size); - if (!*p) abort(); + void *p; - return 0; + p = alloca(size); + if (!p) abort(); + + return p; } -int -stdlib_strict_stack_realloc(void **p, size_t size) +void * +stdlib_strict_stack_realloc(void *p, size_t size, int *rc) { abort(); } @@ -85,24 +91,26 @@ stdlib_strict_stack_free(void *p) return 0; } -int -stdlib_heap_alloc(void **p, size_t size) +void * +stdlib_heap_alloc(size_t size, int *rc) { - *p = malloc(size); + void *p; + + p = malloc(size); + if (rc && !p) *rc = errno; - return *p ? 0 : errno; + return p; } -int -stdlib_heap_realloc(void **p, size_t size) +void * +stdlib_heap_realloc(void *p, size_t size, int *rc) { void *np; - np = realloc(*p, size); - if (!np) return errno; - *p = np; + np = realloc(p, size); + if (rc && !np) *rc = errno; - return 0; + return np; } int @@ -113,25 +121,26 @@ stdlib_heap_free(void *p) return 0; } -int -stdlib_strict_heap_alloc(void **p, size_t size) +void * +stdlib_strict_heap_alloc(size_t size, int *rc) { - *p = malloc(size); - if (!*p) abort(); + void *p; - return 0; + p = malloc(size); + if (!p) abort(); + + return p; } -int -stdlib_strict_heap_realloc(void **p, size_t size) +void * +stdlib_strict_heap_realloc(void *p, size_t size, int *rc) { void *np; - np = realloc(*p, size); + np = realloc(p, size); if (!np) abort(); - *p = np; - return 0; + return np; } int diff --git a/src/test.c b/src/test.c @@ -12,7 +12,7 @@ main(int argc, const char **argv) struct test *test; int rc; - rc = stdlib_stack_allocator.alloc((void **)&test, sizeof(struct test)); + test = stdlib_strict_stack_allocator.alloc(sizeof(struct test), NULL); if (rc) errx(1, "alloc"); rc = stdlib_stack_allocator.free(test);