commit fa03741cf151f639e361b556f9e60d1925f41d42
parent db114713e3958e350da181c8894ccf330320f66b
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 21 May 2023 17:40:20 +0200
Add allocator argument for non-global allocators
Diffstat:
4 files changed, 35 insertions(+), 26 deletions(-)
diff --git a/include/allocator.h b/include/allocator.h
@@ -3,9 +3,11 @@
#include <stddef.h>
struct allocator {
- void *(*alloc)(size_t size, int *rc);
- void *(*realloc)(void *p, size_t size, int *rc);
- int (*free)(void *p);
+ void *(*alloc)(const struct allocator *allocator,
+ size_t size, int *rc);
+ void *(*realloc)(const struct allocator *allocator,
+ void *p, size_t size, int *rc);
+ int (*free)(const struct allocator *allocator, void *p);
};
extern struct allocator stdlib_heap_allocator;
diff --git a/liballoc.lds b/liballoc.lds
@@ -1,4 +1,4 @@
-LIBALLOC_2.0 {
+LIBALLOC_2.1 {
global:
stdlib_stack_allocator;
stdlib_strict_stack_allocator;
diff --git a/src/allocator.c b/src/allocator.c
@@ -3,13 +3,17 @@
#include <errno.h>
#include <stdlib.h>
-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 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);
+static void *stdlib_heap_alloc(const struct allocator *allocator,
+ size_t size, int *rc);
+static void *stdlib_heap_realloc(const struct allocator *allocator,
+ void *p, size_t size, int *rc);
+static int stdlib_heap_free(const struct allocator *allocator, void *p);
+
+static void *stdlib_strict_heap_alloc(const struct allocator *allocator,
+ size_t size, int *rc);
+static void *stdlib_strict_heap_realloc(const struct allocator *allocator,
+ void *p, size_t size, int *rc);
+static int stdlib_strict_heap_free(const struct allocator *allocator, void *p);
struct allocator stdlib_heap_allocator = {
.alloc = stdlib_heap_alloc,
@@ -24,7 +28,7 @@ struct allocator stdlib_strict_heap_allocator = {
};
void *
-stdlib_heap_alloc(size_t size, int *rc)
+stdlib_heap_alloc(const struct allocator *allocator, size_t size, int *rc)
{
void *p;
@@ -35,7 +39,8 @@ stdlib_heap_alloc(size_t size, int *rc)
}
void *
-stdlib_heap_realloc(void *p, size_t size, int *rc)
+stdlib_heap_realloc(const struct allocator *allocator,
+ void *p, size_t size, int *rc)
{
void *np;
@@ -46,7 +51,7 @@ stdlib_heap_realloc(void *p, size_t size, int *rc)
}
int
-stdlib_heap_free(void *p)
+stdlib_heap_free(const struct allocator *allocator, void *p)
{
free(p);
@@ -54,7 +59,8 @@ stdlib_heap_free(void *p)
}
void *
-stdlib_strict_heap_alloc(size_t size, int *rc)
+stdlib_strict_heap_alloc(const struct allocator *allocator,
+ size_t size, int *rc)
{
void *p;
@@ -65,7 +71,8 @@ stdlib_strict_heap_alloc(size_t size, int *rc)
}
void *
-stdlib_strict_heap_realloc(void *p, size_t size, int *rc)
+stdlib_strict_heap_realloc(const struct allocator *allocator,
+ void *p, size_t size, int *rc)
{
void *np;
@@ -76,7 +83,7 @@ stdlib_strict_heap_realloc(void *p, size_t size, int *rc)
}
int
-stdlib_strict_heap_free(void *p)
+stdlib_strict_heap_free(const struct allocator *allocator, void *p)
{
free(p);
diff --git a/src/test.c b/src/test.c
@@ -1,12 +1,10 @@
#include "allocator.h"
#include <err.h>
+#include <string.h>
+#include <stdlib.h>
-const struct allocator *ga = &stdlib_strict_heap_allocator;
-
-struct test {
- int a, b, c;
-};
+const struct allocator *ga = &stdlib_heap_allocator;
int
main(int argc, const char **argv)
@@ -14,9 +12,11 @@ main(int argc, const char **argv)
struct test *test;
int rc;
- test = ga->alloc(sizeof(struct test), NULL);
- if (rc) errx(1, "alloc");
+ if (argc < 1) exit(1);
+
+ test = ga->alloc(ga, strtoull(argv[1], NULL, 10), &rc);
+ if (!test) errx(1, "alloc: %s", strerror(rc));
- rc = ga->free(test);
- if (rc) errx(1, "free");
+ rc = ga->free(ga, test);
+ if (rc) errx(1, "free: %s", strerror(rc));
}