-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnewvec.b
More file actions
287 lines (256 loc) · 6.89 KB
/
newvec.b
File metadata and controls
287 lines (256 loc) · 6.89 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import "io"
// each newvec allocation also dedicates four bytes:
// -3: size -- (even number = unoccupied chunk, odd number = occupied chunk)
// -2: address of previous memory chunk
// -1: address of next memory chunk
// 0: (this is the address returned to user)
// 1:
// :
// n-1:
// n: size -- (even number = unoccupied chunk, odd number = occupied chunk)
// the heap starts with an array 42 words long (0-41)
// this contains 14 entries, each a pointer (or null)
// to a doubly linked list of initally unallocated chunks
// of a delimited size following the pattern:
// 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 32768
// in addition to each of the 14 entries in the array of pointers to lists
// there are two additonal entries which follow right after the ptr_to_list
// 0: ptr_to_list
// 1: ptr_to_free_list
// 2: items_in_free_list
// when new_vec is called it first checks the corresponding free list.
// if adequately sized chunk in the free list is found then
// + new_vec returns the adress of that chunk
// + and moves that chunk from the free list to the occupied list.
// else if the free list is empty or does not contain a chunk of adequate size then
// + new_vec allocates a chunk of the requested size
// + and adds it to the front of the list
// when free_vec is called the allocated chunk is moved to the appropriate free list
// to be recycled when new_vec asks for a chunk <= the size of that free chunk
// the heap starts off as one page (2048 words / 8192 bytes) and may grow to up to
// 32 pages as needed (ihandle_pagefault takes care of growing the heap)
// max sizeof new chunk is 16 pages / 131 MB
export{ new_vec, free_vec, init_heap }
manifest
{
chunk_size = -3,
previous = -2,
next = -1
}
manifest
{
sizeof_meta_data = 3,
sizeof_all_meta_data = 4,
sizeof_array_of_lists = 42
}
manifest
{
max_size = 32768 //words
}
static { heap, next_free_address }
let error(error_code) be
{
out("error %d, error_code\n");
if error_code = 1 then
{
out("newvec: requested too large of a chunk. max size = 32768 words\n")
}
resultis 0
}
let print_memory(address) be
{
for i = 0 to 75 do
{
out("heap ! %d = ", i);
out("0x%x: %d / 0x%x\n", @(address ! i), address ! i, address ! i)
}
}
//all chunks returned to user are even number of words
let set_chunk_size(size) be
{
if size rem 2 = 1 then size +:= 1;
if size > max_size then resultis error(1);
resultis size
}
//odd size signals chunk is occupied (w/ size-1 words)
let chunk_is_occupied(address) be
{
if (address ! chunk_size) rem 2 = 1 then resultis true;
resultis false
}
let get_index_in_array_of_lists(size) be
{
let index = 0, current_power_of_two = 2;
while current_power_of_two <= 8192 do
{
if size <= current_power_of_two then
{
resultis index;
}
index +:= 3;
current_power_of_two *:= 2
}
resultis index
}
let address_is_ptr_to_list(addr) be
{
if addr >= heap /\ addr < (heap + sizeof_array_of_lists) then
{
resultis true;
}
resultis false
}
let new_chunk(size, index) be
{
let prev_ptr = @(heap ! index);
let next_ptr = heap ! index;
let addr = next_free_address;
next_free_address +:= (size + sizeof_all_meta_data);
if next_ptr <> nil then
{
next_ptr ! previous := addr;
}
addr ! chunk_size := size + 1;
addr ! previous := prev_ptr;
addr ! next := next_ptr;
addr ! size := size + 1;
resultis addr
}
let move_free_chunk_to_list(addr, index) be
{
let prev_ptr = @(heap ! index);
let next_ptr = heap ! index;
let size = addr ! chunk_size;
test (addr ! previous) = @(heap ! (index + 1)) then
{
heap ! (index + 1) := addr ! next
}
else
{
(addr ! previous) ! next := addr ! next;
}
if addr ! next <> nil then
{
(addr ! next) ! previous := addr ! previous;
}
if next_ptr <> nil then
{
next_ptr ! previous := addr;
}
addr ! chunk_size := size + 1;
addr ! previous := prev_ptr;
addr ! next := next_ptr;
addr ! size := size + 1;
heap ! (index + 2) -:= 1;
resultis addr
}
let recycle_free_chunk(size, index) be
{
let prev_ptr = @(heap ! index);
let next_ptr = heap ! index;
let addr = heap ! (index + 1);
until addr = nil do
{
if addr ! chunk_size >= size then
{
resultis move_free_chunk_to_list(addr, index);
}
addr := addr ! next
}
resultis new_chunk(size, prev_ptr, next_ptr)
}
//add to front of list (either new_chunk or recycle_free_chunk)
let new_vec(size) be
{
let index, free_list_ptr, items_in_free_list;
let next_ptr, prev_ptr;
size := set_chunk_size(size);
index := get_index_in_array_of_lists(size);
items_in_free_list := heap ! (index + 2);
test items_in_free_list = 0 then
{
heap ! index := new_chunk(size, index)
}
else
{
heap ! index := recycle_free_chunk(size, index);
}
resultis (heap ! index)
}
let move_chunk_to_free_list(addr, size) be
{
let index = get_index_in_array_of_lists(size);
let prev_ptr = @(heap ! (index + 1));
let next_ptr = heap ! (index + 1);
test (addr ! previous) = @(heap ! index) then
{
heap ! index := addr ! next
}
else
{
(addr ! previous) ! next := addr ! next;
}
if addr ! next <> nil then
{
(addr ! next) ! previous := addr ! previous;
}
if next_ptr <> nil then
{
next_ptr ! previous := addr;
}
addr ! previous := prev_ptr;
addr ! next := next_ptr;
heap ! (index + 1) := addr;
heap ! (index + 2) +:= 1 }
let free_vec(addr) be
{
let size = addr ! chunk_size;
//out("\nfree 0x%x, IS > 0x%x AND < 0x%x\n", addr, (heap + 45), next_free_address);
if addr < (heap + 45) \/ addr > next_free_address then
{
out("Called freevec on non-heap memory\n");
return
}
if ~chunk_is_occupied(addr) then
{
out("memory is already free\n");
return
}
size -:= 1;
addr ! chunk_size := size;
addr ! size := size;
move_chunk_to_free_list(addr, size)
}
let print_heap() be
{
for i = 0 to 100 do
{
out("0x%x || ", @(heap ! i));
out("%d: ", i);
test heap ! i < 10000 then out("%d", heap ! i)
else out("0x%x", heap ! i);
out("\n")
}
}
let init_heap(heap_address) be
{ heap := heap_address;
next_free_address := heap + sizeof_array_of_lists + sizeof_meta_data }
let test_run() be
{ let x, y, z;
x := newvec(10);
y := newvec(12);
z := newvec(15);
out("x = 0x%x\n\n", x);
out("y = 0x%x\n\n", y);
out("z = 0x%x\n\n", z);
print_heap();
out("\n\n\n");
freevec(x);
freevec(y);
freevec(z);
print_heap();
out("\n\n\n");
y := newvec(12);
z := newvec(11);
print_heap();
}