summaryrefslogtreecommitdiff
path: root/mem.c
blob: 2c42758cd14c51e1aba52fa2172b51a3779d9708 (plain)
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
#include "mem.h"

#include <stdio.h>
#include <stdlib.h>

void *M(State *S, void *p, size_t old, size_t new) {
	if (new == 0) {
		free(p);
		return NULL;
	} else {
		void *x = realloc(p, new);
		if (x == NULL) {
			printf("out of memory! aaaaaaa!!!!!\n");
			exit(42);
		}
		return x;
	}
}

Obj *alloc_obj(State *S, size_t sz, ObjTy oty) {
	Obj *o = M(S, NULL, 0, sz);
	o->oty = oty;
	return o;
}


size_t next_pwrof2(size_t x) {
	size_t p = 1;
	while (p < x) p <<= 1;
	return p;
}