aboutsummaryrefslogtreecommitdiff
path: root/lib/alloc/alloc_heap.c
blob: 46fbdc934e6469ee64e86d55f75f13f7cc958e15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>

#include "alloc.h"
#include "errors.h"

void *
alloc_heap(void *raw_ctx, void *ptr, size_t, size_t new, size_t)
{
	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();
}