liballoc-c

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

commit c9c8c8ae30842ac6ad067f6ac42e728ddbfd80d0
parent 29f7aaa6ed8baa6820cb7fe1481c15ebe1e6065b
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sat, 13 May 2023 21:02:16 +0200

Remove inherently broken stack allocator

Diffstat:
MMakefile | 6++++++
Minclude/allocator.h | 2--
Msrc/allocator.c | 68--------------------------------------------------------------------
Msrc/test.c | 6++++--
4 files changed, 10 insertions(+), 72 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,6 +4,12 @@ INCLDIR ?= /include CFLAGS = -I include +ifeq "$(DEBUG)" "1" +CFLAGS += -Og -g +else +CFLAGS += -O2 +endif + include common.mk all: $(LIBALLOC_SO) $(LIBALLOC_A) build/test diff --git a/include/allocator.h b/include/allocator.h @@ -8,7 +8,5 @@ struct allocator { int (*free)(void *p); }; -extern struct allocator stdlib_stack_allocator; -extern struct allocator stdlib_strict_stack_allocator; extern struct allocator stdlib_heap_allocator; extern struct allocator stdlib_strict_heap_allocator; diff --git a/src/allocator.c b/src/allocator.c @@ -2,15 +2,6 @@ #include <errno.h> #include <stdlib.h> -#include <alloca.h> - -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 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 void *stdlib_heap_alloc(size_t size, int *rc); static void *stdlib_heap_realloc(void *p, size_t size, int *rc); @@ -20,18 +11,6 @@ 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 = { - .alloc = stdlib_stack_alloc, - .realloc = stdlib_stack_realloc, - .free = stdlib_stack_free -}; - -struct allocator stdlib_strict_stack_allocator = { - .alloc = stdlib_strict_stack_alloc, - .realloc = stdlib_strict_stack_realloc, - .free = stdlib_strict_stack_free -}; - struct allocator stdlib_heap_allocator = { .alloc = stdlib_heap_alloc, .realloc = stdlib_heap_realloc, @@ -45,53 +24,6 @@ struct allocator stdlib_strict_heap_allocator = { }; void * -stdlib_stack_alloc(size_t size, int *rc) -{ - void *p; - - p = alloca(size); - - return p; -} - -void * -stdlib_stack_realloc(void *p, size_t size, int *rc) -{ - *rc = ENOTSUP; - - return NULL; -} - -int -stdlib_stack_free(void *p) -{ - return 0; -} - -void * -stdlib_strict_stack_alloc(size_t size, int *rc) -{ - void *p; - - p = alloca(size); - if (!p) abort(); - - return p; -} - -void * -stdlib_strict_stack_realloc(void *p, size_t size, int *rc) -{ - abort(); -} - -int -stdlib_strict_stack_free(void *p) -{ - return 0; -} - -void * stdlib_heap_alloc(size_t size, int *rc) { void *p; diff --git a/src/test.c b/src/test.c @@ -2,6 +2,8 @@ #include <err.h> +const struct allocator *ga = &stdlib_strict_heap_allocator; + struct test { int a, b, c; }; @@ -12,9 +14,9 @@ main(int argc, const char **argv) struct test *test; int rc; - test = stdlib_strict_stack_allocator.alloc(sizeof(struct test), NULL); + test = ga->alloc(sizeof(struct test), NULL); if (rc) errx(1, "alloc"); - rc = stdlib_stack_allocator.free(test); + rc = ga->free(test); if (rc) errx(1, "free"); }