summaryrefslogtreecommitdiff
path: root/std/mem.toc
blob: e283364b8afe8351ed598eb5cbf72c7b34a243f4 (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
#include "std/base.toc";

// TODO: check for failed calloc
calloc ::= #foreign("calloc", libc) fn(#C size_t, #C size_t) #C &"void";
free ::= #foreign("free", libc) fn(#C &"void");

new ::= fn(t :: Type) &t {
	calloc(1, (sizeof t) as #C size_t)
}

news ::= fn(t :: Type, n : int) []t {
	s: []t;
	s.data = calloc(n as #C size_t, (sizeof t) as #C size_t);
	s.len = n;
	s
}

// TODO(eventually): use type information to make this just one function
del ::= fn(t::=, x: &t) {
	free(x);
}

dels ::= fn(t::=, x: []t) {
	free(x.data);
}