summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-03-18 18:50:17 +0100
committerLouis Burda <quent.burda@gmail.com>2023-03-18 18:50:17 +0100
commit7bb4bd65d7a11bbf4ad4e085aa2a32d5028585dd (patch)
treeb2f4a4093246fcbd380ab0d4fcef882af097e014 /src
parent22d274eb7a43de119af51cf46a892aa6be6a7d91 (diff)
downloadliballoc-c-7bb4bd65d7a11bbf4ad4e085aa2a32d5028585dd.tar.gz
liballoc-c-7bb4bd65d7a11bbf4ad4e085aa2a32d5028585dd.zip
Add strict variants of stack and heap allocators
Diffstat (limited to 'src')
-rw-r--r--src/allocator.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/allocator.c b/src/allocator.c
index d0f358f..eb07e50 100644
--- a/src/allocator.c
+++ b/src/allocator.c
@@ -7,10 +7,16 @@
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_strict_stack_alloc(void **p, size_t size);
+static int stdlib_strict_stack_realloc(void **p, size_t size);
+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 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 int stdlib_strict_heap_free(void *p);
struct allocator stdlib_stack_allocator = {
.alloc = stdlib_stack_alloc,
@@ -18,12 +24,24 @@ struct allocator stdlib_stack_allocator = {
.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,
.free = stdlib_heap_free
};
+struct allocator stdlib_strict_heap_allocator = {
+ .alloc = stdlib_strict_heap_alloc,
+ .realloc = stdlib_strict_heap_realloc,
+ .free = stdlib_strict_heap_free
+};
+
int
stdlib_stack_alloc(void **p, size_t size)
{
@@ -47,6 +65,27 @@ stdlib_stack_free(void *p)
}
int
+stdlib_strict_stack_alloc(void **p, size_t size)
+{
+ *p = alloca(size);
+ if (!*p) abort();
+
+ return 0;
+}
+
+int
+stdlib_strict_stack_realloc(void **p, size_t size)
+{
+ abort();
+}
+
+int
+stdlib_strict_stack_free(void *p)
+{
+ return 0;
+}
+
+int
stdlib_heap_alloc(void **p, size_t size)
{
*p = malloc(size);
@@ -73,3 +112,32 @@ stdlib_heap_free(void *p)
return 0;
}
+
+int
+stdlib_strict_heap_alloc(void **p, size_t size)
+{
+ *p = malloc(size);
+ if (!*p) abort();
+
+ return 0;
+}
+
+int
+stdlib_strict_heap_realloc(void **p, size_t size)
+{
+ void *np;
+
+ np = realloc(*p, size);
+ if (!np) abort();
+ *p = np;
+
+ return 0;
+}
+
+int
+stdlib_strict_heap_free(void *p)
+{
+ free(p);
+
+ return 0;
+}