Many changes, Mostly working on improving the BootConf script engine.
[tpg/acess2.git] / Kernel / heap.c
1 /*
2  * AcessOS Microkernel Version
3  * heap.c
4  */
5 #include <acess.h>
6 #include <mm_virt.h>
7 #include <heap.h>
8
9 #define WARNINGS        1
10 #define DEBUG_TRACE     0
11
12 // === CONSTANTS ===
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?
18 #define FIRST_FIT       0
19
20 #define MAGIC_FOOT      0x2ACE5505
21 #define MAGIC_FREE      0xACE55000
22 #define MAGIC_USED      0x862B0505      // MAGIC_FOOT ^ MAGIC_FREE
23
24 // === PROTOTYPES ===
25 void    Heap_Install();
26 void    *Heap_Extend(int Bytes);
27 void    *Heap_Merge(tHeapHead *Head);
28 void    *malloc(size_t Bytes);
29 void    free(void *Ptr);
30 void    Heap_Dump();
31
32 // === GLOBALS ===
33  int    glHeap;
34 void    *gHeapStart;
35 void    *gHeapEnd;
36
37 // === CODE ===
38 void Heap_Install()
39 {
40         gHeapStart      = (void*)MM_KHEAP_BASE;
41         gHeapEnd        = (void*)MM_KHEAP_BASE;
42         Heap_Extend(HEAP_INIT_SIZE);
43 }
44
45 /**
46  * \fn void *Heap_Extend(int Bytes)
47  * \brief Extend the size of the heap
48  */
49 void *Heap_Extend(int Bytes)
50 {
51         Uint    i;
52         tHeapHead       *head = gHeapEnd;
53         tHeapFoot       *foot;
54         
55         // Bounds Check
56         if( (Uint)gHeapEnd == MM_KHEAP_MAX )
57                 return NULL;
58         
59         // Bounds Check
60         if( (Uint)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
61                 Bytes = MM_KHEAP_MAX - (Uint)gHeapEnd;
62                 return NULL;
63         }
64         
65         // Heap expands in pages
66         for(i=0;i<(Bytes+0xFFF)>>12;i++)
67                 MM_Allocate( (Uint)gHeapEnd+(i<<12) );
68         
69         // Increas heap end
70         gHeapEnd += i << 12;
71         
72         // Create Block
73         head->Size = (Bytes+0xFFF)&~0xFFF;
74         head->Magic = MAGIC_FREE;
75         foot = (void*)( (Uint)gHeapEnd - sizeof(tHeapFoot) );
76         foot->Head = head;
77         foot->Magic = MAGIC_FOOT;
78         
79         //Log(" Heap_Extend: head = %p", head);
80         return Heap_Merge(head);        // Merge with previous block
81 }
82
83 /**
84  * \fn void *Heap_Merge(tHeapHead *Head)
85  * \brief Merges two ajacent heap blocks
86  */
87 void *Heap_Merge(tHeapHead *Head)
88 {
89         tHeapFoot       *foot;
90         tHeapFoot       *thisFoot;
91         tHeapHead       *head;
92         
93         //Log("Heap_Merge: (Head=%p)", Head);
94         
95         thisFoot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
96         if((Uint)thisFoot > (Uint)gHeapEnd)     return NULL;
97         
98         // Merge Left (Down)
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
105                 Head->Size = 0;
106                 Head = foot->Head;      // Save new head address
107                 foot->Head = NULL;      // Clear central footer
108                 foot->Magic = 0;
109         }
110         
111         // Merge Right (Upwards)
112         head = (void*)( (Uint)Head + Head->Size );
113         if((Uint)head < (Uint)gHeapEnd && head->Magic == MAGIC_FREE)
114         {
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
119                 thisFoot->Magic = 0;
120                 head->Size = 0;         // Clear old header
121                 head->Magic = 0;
122         }
123         
124         // Return new address
125         return Head;
126 }
127
128 /**
129  * \fn void *malloc(size_t Bytes)
130  * \brief Allocate memory from the heap
131  */
132 void *malloc(size_t Bytes)
133 {
134         tHeapHead       *head, *newhead;
135         tHeapFoot       *foot, *newfoot;
136         tHeapHead       *best = NULL;
137         Uint    bestSize = 0;   // Speed hack
138         
139         // Get required size
140         Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
141         
142         // Lock Heap
143         LOCK(&glHeap);
144         
145         // Traverse Heap
146         for( head = gHeapStart;
147                 (Uint)head < (Uint)gHeapEnd;
148                 head = (void*)((Uint)head + head->Size)
149                 )
150         {
151                 // Alignment Check
152                 if( head->Size & (BLOCK_SIZE-1) ) {
153                         #if WARNINGS
154                         Warning("Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
155                         Heap_Dump();
156                         #endif
157                         RELEASE(&glHeap);
158                         return NULL;
159                 }
160                 
161                 // Check if allocated
162                 if(head->Magic == MAGIC_USED)   continue;
163                 // Error check
164                 if(head->Magic != MAGIC_FREE)   {
165                         #if WARNINGS
166                         Warning("Magic of heap address %p is invalid (0x%x)", head, head->Magic);
167                         Heap_Dump();
168                         #endif
169                         RELEASE(&glHeap);       // Release spinlock
170                         return NULL;
171                 }
172                 
173                 // Size check
174                 if(head->Size < Bytes)  continue;
175                 
176                 // Perfect fit
177                 if(head->Size == Bytes) {
178                         head->Magic = MAGIC_USED;
179                         RELEASE(&glHeap);       // Release spinlock
180                         #if DEBUG_TRACE
181                         LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0));
182                         #endif
183                         return best->Data;
184                 }
185                 
186                 // Break out of loop
187                 #if FIRST_FIT
188                 best = head;
189                 bestSize = head->Size;
190                 break;
191                 #else
192                 // or check if the block is the best size
193                 if(bestSize > head->Size) {
194                         best = head;
195                         bestSize = head->Size;
196                 }
197                 #endif
198         }
199         
200         // If no block large enough is found, create one
201         if(!best)
202         {
203                 best = Heap_Extend( Bytes );
204                 // Check for errors
205                 if(!best) {
206                         RELEASE(&glHeap);       // Release spinlock
207                         return NULL;
208                 }
209                 // Check size
210                 if(best->Size == Bytes) {
211                         RELEASE(&glHeap);       // Release spinlock
212                         #if DEBUG_TRACE
213                         LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0));
214                         #endif
215                         return best->Data;
216                 }
217         }
218         
219         // Split Block
220         newhead = (void*)( (Uint)best + Bytes );
221         newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
222         foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
223         
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
231         
232         RELEASE(&glHeap);       // Release spinlock
233         #if DEBUG_TRACE
234         LOG("RETURN %p, to %p", best->Data, __builtin_return_address(0));
235         #endif
236         return best->Data;
237 }
238
239 /**
240  * \fn void free(void *Ptr)
241  * \brief Free an allocated memory block
242  */
243 void free(void *Ptr)
244 {
245         tHeapHead       *head;
246         tHeapFoot       *foot;
247         
248         #if DEBUG_TRACE
249         LOG("Ptr = %p", Ptr);
250         LOG("Returns to %p", __builtin_return_address(0));
251         #endif
252         
253         // Alignment Check
254         if( (Uint)Ptr & (sizeof(Uint)-1) ) {
255                 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
256                 return;
257         }
258         
259         // Sanity check
260         if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
261         {
262                 Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n",
263                         gHeapStart, Ptr, gHeapEnd);
264                 return;
265         }
266         
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));
271                 return;
272         }
273         if(head->Magic != MAGIC_USED) {
274                 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)\n", head, head->Magic);
275                 return;
276         }
277         
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);
282                 return;
283         }
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);
286                 return;
287         }
288         
289         // Lock
290         LOCK( &glHeap );
291         
292         // Mark as free
293         head->Magic = MAGIC_FREE;
294         // Merge blocks
295         Heap_Merge( head );
296         
297         // Release
298         RELEASE( &glHeap );
299 }
300
301 /**
302  */
303 void *realloc(void *__ptr, size_t __size)
304 {
305         tHeapHead       *head = (void*)( (Uint)__ptr-8 );
306         tHeapHead       *nextHead;
307         tHeapFoot       *foot;
308         Uint    newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+BLOCK_SIZE-1)&~(BLOCK_SIZE-1);
309         
310         // Check for reallocating NULL
311         if(__ptr == NULL)       return malloc(__size);
312         
313         // Check if resize is needed
314         if(newSize <= head->Size)       return __ptr;
315         
316         // Check if next block is free
317         nextHead = (void*)( (Uint)head + head->Size );
318         
319         // Extend into next block
320         if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
321         {
322                 Uint    size = nextHead->Size + head->Size;
323                 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
324                 // Exact Fit
325                 if(size == newSize) {
326                         head->Size = newSize;
327                         foot->Head = head;
328                         nextHead->Magic = 0;
329                         nextHead->Size = 0;
330                         return __ptr;
331                 }
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
338                 // Create new footer
339                 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
340                 foot->Head = head;
341                 foot->Magic = MAGIC_FOOT;
342                 return __ptr;
343         }
344         
345         // Extend downwards?
346         foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
347         nextHead = foot->Head;
348         if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
349         {
350                 Uint    size = nextHead->Size + head->Size;
351                 // Exact fit
352                 if(size == newSize)
353                 {
354                         Uint    oldDataSize;
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);
363                         // Clear old header
364                         head->Size = 0;
365                         head->Magic = 0;
366                         memcpy(nextHead->Data, __ptr, oldDataSize);
367                 }
368         }
369         
370         return NULL;
371 }
372
373 /**
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
378  */
379 void *calloc(size_t num, size_t size)
380 {
381         void    *ret = malloc(num*size);
382         if(ret == NULL) return NULL;
383         
384         memset( ret, 0, num*size );
385         
386         return ret;
387 }
388
389 /**
390  * \fn int IsHeap(void *Ptr)
391  * \brief Checks if an address is a heap pointer
392  */
393 int IsHeap(void *Ptr)
394 {
395         tHeapHead       *head;
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;
399         
400         head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
401         if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
402                 return 0;
403         
404         return 1;
405 }
406
407 #if WARNINGS
408 void Heap_Dump()
409 {
410         tHeapHead       *head;
411         tHeapFoot       *foot;
412         
413         head = gHeapStart;
414         while( (Uint)head < (Uint)gHeapEnd )
415         {               
416                 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
417                 Log("%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
418                 Log("%p 0x%lx", foot->Head, foot->Magic);
419                 Log("");
420                 
421                 // Sanity Check Header
422                 if(head->Size == 0) {
423                         Log("HALTED - Size is zero");
424                         break;
425                 }
426                 if(head->Size & (BLOCK_SIZE-1)) {
427                         Log("HALTED - Size is malaligned");
428                         break;
429                 }
430                 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
431                         Log("HALTED - Head Magic is Bad");
432                         break;
433                 }
434                 
435                 // Check footer
436                 if(foot->Magic != MAGIC_FOOT) {
437                         Log("HALTED - Foot Magic is Bad");
438                         break;
439                 }
440                 if(head != foot->Head) {
441                         Log("HALTED - Footer backlink is invalid");
442                         break;
443                 }
444                 
445                 // All OK? Go to next
446                 head = foot->NextHead;
447         }
448 }
449 #endif
450
451 // === EXPORTS ===
452 EXPORT(malloc);
453 EXPORT(realloc);
454 EXPORT(free);

UCC git Repository :: git.ucc.asn.au