2 * AcessOS Microkernel Version
13 #define HEAP_BASE 0xE0800000
14 #define HEAP_MAX 0xF0000000 // 120MiB, Plenty
15 #define HEAP_INIT_SIZE 0x8000 // 32 KiB
16 #define BLOCK_SIZE (sizeof(void*)) // 8 Machine Words
17 #define COMPACT_HEAP 0 // Use 4 byte header?
20 #define MAGIC_FOOT 0x2ACE5505
21 #define MAGIC_FREE 0xACE55000
22 #define MAGIC_USED 0x862B0505 // MAGIC_FOOT ^ MAGIC_FREE
26 void *Heap_Extend(int Bytes);
27 void *Heap_Merge(tHeapHead *Head);
28 void *malloc(size_t Bytes);
40 gHeapStart = (void*)MM_KHEAP_BASE;
41 gHeapEnd = (void*)MM_KHEAP_BASE;
42 Heap_Extend(HEAP_INIT_SIZE);
46 * \fn void *Heap_Extend(int Bytes)
47 * \brief Extend the size of the heap
49 void *Heap_Extend(int Bytes)
52 tHeapHead *head = gHeapEnd;
56 if( (Uint)gHeapEnd == MM_KHEAP_MAX )
60 if( (Uint)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
61 Bytes = MM_KHEAP_MAX - (Uint)gHeapEnd;
65 // Heap expands in pages
66 for(i=0;i<(Bytes+0xFFF)>>12;i++)
67 MM_Allocate( (Uint)gHeapEnd+(i<<12) );
73 head->Size = (Bytes+0xFFF)&~0xFFF;
74 head->Magic = MAGIC_FREE;
75 foot = (void*)( (Uint)gHeapEnd - sizeof(tHeapFoot) );
77 foot->Magic = MAGIC_FOOT;
79 //Log(" Heap_Extend: head = %p", head);
80 return Heap_Merge(head); // Merge with previous block
84 * \fn void *Heap_Merge(tHeapHead *Head)
85 * \brief Merges two ajacent heap blocks
87 void *Heap_Merge(tHeapHead *Head)
93 //Log("Heap_Merge: (Head=%p)", Head);
95 thisFoot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
96 if((Uint)thisFoot > (Uint)gHeapEnd) return NULL;
99 foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
100 if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot > HEAP_BASE)
101 && foot->Head->Magic == MAGIC_FREE) {
102 foot->Head->Size += Head->Size; // Increase size
103 thisFoot->Head = foot->Head; // Change backlink
104 Head->Magic = 0; // Clear old head
106 Head = foot->Head; // Save new head address
107 foot->Head = NULL; // Clear central footer
111 // Merge Right (Upwards)
112 head = (void*)( (Uint)Head + Head->Size );
113 if((Uint)head < (Uint)gHeapEnd && head->Magic == MAGIC_FREE)
115 Head->Size += head->Size;
116 foot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
117 foot->Head = Head; // Update Backlink
118 thisFoot->Head = NULL; // Clear old footer
120 head->Size = 0; // Clear old header
124 // Return new address
129 * \fn void *malloc(size_t Bytes)
130 * \brief Allocate memory from the heap
132 void *malloc(size_t Bytes)
134 tHeapHead *head, *newhead;
135 tHeapFoot *foot, *newfoot;
136 tHeapHead *best = NULL;
137 Uint bestSize = 0; // Speed hack
140 Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
146 for( head = gHeapStart;
147 (Uint)head < (Uint)gHeapEnd;
148 head = (void*)((Uint)head + head->Size)
152 if( head->Size & (BLOCK_SIZE-1) ) {
154 Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
161 // Check if allocated
162 if(head->Magic == MAGIC_USED) continue;
164 if(head->Magic != MAGIC_FREE) {
166 Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic);
169 RELEASE(&glHeap); // Release spinlock
174 if(head->Size < Bytes) continue;
177 if(head->Size == Bytes) {
178 head->Magic = MAGIC_USED;
179 RELEASE(&glHeap); // Release spinlock
181 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size, __builtin_return_address(0));
189 bestSize = head->Size;
192 // or check if the block is the best size
193 if(bestSize > head->Size) {
195 bestSize = head->Size;
200 // If no block large enough is found, create one
203 best = Heap_Extend( Bytes );
206 RELEASE(&glHeap); // Release spinlock
210 if(best->Size == Bytes) {
211 RELEASE(&glHeap); // Release spinlock
213 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
220 newhead = (void*)( (Uint)best + Bytes );
221 newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
222 foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
224 newfoot->Head = best; // Create new footer
225 newfoot->Magic = MAGIC_FOOT;
226 newhead->Size = best->Size - Bytes; // Create new header
227 newhead->Magic = MAGIC_FREE;
228 foot->Head = newhead; // Update backlink in old footer
229 best->Size = Bytes; // Update size in old header
230 best->Magic = MAGIC_USED; // Mark block as used
232 RELEASE(&glHeap); // Release spinlock
234 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
240 * \fn void free(void *Ptr)
241 * \brief Free an allocated memory block
249 Log_Log("Heap", "free: Ptr = %p", Ptr);
250 Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0));
254 if( (Uint)Ptr & (sizeof(Uint)-1) ) {
255 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
260 if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
262 Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n",
263 gHeapStart, Ptr, gHeapEnd);
267 // Check memory block - Header
268 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
269 if(head->Magic == MAGIC_FREE) {
270 Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0));
273 if(head->Magic != MAGIC_USED) {
274 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic);
278 // Check memory block - Footer
279 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
280 if(foot->Head != head) {
281 Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)\n", head, foot->Head);
284 if(foot->Magic != MAGIC_FOOT) {
285 Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)\n", head, &foot->Magic, foot->Magic);
293 head->Magic = MAGIC_FREE;
303 void *realloc(void *__ptr, size_t __size)
305 tHeapHead *head = (void*)( (Uint)__ptr-8 );
308 Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1);
310 // Check for reallocating NULL
311 if(__ptr == NULL) return malloc(__size);
313 // Check if resize is needed
314 if(newSize <= head->Size) return __ptr;
316 // Check if next block is free
317 nextHead = (void*)( (Uint)head + head->Size );
319 // Extend into next block
320 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
322 Uint size = nextHead->Size + head->Size;
323 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
325 if(size == newSize) {
326 head->Size = newSize;
332 // Create a new heap block
333 nextHead = (void*)( (Uint)head + newSize );
334 nextHead->Size = size - newSize;
335 nextHead->Magic = MAGIC_FREE;
336 foot->Head = nextHead; // Edit 2nd footer
337 head->Size = newSize; // Edit first header
339 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
341 foot->Magic = MAGIC_FOOT;
346 foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
347 nextHead = foot->Head;
348 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
350 Uint size = nextHead->Size + head->Size;
355 // Set 1st (new/lower) header
356 nextHead->Magic = MAGIC_USED;
357 nextHead->Size = newSize;
358 // Get 2nd (old) footer
359 foot = (void*)( (Uint)nextHead + newSize );
360 foot->Head = nextHead;
361 // Save old data size
362 oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
366 memcpy(nextHead->Data, __ptr, oldDataSize);
374 * \fn void *calloc(size_t num, size_t size)
375 * \brief Allocate and Zero a buffer in memory
376 * \param num Number of elements
377 * \param size Size of each element
379 void *calloc(size_t num, size_t size)
381 void *ret = malloc(num*size);
382 if(ret == NULL) return NULL;
384 memset( ret, 0, num*size );
390 * \fn int IsHeap(void *Ptr)
391 * \brief Checks if an address is a heap pointer
393 int IsHeap(void *Ptr)
396 if((Uint)Ptr < (Uint)gHeapStart) return 0;
397 if((Uint)Ptr > (Uint)gHeapEnd) return 0;
398 if((Uint)Ptr & (sizeof(Uint)-1)) return 0;
400 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
401 if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
414 while( (Uint)head < (Uint)gHeapEnd )
416 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
417 Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
418 Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic);
421 // Sanity Check Header
422 if(head->Size == 0) {
423 Log_Warning("Heap", "HALTED - Size is zero");
426 if(head->Size & (BLOCK_SIZE-1)) {
427 Log_Warning("Heap", "HALTED - Size is malaligned");
430 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
431 Log_Warning("Heap", "HALTED - Head Magic is Bad");
436 if(foot->Magic != MAGIC_FOOT) {
437 Log_Warning("Heap", "HALTED - Foot Magic is Bad");
440 if(head != foot->Head) {
441 Log_Warning("Heap", "HALTED - Footer backlink is invalid");
445 // All OK? Go to next
446 head = foot->NextHead;