about summary refs log tree commit diff
path: root/std/mem.src
blob: 3bb1a0a38b431e488e3ea541065573c852ed67c1 (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
extern calloc = (i64, i64) => *void;
extern realloc = (*void, i64) => *void;
extern free = (*void) => void;

import "!stdlib.src";

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

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

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

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