about summary refs log tree commit diff
path: root/std/mem.pry
blob: 52de4d5f102c5c2eaa2ff69a323a03d709e9b551 (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
27
28
29
30
31
32
extern calloc = (i64, i64) => *void;
extern realloc = (*void, i64) => *void;
extern free = (*void) => void;

import "!stdlib.pry";

let arena = struct {
	buf: *void,
	offset: i64,
	size: i64,
};

let arena_init = (size: i64) => *arena {
	let a = cast(*arena, calloc(1, sizeof(arena)));
	(*a).buf = calloc(1, size);
	(*a).offset = 0;
	(*a).size = size;
	return a;
};

let arena_free = (a: *arena) => void {
	free((*a).buf);
	free(cast(*void, a));
	return;
};

let arena_alloc = (a: *arena, size: i64) => *void {
	assert((*a).offset + size < (*a).size);
	let old_offset = (*a).offset;
	(*a).offset = (*a).offset + size;
	return cast(*void, cast(*i8, (*a).buf) + cast(*i8, old_offset));
};