2 * AcessOS Microkernel Version
13 #define HEAP_INIT_SIZE 0x8000 // 32 KiB
14 #define BLOCK_SIZE (sizeof(void*))*8 // 8 Machine Words
15 #define COMPACT_HEAP 0 // Use 4 byte header?
18 #define MAGIC_FOOT 0x2ACE5505
19 #define MAGIC_FREE 0xACE55000
20 #define MAGIC_USED 0x862B0505 // MAGIC_FOOT ^ MAGIC_FREE
23 void Heap_Install(void);
24 void *Heap_Extend(int Bytes);
25 void *Heap_Merge(tHeapHead *Head);
26 void *malloc(size_t Bytes);
36 void Heap_Install(void)
38 gHeapStart = (void*)MM_KHEAP_BASE;
39 gHeapEnd = (void*)MM_KHEAP_BASE;
40 Heap_Extend(HEAP_INIT_SIZE);
44 * \fn void *Heap_Extend(int Bytes)
45 * \brief Extend the size of the heap
47 void *Heap_Extend(int Bytes)
50 tHeapHead *head = gHeapEnd;
54 if( (tVAddr)gHeapEnd == MM_KHEAP_MAX )
58 if( (tVAddr)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
59 Bytes = MM_KHEAP_MAX - (tVAddr)gHeapEnd;
63 // Heap expands in pages
64 for(i=0;i<(Bytes+0xFFF)>>12;i++)
65 MM_Allocate( (tVAddr)gHeapEnd+(i<<12) );
71 head->Size = (Bytes+0xFFF)&~0xFFF;
72 head->Magic = MAGIC_FREE;
73 foot = (void*)( (Uint)gHeapEnd - sizeof(tHeapFoot) );
75 foot->Magic = MAGIC_FOOT;
77 return Heap_Merge(head); // Merge with previous block
81 * \fn void *Heap_Merge(tHeapHead *Head)
82 * \brief Merges two ajacent heap blocks
84 void *Heap_Merge(tHeapHead *Head)
90 //Log("Heap_Merge: (Head=%p)", Head);
92 thisFoot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
93 if((Uint)thisFoot > (Uint)gHeapEnd) return NULL;
96 foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
97 if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot > (Uint)gHeapStart)
98 && foot->Head->Magic == MAGIC_FREE) {
99 foot->Head->Size += Head->Size; // Increase size
100 thisFoot->Head = foot->Head; // Change backlink
101 Head->Magic = 0; // Clear old head
103 Head = foot->Head; // Save new head address
104 foot->Head = NULL; // Clear central footer
108 // Merge Right (Upwards)
109 head = (void*)( (Uint)Head + Head->Size );
110 if((Uint)head < (Uint)gHeapEnd && head->Magic == MAGIC_FREE)
112 Head->Size += head->Size;
113 foot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
114 foot->Head = Head; // Update Backlink
115 thisFoot->Head = NULL; // Clear old footer
117 head->Size = 0; // Clear old header
121 // Return new address
126 * \fn void *malloc(size_t Bytes)
127 * \brief Allocate memory from the heap
129 void *malloc(size_t Bytes)
131 tHeapHead *head, *newhead;
132 tHeapFoot *foot, *newfoot;
133 tHeapHead *best = NULL;
134 Uint bestSize = 0; // Speed hack
137 Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
143 for( head = gHeapStart;
144 (Uint)head < (Uint)gHeapEnd;
145 head = (void*)((Uint)head + head->Size)
149 if( head->Size & (BLOCK_SIZE-1) ) {
151 Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
158 // Check if allocated
159 if(head->Magic == MAGIC_USED) continue;
161 if(head->Magic != MAGIC_FREE) {
163 Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic);
166 RELEASE(&glHeap); // Release spinlock
171 if(head->Size < Bytes) continue;
174 if(head->Size == Bytes) {
175 head->Magic = MAGIC_USED;
176 RELEASE(&glHeap); // Release spinlock
178 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size, __builtin_return_address(0));
187 bestSize = head->Size;
190 // or check if the block is the best size
191 if(bestSize > head->Size) {
193 bestSize = head->Size;
198 // If no block large enough is found, create one
201 best = Heap_Extend( Bytes );
204 RELEASE(&glHeap); // Release spinlock
208 if(best->Size == Bytes) {
209 best->Magic = MAGIC_USED; // Mark block as used
211 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
213 RELEASE(&glHeap); // Release spinlock
219 newhead = (void*)( (Uint)best + Bytes );
220 newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
221 foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
223 newfoot->Head = best; // Create new footer
224 newfoot->Magic = MAGIC_FOOT;
225 newhead->Size = best->Size - Bytes; // Create new header
226 newhead->Magic = MAGIC_FREE;
227 foot->Head = newhead; // Update backlink in old footer
228 best->Size = Bytes; // Update size in old header
229 best->Magic = MAGIC_USED; // Mark block as used
231 RELEASE(&glHeap); // Release spinlock
233 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
239 * \fn void free(void *Ptr)
240 * \brief Free an allocated memory block
248 Log_Log("Heap", "free: Ptr = %p", Ptr);
249 Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0));
253 if( (Uint)Ptr & (sizeof(Uint)-1) ) {
254 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
259 if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
261 Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n",
262 gHeapStart, Ptr, gHeapEnd);
266 // Check memory block - Header
267 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
268 if(head->Magic == MAGIC_FREE) {
269 Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0));
272 if(head->Magic != MAGIC_USED) {
273 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic);
277 // Check memory block - Footer
278 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
279 if(foot->Head != head) {
280 Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)\n", head, foot->Head);
283 if(foot->Magic != MAGIC_FOOT) {
284 Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)\n", head, &foot->Magic, foot->Magic);
292 head->Magic = MAGIC_FREE;
302 void *realloc(void *__ptr, size_t __size)
304 tHeapHead *head = (void*)( (Uint)__ptr-8 );
307 Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1);
309 // Check for reallocating NULL
310 if(__ptr == NULL) return malloc(__size);
312 // Check if resize is needed
313 if(newSize <= head->Size) return __ptr;
315 // Check if next block is free
316 nextHead = (void*)( (Uint)head + head->Size );
318 // Extend into next block
319 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
321 Uint size = nextHead->Size + head->Size;
322 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
324 if(size == newSize) {
325 head->Size = newSize;
331 // Create a new heap block
332 nextHead = (void*)( (Uint)head + newSize );
333 nextHead->Size = size - newSize;
334 nextHead->Magic = MAGIC_FREE;
335 foot->Head = nextHead; // Edit 2nd footer
336 head->Size = newSize; // Edit first header
338 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
340 foot->Magic = MAGIC_FOOT;
345 foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
346 nextHead = foot->Head;
347 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
349 Uint size = nextHead->Size + head->Size;
350 // Inexact fit, split things up
354 Warning("[Heap ] TODO: Space efficient realloc when new size is smaller");
361 // Set 1st (new/lower) header
362 nextHead->Magic = MAGIC_USED;
363 nextHead->Size = newSize;
364 // Get 2nd (old) footer
365 foot = (void*)( (Uint)nextHead + newSize );
366 foot->Head = nextHead;
367 // Save old data size
368 oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
373 memcpy(nextHead->Data, __ptr, oldDataSize);
375 return nextHead->Data;
377 // On to the expensive then
381 nextHead = malloc( __size );
387 head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead)
392 return nextHead->Data;
396 * \fn void *calloc(size_t num, size_t size)
397 * \brief Allocate and Zero a buffer in memory
398 * \param num Number of elements
399 * \param size Size of each element
401 void *calloc(size_t num, size_t size)
403 void *ret = malloc(num*size);
404 if(ret == NULL) return NULL;
406 memset( ret, 0, num*size );
412 * \fn int IsHeap(void *Ptr)
413 * \brief Checks if an address is a heap pointer
415 int IsHeap(void *Ptr)
418 if((Uint)Ptr < (Uint)gHeapStart) return 0;
419 if((Uint)Ptr > (Uint)gHeapEnd) return 0;
420 if((Uint)Ptr & (sizeof(Uint)-1)) return 0;
422 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
423 if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
436 while( (Uint)head < (Uint)gHeapEnd )
438 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
439 Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx",
440 head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
441 Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic);
444 // Sanity Check Header
445 if(head->Size == 0) {
446 Log_Warning("Heap", "HALTED - Size is zero");
449 if(head->Size & (BLOCK_SIZE-1)) {
450 Log_Warning("Heap", "HALTED - Size is malaligned");
453 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
454 Log_Warning("Heap", "HALTED - Head Magic is Bad");
459 if(foot->Magic != MAGIC_FOOT) {
460 Log_Warning("Heap", "HALTED - Foot Magic is Bad");
463 if(head != foot->Head) {
464 Log_Warning("Heap", "HALTED - Footer backlink is invalid");
468 // All OK? Go to next
469 head = foot->NextHead;