aboutsummaryrefslogtreecommitdiff
path: root/lib/alloc/alloc_heap.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/alloc/alloc_heap.c')
-rw-r--r--lib/alloc/alloc_heap.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/lib/alloc/alloc_heap.c b/lib/alloc/alloc_heap.c
index 72cd7ad..46fbdc9 100644
--- a/lib/alloc/alloc_heap.c
+++ b/lib/alloc/alloc_heap.c
@@ -1,12 +1,26 @@
+#include <stddef.h>
+#include <setjmp.h>
#include <stdlib.h>
#include "alloc.h"
+#include "errors.h"
void *
-alloc_heap(void *, void *ptr, size_t, size_t new, size_t)
+alloc_heap(void *raw_ctx, void *ptr, size_t, size_t new, size_t)
{
- if (new > 0)
- return realloc(ptr, new);
- free(ptr);
- return nullptr;
+ if (new == 0) {
+ free(ptr);
+ return nullptr;
+ }
+
+ void *p = realloc(ptr, new);
+ if (p != nullptr)
+ return p;
+
+ struct heap_ctx *ctx = raw_ctx;
+ if (ctx == nullptr || ctx->jmp == nullptr)
+ err("realloc:");
+
+ longjmp(*ctx->jmp, 1);
+ unreachable();
}