-
Notifications
You must be signed in to change notification settings - Fork 0
/
kheap.c
422 lines (379 loc) · 14 KB
/
kheap.c
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// kheap.c
#include "kheap.h"
#include "paging.h"
#include "common.h"
#include "monitor.h"
// end is defined int he linked script.
extern u32int end;
extern page_directory_t *kernel_directory;
u32int placement_address = (u32int)&end;
heap_t *kheap = 0;
void kfree(void *p)
{
free(p, kheap);
}
u32int kmalloc_int(u32int size, int align, u32int *phys)
{
if (kheap != 0)
{
void *addr = alloc(size, (u8int)align, kheap);
if (phys != 0)
{
page_t *page = get_page((u32int)addr, 0, kernel_directory);
*phys = page->frame * 0x1000 + ((u32int)addr & 0xFFF);
}
return (u32int)addr;
}
else
{
u32int tmp;
if (align == 1 && (placement_address % 0x1000) )
{
/* Align the placement address */
placement_address &= 0xFFFFF000;
placement_address += 0x1000;
}
if (phys)
{
*phys = placement_address;
}
tmp = placement_address;
placement_address += size;
return tmp;
}
}
// Vanilla.
u32int kmalloc(u32int size)
{
return kmalloc_int(size, 0, 0);
}
// Page aligned.
u32int kmalloc_a(u32int size)
{
return kmalloc_int(size, 1, 0);
}
// returns physical address.
u32int kmalloc_p(u32int size, u32int *phys)
{
return kmalloc_int(size, 0, phys);
}
// page aligned and returns physical address.
u32int kmalloc_ap(u32int size, u32int *phys)
{
return kmalloc_int(size, 1, phys);
}
static s32int find_smallest_hole(u32int size, u8int page_align, heap_t *heap)
{
// Find the smallest hole that will fit.
u32int i = 0;
while(i < heap->index.size)
{
header_t *header = (header_t *)lookup_ordered_array(i, &heap->index);
// If the user has requested the memory be page-aligned.
if(page_align > 0)
{
// Page-align the starting point of this header.
u32int location = (u32int)header;
s32int offset = 0;
if((location + sizeof(header_t)) % 0x1000)
{
offset = 0x1000 /*page size*/ - (location + sizeof(header_t)) % 0x1000;
}
s32int hole_size = (s32int)header->size - offset;
if(hole_size >= (s32int)size)
{
break;
}
}
else if(header->size >= size)
break;
i++;
}
// Why did the loop exit ?
if(i == heap->index.size)
return - 1; // Nothing founded.
return i;
}
static s8int header_t_less_than(void *a, void *b)
{
return (((header_t *)a)->size < ((header_t*)b)->size) ? 1 : 0;
}
heap_t *create_heap(u32int start, u32int end_addr, u32int max, u8int supervisor, u8int readonly)
{
heap_t *heap = (heap_t*)kmalloc(sizeof(heap_t));
// All our assumptions are made on startAddress and endAddress being page-aligned.
//ASSERT(start%0x1000 == 0);
//ASSERT(end_addr%0x1000 == 0);
// Make sure the start address is page_aligned.
monitor_write_hex(start);
monitor_write("\n");
if(start % 0x1000)
{
start &= 0xFFFFF000;
start += 0x1000;
}
monitor_write_hex(start);
monitor_write("\n");
// Initialise the index.
heap->index = place_ordered_array((void*)start, HEAP_INDEX_SIZE, &header_t_less_than);
// Shift the start address forward to resemble where we can start putting data.
start += sizeof(type_t)*HEAP_INDEX_SIZE;
monitor_write("heap 2\n");
monitor_write("heap 3\n");
if(end_addr % 0x1000)
{
end_addr &= 0xFFFFF000;
end_addr += 0x1000;
}
// Write the start, end and max addresses into the heap structure.
heap->start_address = start;
heap->end_address = end_addr;
heap->max_address = max;
heap->supervisor = supervisor;
heap->readonly = readonly;
monitor_write("heap 4\n");
// We start off with one large hole in the index.
header_t *hole = (header_t *)start;
hole->size = end_addr - start;
hole->magic = HEAP_MAGIC;
hole->is_hole = 1;
insert_ordered_array((void*)hole, &heap->index);
monitor_write("heap 5\n");
return heap;
}
static void expand(u32int new_size, heap_t *heap)
{
// Sanity check.
// ASSERT(new_size > heap->end_address - heap->start_address);
// Get the nearest following page boundary.
if (new_size % 0x1000)
{
new_size &= 0xFFFFF000;
new_size += 0x1000;
}
// Make sure we are not overreaching ourselves.
//ASSERT(heap->start_address + new_size <= heap->max_address);
// This should always be on a page boundary.
u32int old_size = heap->end_address - heap->start_address;
u32int i = old_size;
while (i < new_size)
{
alloc_frame(get_page(heap->start_address + i, 1, kernel_directory),
(heap->supervisor) ? 1 : 0,
(heap->readonly) ? 0 : 1);
i += 0x1000 /* page size */;
}
heap->end_address = heap->start_address+new_size;
}
static u32int contract(u32int new_size, heap_t *heap)
{
// Sanity check.
// ASSERT(new_size < heap->end_address-heap->start_address);
// Get the nearest following page boundary.
if (new_size % 0x1000)
{
new_size &= 0xFFFFF000;
new_size += 0x1000;
}
// Don't contract too far!
if (new_size < HEAP_MIN_SIZE)
new_size = HEAP_MIN_SIZE;
u32int old_size = heap->end_address-heap->start_address;
u32int i = old_size - 0x1000;
while (new_size < i)
{
free_frame(get_page(heap->start_address+i, 0, kernel_directory));
i -= 0x1000;
}
heap->end_address = heap->start_address + new_size;
return new_size;
}
void *alloc(u32int size, u8int page_align, heap_t *heap)
{
// Make sure we take the size of header/footer into account.
u32int new_size = size + sizeof(header_t) + sizeof(footer_t);
// Find the smallest hole that will fit.
s32int iterator = find_smallest_hole(new_size, page_align, heap);
if (iterator == -1) // If we didn't find a suitable hole
{
// Save some previous data.
u32int old_length = heap->end_address - heap->start_address;
u32int old_end_address = heap->end_address;
// We need to allocate some more space.
expand(old_length+new_size, heap);
u32int new_length = heap->end_address-heap->start_address;
// Find the endmost header. (Not endmost in size, but in location).
iterator = 0;
// Vars to hold the index of, and value of, the endmost header found so far.
u32int idx = -1; u32int value = 0x0;
while (iterator < heap->index.size)
{
u32int tmp = (u32int)lookup_ordered_array(iterator, &heap->index);
if (tmp > value)
{
value = tmp;
idx = iterator;
}
iterator++;
}
// If we didn't find ANY headers, we need to add one.
if (idx == -1)
{
header_t *header = (header_t *)old_end_address;
header->magic = HEAP_MAGIC;
header->size = new_length - old_length;
header->is_hole = 1;
footer_t *footer = (footer_t *) (old_end_address + header->size - sizeof(footer_t));
footer->magic = HEAP_MAGIC;
footer->header = header;
insert_ordered_array((void*)header, &heap->index);
}
else
{
// The last header needs adjusting.
header_t *header = lookup_ordered_array(idx, &heap->index);
header->size += new_length - old_length;
// Rewrite the footer.
footer_t *footer = (footer_t *) ( (u32int)header + header->size - sizeof(footer_t) );
footer->header = header;
footer->magic = HEAP_MAGIC;
}
// We now have enough space. Recurse, and call the function again.
return alloc(size, page_align, heap);
}
header_t *orig_hole_header = (header_t *)lookup_ordered_array(iterator, &heap->index);
u32int orig_hole_pos = (u32int)orig_hole_header;
u32int orig_hole_size = orig_hole_header->size;
// Here we work out if we should split the hole we found into two parts.
// Is the original hole size - requested hole size less than the overhead for adding a new hole?
if (orig_hole_size-new_size < sizeof(header_t)+sizeof(footer_t))
{
// Then just increase the requested size to the size of the hole we found.
size += orig_hole_size-new_size;
new_size = orig_hole_size;
}
// If we need to page-align the data, do it now and make a new hole in front of our block.
if (page_align && (orig_hole_pos % 0x1000))
{
u32int new_location = orig_hole_pos + 0x1000 /* page size */ - (orig_hole_pos&0xFFF) - sizeof(header_t);
header_t *hole_header = (header_t *)orig_hole_pos;
hole_header->size = 0x1000 /* page size */ - (orig_hole_pos & 0xFFF) - sizeof(header_t);
hole_header->magic = HEAP_MAGIC;
hole_header->is_hole = 1;
footer_t *hole_footer = (footer_t *) ( (u32int)new_location - sizeof(footer_t) );
hole_footer->magic = HEAP_MAGIC;
hole_footer->header = hole_header;
orig_hole_pos = new_location;
orig_hole_size = orig_hole_size - hole_header->size;
}
else
{
// Else we don't need this hole any more, delete it from the index.
remove_ordered_array(iterator, &heap->index);
}
// Overwrite the original header...
header_t *block_header = (header_t *)orig_hole_pos;
block_header->magic = HEAP_MAGIC;
block_header->is_hole = 0;
block_header->size = new_size;
// ...And the footer
footer_t *block_footer = (footer_t *) (orig_hole_pos + sizeof(header_t) + size);
block_footer->magic = HEAP_MAGIC;
block_footer->header = block_header;
// We may need to write a new hole after the allocated block.
// We do this only if the new hole would have positive size...
if (orig_hole_size - new_size > 0)
{
header_t *hole_header = (header_t *) (orig_hole_pos + sizeof(header_t) + size + sizeof(footer_t));
hole_header->magic = HEAP_MAGIC;
hole_header->is_hole = 1;
hole_header->size = orig_hole_size - new_size;
footer_t *hole_footer = (footer_t *) ( (u32int)hole_header + orig_hole_size - new_size - sizeof(footer_t) );
if ((u32int)hole_footer < heap->end_address)
{
hole_footer->magic = HEAP_MAGIC;
hole_footer->header = hole_header;
}
// Put the new hole in the index;
insert_ordered_array((void*)hole_header, &heap->index);
}
// ...And we're done!
return (void *) ( (u32int)block_header+sizeof(header_t) );
}
void free(void *p, heap_t *heap)
{
// Exit gracefully for null pointers.
if (p == 0)
return;
// Get the header and footer associated with this pointer.
header_t *header = (header_t*) ( (u32int)p - sizeof(header_t) );
footer_t *footer = (footer_t*) ( (u32int)header + header->size - sizeof(footer_t) );
// Sanity checks.
// ASSERT(header->magic == HEAP_MAGIC);
// ASSERT(footer->magic == HEAP_MAGIC);
// Make us a hole.
header->is_hole = 1;
// Do we want to add this header into the 'free holes' index?
char do_add = 1;
// Unify left
// If the thing immediately to the left of us is a footer...
footer_t *test_footer = (footer_t*) ( (u32int)header - sizeof(footer_t) );
if (test_footer->magic == HEAP_MAGIC &&
test_footer->header->is_hole == 1)
{
u32int cache_size = header->size; // Cache our current size.
header = test_footer->header; // Rewrite our header with the new one.
footer->header = header; // Rewrite our footer to point to the new header.
header->size += cache_size; // Change the size.
do_add = 0; // Since this header is already in the index, we don't want to add it again.
}
// Unify right
// If the thing immediately to the right of us is a header...
header_t *test_header = (header_t*) ( (u32int)footer + sizeof(footer_t) );
if (test_header->magic == HEAP_MAGIC &&
test_header->is_hole)
{
header->size += test_header->size; // Increase our size.
test_footer = (footer_t*) ( (u32int)test_header + // Rewrite it's footer to point to our header.
test_header->size - sizeof(footer_t) );
footer = test_footer;
// Find and remove this header from the index.
u32int iterator = 0;
while ( (iterator < heap->index.size) &&
(lookup_ordered_array(iterator, &heap->index) != (void*)test_header) )
iterator++;
// Make sure we actually found the item.
// ASSERT(iterator < heap->index.size);
// Remove it.
remove_ordered_array(iterator, &heap->index);
}
// If the footer location is the end address, we can contract.
if ( (u32int)footer+sizeof(footer_t) == heap->end_address)
{
u32int old_length = heap->end_address-heap->start_address;
u32int new_length = contract( (u32int)header - heap->start_address, heap);
// Check how big we will be after resizing.
if (header->size - (old_length-new_length) > 0)
{
// We will still exist, so resize us.
header->size -= old_length-new_length;
footer = (footer_t*) ( (u32int)header + header->size - sizeof(footer_t) );
footer->magic = HEAP_MAGIC;
footer->header = header;
}
else
{
// We will no longer exist :(. Remove us from the index.
u32int iterator = 0;
while ( (iterator < heap->index.size) &&
(lookup_ordered_array(iterator, &heap->index) != (void*)test_header) )
iterator++;
// If we didn't find ourselves, we have nothing to remove.
if (iterator < heap->index.size)
remove_ordered_array(iterator, &heap->index);
}
}
// If required, add us to the index.
if (do_add == 1)
insert_ordered_array((void*)header, &heap->index);
}