commit 322bdbc251ccb21e967c75ee1b88ce152fc99982
parent c4011ee8089f2f0680d44670c59381dd9bed3987
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 17 Mar 2023 18:53:32 +0100
Refactor and add realloc
Diffstat:
8 files changed, 124 insertions(+), 62 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,16 +1,10 @@
-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)
+PREFIX ?= /usr/local
+LIBDIR ?= /lib
+INCLDIR ?= /include
-.PHONY: all clean
+CFLAGS = -I include
-all: build/liballoc.so build/liballoc.a # build/test
+all: build/liballoc.so build/liballoc.a build/test
clean:
rm -rf build
@@ -18,24 +12,26 @@ clean:
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
+build/liballoc.a: src/allocator.c include/allocator.h | build
+ $(CC) -o build/tmp.o src/allocator.c $(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/liballoc.so: src/allocator.c include/allocator.h | build
+ $(CC) -o $@ src/allocator.c $(CFLAGS) \
+ -shared -Wl,-version-script liballoc.lds
+
+build/test: src/test.c build/liballoc.a | build
+ $(CC) -o $@ $^ -I include
+
+install:
+ install -m755 include/allocator.h -t "$(DESTDIR)$(PREFIX)$(INCLDIR)"
+ install -m755 build/liballoc.a -t "$(DESTDIR)$(PREFIX)$(LIBDIR)"
+ install -m755 build/liballoc.so -t "$(DESTDIR)$(PREFIX)$(LIBDIR)"
-# build/test: src/test.c build/liballoc.a | build
-# $(CC) -o $@ $^ -I include
+uninstall:
+ rm -f "$(DESTDIR)$(PREFIX)$(INCLDIR)/allocator.h"
+ rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/liballoc.a"
+ rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/liballoc.so"
+.PHONY: all clean install uninstall
diff --git a/include/alloc.h b/include/alloc.h
@@ -1,9 +0,0 @@
-#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/include/allocator.h b/include/allocator.h
@@ -0,0 +1,10 @@
+#include <stdlib.h>
+
+struct allocator {
+ int (*alloc)(void **p, size_t size);
+ int (*realloc)(void **p, size_t size);
+ int (*free)(void *p);
+};
+
+extern struct allocator stdlib_stack_allocator;
+extern struct allocator stdlib_heap_allocator;
diff --git a/liballoc.api b/liballoc.api
@@ -1,2 +1,2 @@
-stack_allocator
-heap_allocator
+stdlib_stack_allocator
+stdlib_heap_allocator
diff --git a/liballoc.lds b/liballoc.lds
@@ -1,7 +1,6 @@
-LIBALLOC_1.0 {
+LIBALLOC_1.1 {
global:
- stack_allocator;
- heap_allocator;
- local:
- *;
+ stdlib_stack_allocator;
+ stdlib_heap_allocator;
+ local: *;
};
diff --git a/src/alloc.c b/src/alloc.c
@@ -1,16 +0,0 @@
-#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/allocator.c b/src/allocator.c
@@ -0,0 +1,73 @@
+#include "allocator.h"
+
+#include <errno.h>
+#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 int stdlib_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 int stdlib_heap_free(void *p);
+
+struct allocator stdlib_stack_allocator = {
+ .alloc = stdlib_stack_alloc,
+ .realloc = stdlib_stack_realloc,
+ .free = stdlib_stack_free
+};
+
+struct allocator stdlib_heap_allocator = {
+ .alloc = stdlib_heap_alloc,
+ .realloc = stdlib_heap_realloc,
+ .free = stdlib_heap_free
+};
+
+int
+stdlib_stack_alloc(void **p, size_t size)
+{
+ *p = alloca(size);
+
+ return *p ? 0 : 1;
+}
+
+int
+stdlib_stack_realloc(void **p, size_t size)
+{
+ return 1;
+}
+
+int
+stdlib_stack_free(void *p)
+{
+ return 0;
+}
+
+int
+stdlib_heap_alloc(void **p, size_t size)
+{
+ *p = malloc(size);
+
+ return *p ? 0 : -errno;
+}
+
+int
+stdlib_heap_realloc(void **p, size_t size)
+{
+ void *np;
+
+ np = realloc(*p, size);
+ if (!np) return -errno;
+ *p = np;
+
+ return 0;
+}
+
+int
+stdlib_heap_free(void *p)
+{
+ free(p);
+
+ return 0;
+}
diff --git a/src/test.c b/src/test.c
@@ -1,11 +1,20 @@
-#include "alloc.h"
+#include "allocator.h"
-void *test_alloc(struct allocator *ator);
+#include <err.h>
+
+struct test {
+ int a, b, c;
+};
int
main(int argc, const char **argv)
{
struct test *test;
+ int rc;
+
+ rc = stdlib_stack_allocator.alloc((void **)&test, sizeof(struct test));
+ if (rc) errx(1, "alloc");
- test = test_alloc(&stack_allocator);
+ rc = stdlib_stack_allocator.free(test);
+ if (rc) errx(1, "free");
}