2 * AcessOS Microkernel Version
12 #define HEAP_BASE 0xE0800000
13 #define HEAP_MAX 0xF0000000 // 120MiB, Plenty
14 #define HEAP_INIT_SIZE 0x8000 // 32 KiB
15 #define BLOCK_SIZE (sizeof(void*)) // 8 Machine Words
16 #define COMPACT_HEAP 0 // Use 4 byte header?
19 #define MAGIC_FOOT 0x2ACE5505
20 #define MAGIC_FREE 0xACE55000
21 #define MAGIC_USED 0x862B0505 // MAGIC_FOOT ^ MAGIC_FREE
25 void *Heap_Extend(int Bytes);
26 void *Heap_Merge(tHeapHead *Head);
27 void *malloc(size_t Bytes);
39 gHeapStart = (void*)MM_KHEAP_BASE;
40 gHeapEnd = (void*)MM_KHEAP_BASE;
41 Heap_Extend(HEAP_INIT_SIZE);
45 * \fn void *Heap_Extend(int Bytes)
46 * \brief Extend the size of the heap
48 void *Heap_Extend(int Bytes)
51 tHeapHead *head = gHeapEnd;
55 if( (Uint)gHeapEnd == MM_KHEAP_MAX )
59 if( (Uint)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
60 Bytes = MM_KHEAP_MAX - (Uint)gHeapEnd;
64 // Heap expands in pages
65 for(i=0;i<(Bytes+0xFFF)>>12;i++)
66 MM_Allocate( (Uint)gHeapEnd+(i<<12) );
72 head->Size = (Bytes+0xFFF)&~0xFFF;
73 head->Magic = MAGIC_FREE;
74 foot = (void*)( (Uint)gHeapEnd - sizeof(tHeapFoot) );
76 foot->Magic = MAGIC_FOOT;
78 //Log(" Heap_Extend: head = %p", head);
79 return Heap_Merge(head); // Merge with previous block
83 * \fn void *Heap_Merge(tHeapHead *Head)
84 * \brief Merges two ajacent heap blocks
86 void *Heap_Merge(tHeapHead *Head)
92 //Log("Heap_Merge: (Head=%p)", Head);
94 thisFoot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
95 if((Uint)thisFoot > (Uint)gHeapEnd) return NULL;
98 foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
99 if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot > HEAP_BASE)
100 && foot->Head->Magic == MAGIC_FREE) {
101 foot->Head->Size += Head->Size; // Increase size
102 thisFoot->Head = foot->Head; // Change backlink
103 Head->Magic = 0; // Clear old head
105 Head = foot->Head; // Save new head address
106 foot->Head = NULL; // Clear central footer
110 // Merge Right (Upwards)
111 head = (void*)( (Uint)Head + Head->Size );
112 if((Uint)head < (Uint)gHeapEnd && head->Magic == MAGIC_FREE)
114 Head->Size += head->Size;
115 foot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
116 foot->Head = Head; // Update Backlink
117 thisFoot->Head = NULL; // Clear old footer
119 head->Size = 0; // Clear old header
123 // Return new address
128 * \fn void *malloc(size_t Bytes)
129 * \brief Allocate memory from the heap
131 void *malloc(size_t Bytes)
133 tHeapHead *head, *newhead;
134 tHeapFoot *foot, *newfoot;
135 tHeapHead *best = NULL;
136 Uint bestSize = 0; // Speed hack
139 Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
142 LOCK(&giHeapSpinlock);
145 for( head = gHeapStart;
146 (Uint)head < (Uint)gHeapEnd;
147 head = (void*)((Uint)head + head->Size)
151 if( head->Size & (BLOCK_SIZE-1) ) {
153 Warning("Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
159 // Check if allocated
160 if(head->Magic == MAGIC_USED) continue;
162 if(head->Magic != MAGIC_FREE) {
164 Warning("Magic of heap address %p is invalid (0x%x)", head, head->Magic);
167 RELEASE(&giHeapSpinlock); // Release spinlock
172 if(head->Size < Bytes) continue;
175 if(head->Size == Bytes) {
176 head->Magic = MAGIC_USED;
177 RELEASE(&giHeapSpinlock); // Release spinlock
184 bestSize = head->Size;
187 // or check if the block is the best size
188 if(bestSize > head->Size) {
190 bestSize = head->Size;
195 // If no block large enough is found, create one
198 best = Heap_Extend( Bytes );
201 RELEASE(&giHeapSpinlock); // Release spinlock
205 if(best->Size == Bytes) {
206 RELEASE(&giHeapSpinlock); // Release spinlock
212 newhead = (void*)( (Uint)best + Bytes );
213 newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
214 foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
216 newfoot->Head = best; // Create new footer
217 newfoot->Magic = MAGIC_FOOT;
218 newhead->Size = best->Size - Bytes; // Create new header
219 newhead->Magic = MAGIC_FREE;
220 foot->Head = newhead; // Update backlink in old footer
221 best->Size = Bytes; // Update size in old header
222 best->Magic = MAGIC_USED; // Mark block as used
224 RELEASE(&giHeapSpinlock); // Release spinlock
229 * \fn void free(void *Ptr)
230 * \brief Free an allocated memory block
237 LOG("Ptr = %p", Ptr);
238 LOG("Returns to %p", __builtin_return_address(0));
241 if( (Uint)Ptr & (sizeof(Uint)-1) ) {
242 Warning("free - Passed a non-aligned address (%p)\n", Ptr);
247 if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
249 Warning("free - Passed a non-heap address (%p)\n", Ptr);
253 // Check memory block - Header
254 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
255 if(head->Magic == MAGIC_FREE) {
256 Warning("free - Passed a freed block (%p)\n", head);
259 if(head->Magic != MAGIC_USED) {
260 Warning("free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic);
264 // Check memory block - Footer
265 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
266 if(foot->Head != head) {
267 Warning("free - Footer backlink is incorrect (%p, 0x%x)\n", head, foot->Head);
270 if(foot->Magic != MAGIC_FOOT) {
271 Warning("free - Footer magic is invalid (%p, 0x%x)\n", head, foot->Magic);
276 LOCK( &giHeapSpinlock );
279 head->Magic = MAGIC_FREE;
284 RELEASE( &giHeapSpinlock );
289 void *realloc(void *__ptr, size_t __size)
291 tHeapHead *head = (void*)( (Uint)__ptr-8 );
294 Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1);
296 // Check for reallocating NULL
297 if(__ptr == NULL) return malloc(__size);
299 // Check if resize is needed
300 if(newSize <= head->Size) return __ptr;
302 // Check if next block is free
303 nextHead = (void*)( (Uint)head + head->Size );
305 // Extend into next block
306 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
308 Uint size = nextHead->Size + head->Size;
309 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
311 if(size == newSize) {
312 head->Size = newSize;
318 // Create a new heap block
319 nextHead = (void*)( (Uint)head + newSize );
320 nextHead->Size = size - newSize;
321 nextHead->Magic = MAGIC_FREE;
322 foot->Head = nextHead; // Edit 2nd footer
323 head->Size = newSize; // Edit first header
325 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
327 foot->Magic = MAGIC_FOOT;
332 foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
333 nextHead = foot->Head;
334 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
336 Uint size = nextHead->Size + head->Size;
341 // Set 1st (new/lower) header
342 nextHead->Magic = MAGIC_USED;
343 nextHead->Size = newSize;
344 // Get 2nd (old) footer
345 foot = (void*)( (Uint)nextHead + newSize );
346 foot->Head = nextHead;
347 // Save old data size
348 oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
352 memcpy(nextHead->Data, __ptr, oldDataSize);
366 while( (Uint)head < (Uint)gHeapEnd )
368 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
369 Log("%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
370 Log("%p 0x%lx", foot->Head, foot->Magic);
373 // Sanity Check Header
374 if(head->Size == 0) {
375 Log("HALTED - Size is zero");
378 if(head->Size & (BLOCK_SIZE-1)) {
379 Log("HALTED - Size is malaligned");
382 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
383 Log("HALTED - Head Magic is Bad");
388 if(foot->Magic != MAGIC_FOOT) {
389 Log("HALTED - Foot Magic is Bad");
392 if(head != foot->Head) {
393 Log("HALTED - Footer backlink is invalid");
397 // All OK? Go to next
398 head = foot->NextHead;