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
|
use "die"
use "extremum"
use "memops"
use "syswrap"
use "threadhooks"
use "types"
use "units"
use "bytealloc"
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)
p = bytealloc(sz)
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)
p = zbytealloc(sz)
p = inithdr(p, sz)
-> (p : @a#)[0:len]
}
const inithdr = {p, sz
var phdr, prest
phdr = (p : slheader#)
phdr.cap = allocsz(sz) - align(sizeof(slheader), Align)
phdr.magic = (0xdeadbeefbadf00d : size)
prest = (p : size) + align(sizeof(slheader), Align)
-> (prest : byte#)
}
const checkhdr = {p
var phdr, addr
addr = (p : size)
addr -= align(sizeof(slheader), Align)
phdr = (addr : slheader#)
iassert(phdr.magic == (0xdeadbeefbadf00d : size), "corrupt memory\n")
}
/* Frees a slice */
generic slfree = {sl
var head
if (sl : byte#) == Zsliceptr
-> void
;;
checkhdr((sl : byte#))
head = ((sl : byte#) : size)
head -= align(sizeof(slheader), Align)
bytefree((head : byte#), slcap((sl : byte#)))
}
/* Grows a slice */
generic slgrow = {sl : @a[:]#, len
var cap
var new
var n
/* if the slice doesn't need a bigger bucket, we don't need to realloc. */
cap = 0
if (sl# : byte#) != Zsliceptr
cap = slcap((sl# : byte#))
;;
if cap >= allocsz(len*sizeof(@a))
sl# = (sl# : @a#)[:len]
-> sl#
;;
/* grow in factors of 1.5 */
cap = max(Align, cap)
while cap < len
cap += (cap >> 1)
;;
new = slalloc(cap)
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
}
|