2 * AcessOS Microkernel Version
13 #define HEAP_INIT_SIZE 0x8000 // 32 KiB
14 #define BLOCK_SIZE (sizeof(void*)) // 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
24 void *Heap_Extend(int Bytes);
25 void *Heap_Merge(tHeapHead *Head);
26 void *malloc(size_t Bytes);
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));
186 bestSize = head->Size;
189 // or check if the block is the best size
190 if(bestSize > head->Size) {
192 bestSize = head->Size;
197 // If no block large enough is found, create one
200 best = Heap_Extend( Bytes );
203 RELEASE(&glHeap); // Release spinlock
207 if(best->Size == Bytes) {
208 RELEASE(&glHeap); // Release spinlock
210 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
217 newhead = (void*)( (Uint)best + Bytes );
218 newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
219 foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
221 newfoot->Head = best; // Create new footer
222 newfoot->Magic = MAGIC_FOOT;
223 newhead->Size = best->Size - Bytes; // Create new header
224 newhead->Magic = MAGIC_FREE;
225 foot->Head = newhead; // Update backlink in old footer
226 best->Size = Bytes; // Update size in old header
227 best->Magic = MAGIC_USED; // Mark block as used
229 RELEASE(&glHeap); // Release spinlock
231 Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
237 * \fn void free(void *Ptr)
238 * \brief Free an allocated memory block
246 Log_Log("Heap", "free: Ptr = %p", Ptr);
247 Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0));
251 if( (Uint)Ptr & (sizeof(Uint)-1) ) {
252 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
257 if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
259 Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n",
260 gHeapStart, Ptr, gHeapEnd);
264 // Check memory block - Header
265 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
266 if(head->Magic == MAGIC_FREE) {
267 Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0));
270 if(head->Magic != MAGIC_USED) {
271 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic);
275 // Check memory block - Footer
276 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
277 if(foot->Head != head) {
278 Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)\n", head, foot->Head);
281 if(foot->Magic != MAGIC_FOOT) {
282 Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)\n", head, &foot->Magic, foot->Magic);
290 head->Magic = MAGIC_FREE;
300 void *realloc(void *__ptr, size_t __size)
302 tHeapHead *head = (void*)( (Uint)__ptr-8 );
305 Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1);
307 // Check for reallocating NULL
308 if(__ptr == NULL) return malloc(__size);
310 // Check if resize is needed
311 if(newSize <= head->Size) return __ptr;
313 // Check if next block is free
314 nextHead = (void*)( (Uint)head + head->Size );
316 // Extend into next block
317 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
319 Uint size = nextHead->Size + head->Size;
320 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
322 if(size == newSize) {
323 head->Size = newSize;
329 // Create a new heap block
330 nextHead = (void*)( (Uint)head + newSize );
331 nextHead->Size = size - newSize;
332 nextHead->Magic = MAGIC_FREE;
333 foot->Head = nextHead; // Edit 2nd footer
334 head->Size = newSize; // Edit first header
336 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
338 foot->Magic = MAGIC_FOOT;
343 foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
344 nextHead = foot->Head;
345 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
347 Uint size = nextHead->Size + head->Size;
352 // Set 1st (new/lower) header
353 nextHead->Magic = MAGIC_USED;
354 nextHead->Size = newSize;
355 // Get 2nd (old) footer
356 foot = (void*)( (Uint)nextHead + newSize );
357 foot->Head = nextHead;
358 // Save old data size
359 oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
363 memcpy(nextHead->Data, __ptr, oldDataSize);
371 * \fn void *calloc(size_t num, size_t size)
372 * \brief Allocate and Zero a buffer in memory
373 * \param num Number of elements
374 * \param size Size of each element
376 void *calloc(size_t num, size_t size)
378 void *ret = malloc(num*size);
379 if(ret == NULL) return NULL;
381 memset( ret, 0, num*size );
387 * \fn int IsHeap(void *Ptr)
388 * \brief Checks if an address is a heap pointer
390 int IsHeap(void *Ptr)
393 if((Uint)Ptr < (Uint)gHeapStart) return 0;
394 if((Uint)Ptr > (Uint)gHeapEnd) return 0;
395 if((Uint)Ptr & (sizeof(Uint)-1)) return 0;
397 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
398 if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
411 while( (Uint)head < (Uint)gHeapEnd )
413 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
414 Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
415 Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic);
418 // Sanity Check Header
419 if(head->Size == 0) {
420 Log_Warning("Heap", "HALTED - Size is zero");
423 if(head->Size & (BLOCK_SIZE-1)) {
424 Log_Warning("Heap", "HALTED - Size is malaligned");
427 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
428 Log_Warning("Heap", "HALTED - Head Magic is Bad");
433 if(foot->Magic != MAGIC_FOOT) {
434 Log_Warning("Heap", "HALTED - Foot Magic is Bad");
437 if(head != foot->Head) {
438 Log_Warning("Heap", "HALTED - Footer backlink is invalid");
442 // All OK? Go to next
443 head = foot->NextHead;