liballoc-c

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

commit c4011ee8089f2f0680d44670c59381dd9bed3987
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 12 Sep 2022 15:57:41 +0200

Stash progress

Diffstat:
A.gitignore | 5+++++
AMakefile | 41+++++++++++++++++++++++++++++++++++++++++
Ainclude/alloc.h | 9+++++++++
Aliballoc.api | 2++
Aliballoc.lds | 7+++++++
Asrc/alloc.c | 16++++++++++++++++
Asrc/test.c | 11+++++++++++
7 files changed, 91 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,5 @@ +compile_commands.json +build +.cache +vgcore* +test diff --git a/Makefile b/Makefile @@ -0,0 +1,41 @@ +CFLAGS = -I include +LDLIBS = +DEPFLAGS = -MT $@ -MMD -MP -MF build/$*.d + +_SRCS = alloc.c +SRCS = $(_SRCS:%.c=src/%.c) +OBJS = $(_SRCS:%.c=build/%.o) +DEPS = $(_SRCS:%.c=build/%.d) +PI_OBJS = $(_SRCS:%.c=build/%.pi.o) + +.PHONY: all clean + +all: build/liballoc.so build/liballoc.a # build/test + +clean: + rm -rf build + +build: + mkdir build + +build/%.o: src/%.c build/%.d | build + $(CC) -c -o $@ $< $(DEPFLAGS) $(CFLAGS) + +build/%.pi.o: src/%.c build/%.d | build + $(CC) -c -o $@ $< $(DEPFLAGS) $(CFLAGS) -fPIC + +build/%.d: | build; + +include $(DEPS) + +build/liballoc.a: $(OBJS) $(LIBLIST_A) | build + $(CC) -o build/tmp.o $^ $(CFLAGS) -r + objcopy --keep-global-symbols=liballoc.api build/tmp.o build/fixed.o + ar rcs $@ build/fixed.o + +build/liballoc.so: $(PI_OBJS) $(LIBLIST_SO) | build + $(CC) -o $@ $(PI_OBJS) $(CFLAGS) -shared -Wl,-version-script liballoc.lds + +# build/test: src/test.c build/liballoc.a | build +# $(CC) -o $@ $^ -I include + diff --git a/include/alloc.h b/include/alloc.h @@ -0,0 +1,9 @@ +#include <stdlib.h> + +struct allocator { + void *(*alloc)(size_t size); + void (*free)(void *p); +}; + +extern struct allocator stack_allocator; +extern struct allocator heap_allocator; diff --git a/liballoc.api b/liballoc.api @@ -0,0 +1,2 @@ +stack_allocator +heap_allocator diff --git a/liballoc.lds b/liballoc.lds @@ -0,0 +1,7 @@ +LIBALLOC_1.0 { + global: + stack_allocator; + heap_allocator; + local: + *; +}; diff --git a/src/alloc.c b/src/alloc.c @@ -0,0 +1,16 @@ +#include "alloc.h" + +#include <stdlib.h> +#include <alloca.h> + +static void stub() {}; + +struct allocator stack_allocator = { + .alloc = alloca, + .free = stub +}; + +struct allocator heap_allocator = { + .alloc = malloc, + .free = free +}; diff --git a/src/test.c b/src/test.c @@ -0,0 +1,11 @@ +#include "alloc.h" + +void *test_alloc(struct allocator *ator); + +int +main(int argc, const char **argv) +{ + struct test *test; + + test = test_alloc(&stack_allocator); +}