summaryrefslogtreecommitdiff
path: root/lib/std/alloc.myr
blob: c3e64f7c6da085e47d94938f2db413e1462d75da (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use "die"
use "extremum"
use "memops"
use "syswrap"
use "threadhooks"
use "types"
use "units"
use "bytealloc"
use "backtrace"
use "memops"

/*
The allocator implementation here is based on Bonwick's slab allocator.

For small allocations (up to Bktmax), it works by requesting large,
power of two aligned chunks from the operating system, and breaking
them into a linked list of equal sized chunks. Allocations are then
satisfied by taking the head of the list of chunks. Empty slabs
are removed from the freelist.

The data structure looks something like this:
   Bkts:
	[16  byte] -> [slab hdr | chunk -> chunk -> chunk] -> [slab hdr | chunk -> chunk -> chunk]
	[32  byte] -> Zslab
	[64  byte] -> [slab hdr | chunk -> chunk]
	...
	[32k byte] -> ...

Large allocations are simply satisfied by mmap().

*/

pkg std =
	generic alloc		: ( -> @a#)
	generic zalloc		: ( -> @a#)
	generic free		: (v:@a# -> void)

	generic slalloc		: (len : size -> @a[:])
	generic slzalloc	: (len : size -> @a[:])
	generic slgrow		: (sl : @a[:]#, len : size -> @a[:])
	generic slzgrow		: (sl : @a[:]#, len : size -> @a[:])
	generic slfree		: (sl : @a[:] -> void)
;;

type slheader = struct
	cap	: size	/* capacity in bytes */
	magic	: size	/* magic check value */
;;

/* Allocates an object of type @a, returning a pointer to it. */
generic alloc = {-> @a#
	-> (bytealloc(sizeof(@a)) : @a#)
}

generic zalloc = {-> @a#
	-> (zbytealloc(sizeof(@a)) : @a#)
}

/* Frees a value of type @a */
generic free = {v:@a# -> void
	bytefree((v : byte#), sizeof(@a))
}

/* allocates a slice of 'len' elements. */
generic slalloc = {len
	var p, sz

	if len == 0
		-> [][:]
	;;
	sz = len*sizeof(@a) + align(sizeof(slheader), Align)
	sz = allocsz(sz)
	p = bytealloc(sz + sizeof(size))
	p = inithdr(p, sz)
	-> (p : @a#)[0:len]
}

generic slzalloc = {len
	var p, sz

	if len == 0
		-> [][:]
	;;
	sz = len*sizeof(@a) + align(sizeof(slheader), Align)
	sz = allocsz(sz)
	p = zbytealloc(sz + sizeof(size))
	p = inithdr(p, sz)
	-> (p : @a#)[0:len]
}

const inithdr = {p, sz
	var phdr, pdat, pend

	phdr = (p : slheader#)
	phdr.cap = sz - align(sizeof(slheader), Align)

	/* add start/end magics */
	phdr.magic = (0xdeadbeefbadf00d : size)
	pdat = (p : size) + align(sizeof(slheader), Align)
	pend = ((pdat : size) + sz - align(sizeof(slheader), Align) : size#)
	pend# = 0xfee1deadfee1dead

	-> (pdat : byte#)
}

const checkhdr = {p
	var phdr, pend, addr

	addr = (p : size)
	addr -= align(sizeof(slheader), Align)

	/* check start/end magics */
	phdr = (addr : slheader#)
	iassert(phdr.magic == (0xdeadbeefbadf00d : size), "corrupt memory\n")
	pend = ((p : size) + phdr.cap : size#)
	iassert(pend# == 0xfee1deadfee1dead, "corrupt memory")
}

/* Frees a slice */
generic slfree	 = {sl
	var head, sz

	if (sl : byte#) == Zslicep
		-> void
	;;

	checkhdr((sl : byte#))
	head = ((sl : byte#) : size)
	head -= align(sizeof(slheader), Align)
	sz = slcap((sl : byte#)) + align(sizeof(slheader), Align)
	bytefree((head : byte#), sz + sizeof(size))
}

/* Grows a slice */
generic slgrow = {sl : @a[:]#, len
	var cap, nel
	var new
	var n

	/* if the slice doesn't need a bigger bucket, we don't need to realloc. */
	cap = 0
	if (sl# : byte#) != Zslicep
		cap = slcap((sl# : byte#))
	;;
	if cap >= len*sizeof(@a)
		/* cast to pointer to work around bounds check */
		sl# = (sl# : @a#)[:len]
		-> sl#
	;;

	/* grow in factors of 1.5 */
	nel = sl#.len
	while nel < len
		nel += (nel >> 2) + 1
	;;

	new = slalloc(nel)
	n = min(len, sl#.len)
	memblit((new : byte#), (sl# : byte#), n * sizeof(@a))
	if sl#.len > 0
		slfree(sl#)
	;;
	sl# = new[:len]
	-> sl#
}

/* Grows a slice, filling new entries with zero bytes */
generic slzgrow = {sl : @a[:]#, len
	var oldlen
	var base

	oldlen = sl#.len
	slgrow(sl, len)
	base = ((sl# : byte#) : intptr)
	if oldlen < len
		memfill((sl#[oldlen:] : byte#), 0, (len - oldlen)*sizeof(@a))
	;;
	-> sl#
}

const slcap = {p
	var phdr

	phdr = ((p : size) - align(sizeof(slheader), Align) : slheader#)
	std.iassert(phdr.magic == (0xdeadbeefbadf00d : size), "corrupt memory\n")
	-> phdr.cap
}