07a3aad034a6f082982b5ed7e7cf18f1505ac4a5
[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_int.h>
8
9 #define WARNINGS        1
10 #define DEBUG_TRACE     0
11 #define VERBOSE_DUMP    0
12
13 // === CONSTANTS ===
14 #define HEAP_INIT_SIZE  0x8000  // 32 KiB
15 #define MIN_SIZE        (sizeof(void*))*8       // 8 Machine Words
16 #define POW2_SIZES      0
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 #define MAGIC_FOOT      0x544F4F46      // 'FOOT'
24 #define MAGIC_FREE      0x45455246      // 'FREE'
25 #define MAGIC_USED      0x44455355      // 'USED'
26
27 // === PROTOTYPES ===
28 void    Heap_Install(void);
29 void    *Heap_Extend(int Bytes);
30 void    *Heap_Merge(tHeapHead *Head);
31 //void  *Heap_Allocate(const char *File, int Line, size_t Bytes);
32 //void  *Heap_AllocateZero(const char *File, int Line, size_t Bytes);
33 //void  *Heap_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes);
34 //void  Heap_Deallocate(void *Ptr);
35 void    Heap_Dump(void);
36 void    Heap_Stats(void);
37
38 // === GLOBALS ===
39 tMutex  glHeap;
40 void    *gHeapStart;
41 void    *gHeapEnd;
42
43 // === CODE ===
44 void Heap_Install(void)
45 {
46         gHeapStart      = (void*)MM_KHEAP_BASE;
47         gHeapEnd        = (void*)MM_KHEAP_BASE;
48         Heap_Extend(HEAP_INIT_SIZE);
49 }
50
51 /**
52  * \fn void *Heap_Extend(int Bytes)
53  * \brief Extend the size of the heap
54  */
55 void *Heap_Extend(int Bytes)
56 {
57         Uint    i;
58         tHeapHead       *head = gHeapEnd;
59         tHeapFoot       *foot;
60         
61         // Bounds Check
62         if( (tVAddr)gHeapEnd == MM_KHEAP_MAX )
63                 return NULL;
64         
65         if( Bytes == 0 ) {
66                 Log_Warning("Heap", "Heap_Extend called with Bytes=%i", Bytes);
67                 return NULL;
68         }
69         
70         // Bounds Check
71         if( (tVAddr)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
72                 Bytes = MM_KHEAP_MAX - (tVAddr)gHeapEnd;
73                 return NULL;
74         }
75         
76         // Heap expands in pages
77         for( i = 0; i < (Bytes+0xFFF) >> 12; i ++ )
78         {
79                 if( !MM_Allocate( (tVAddr)gHeapEnd+(i<<12) ) )
80                 {
81                         Warning("OOM - Heap_Extend");
82                         return NULL;
83                 }
84         }
85         
86         // Increas heap end
87         gHeapEnd = (Uint8*)gHeapEnd + (i << 12);
88         
89         // Create Block
90         head->Size = (Bytes+0xFFF)&~0xFFF;
91         head->Magic = MAGIC_FREE;
92         foot = (void*)( (Uint)gHeapEnd - sizeof(tHeapFoot) );
93         foot->Head = head;
94         foot->Magic = MAGIC_FOOT;
95         
96         return Heap_Merge(head);        // Merge with previous block
97 }
98
99 /**
100  * \fn void *Heap_Merge(tHeapHead *Head)
101  * \brief Merges two ajacent heap blocks
102  */
103 void *Heap_Merge(tHeapHead *Head)
104 {
105         tHeapFoot       *foot;
106         tHeapFoot       *thisFoot;
107         tHeapHead       *head;
108         
109         //Log("Heap_Merge: (Head=%p)", Head);
110         
111         thisFoot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
112         if((Uint)thisFoot > (Uint)gHeapEnd)     return NULL;
113         
114         // Merge Left (Down)
115         foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
116         if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot > (Uint)gHeapStart)
117         && foot->Head->Magic == MAGIC_FREE) {
118                 foot->Head->Size += Head->Size; // Increase size
119                 thisFoot->Head = foot->Head;    // Change backlink
120                 Head->Magic = 0;        // Clear old head
121                 Head->Size = 0;
122                 Head = foot->Head;      // Save new head address
123                 foot->Head = NULL;      // Clear central footer
124                 foot->Magic = 0;
125         }
126         
127         // Merge Right (Upwards)
128         head = (void*)( (Uint)Head + Head->Size );
129         if((Uint)head < (Uint)gHeapEnd && head->Magic == MAGIC_FREE)
130         {
131                 Head->Size += head->Size;
132                 foot = (void*)( (Uint)Head + Head->Size - sizeof(tHeapFoot) );
133                 foot->Head = Head;      // Update Backlink
134                 thisFoot->Head = NULL;  // Clear old footer
135                 thisFoot->Magic = 0;
136                 head->Size = 0;         // Clear old header
137                 head->Magic = 0;
138         }
139         
140         // Return new address
141         return Head;
142 }
143
144 /**
145  * \brief Allocate memory from the heap
146  * \param File  Allocating source file
147  * \param Line  Source line
148  * \param Bytes Size of region to allocate
149  */
150 void *Heap_Allocate(const char *File, int Line, size_t __Bytes)
151 {
152         tHeapHead       *head, *newhead;
153         tHeapFoot       *foot, *newfoot;
154         tHeapHead       *best = NULL;
155         Uint    bestSize = 0;   // Speed hack
156         size_t  Bytes;
157
158         if( __Bytes == 0 ) {
159                 //return NULL;  // TODO: Return a known un-mapped range.
160                 return INVLPTR;
161         }
162         
163         // Get required size
164         #if POW2_SIZES
165         Bytes = __Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot);
166         Bytes = 1UUL << LOG2(__Bytes);
167         #else
168         Bytes = (__Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + MIN_SIZE-1) & ~(MIN_SIZE-1);
169         #endif
170         
171         // Lock Heap
172         Mutex_Acquire(&glHeap);
173         
174         // Traverse Heap
175         for( head = gHeapStart;
176                 (Uint)head < (Uint)gHeapEnd;
177                 head = (void*)((Uint)head + head->Size)
178                 )
179         {
180                 // Alignment Check
181                 #if POW2_SIZES
182                 if( head->Size != 1UUL << LOG2(head->Size) ) {
183                 #else
184                 if( head->Size & (MIN_SIZE-1) ) {
185                 #endif
186                         Mutex_Release(&glHeap); // Release spinlock
187                         #if WARNINGS
188                         Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
189                         Heap_Dump();
190                         #endif
191                         return NULL;
192                 }
193                 
194                 // Check if allocated
195                 if(head->Magic == MAGIC_USED)   continue;
196                 // Error check
197                 if(head->Magic != MAGIC_FREE)   {
198                         Mutex_Release(&glHeap); // Release spinlock
199                         #if WARNINGS
200                         Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic);
201                         Heap_Dump();
202                         #endif
203                         return NULL;
204                 }
205                 
206                 // Size check
207                 if(head->Size < Bytes)  continue;
208                 
209                 // Perfect fit
210                 if(head->Size == Bytes) {
211                         head->Magic = MAGIC_USED;
212                         head->File = File;
213                         head->Line = Line;
214                         Mutex_Release(&glHeap); // Release spinlock
215                         #if DEBUG_TRACE
216                         Log("[Heap   ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size,  __builtin_return_address(0));
217                         #endif
218                         return head->Data;
219                 }
220                 
221                 // Break out of loop
222                 #if FIRST_FIT
223                 best = head;
224                 bestSize = head->Size;
225                 break;
226                 #else
227                 // or check if the block is the best size
228                 if(bestSize > head->Size) {
229                         best = head;
230                         bestSize = head->Size;
231                 }
232                 #endif
233         }
234         
235         // If no block large enough is found, create one
236         if(!best)
237         {
238                 best = Heap_Extend( Bytes );
239                 // Check for errors
240                 if(!best) {
241                         Mutex_Release(&glHeap); // Release spinlock
242                         return NULL;
243                 }
244                 // Check size
245                 if(best->Size == Bytes) {
246                         best->Magic = MAGIC_USED;       // Mark block as used
247                         best->File = File;
248                         best->Line = Line;
249                         Mutex_Release(&glHeap); // Release spinlock
250                         #if DEBUG_TRACE
251                         Log("[Heap   ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
252                         #endif
253                         return best->Data;
254                 }
255         }
256         
257         // Split Block
258         newhead = (void*)( (Uint)best + Bytes );
259         newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
260         foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
261         
262         newfoot->Head = best;   // Create new footer
263         newfoot->Magic = MAGIC_FOOT;
264         newhead->Size = best->Size - Bytes;     // Create new header
265         newhead->Magic = MAGIC_FREE;
266         foot->Head = newhead;   // Update backlink in old footer
267         best->Size = Bytes;             // Update size in old header
268         best->ValidSize = __Bytes;
269         best->Magic = MAGIC_USED;       // Mark block as used
270         best->File = File;
271         best->Line = Line;
272         
273         Mutex_Release(&glHeap); // Release spinlock
274         #if DEBUG_TRACE
275         Log_Debug("Heap", "newhead(%p)->Size = 0x%x", newhead, newhead->Size);
276         Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i",
277                 best->Data, best->Size, File, Line);
278         #endif
279         return best->Data;
280 }
281
282 /**
283  * \fn void Heap_Deallocate(void *Ptr)
284  * \brief Free an allocated memory block
285  */
286 void Heap_Deallocate(void *Ptr)
287 {
288         tHeapHead       *head;
289         tHeapFoot       *foot;
290         
291         #if DEBUG_TRACE
292         Log_Log("Heap", "free: Ptr = %p", Ptr);
293         Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0));
294         #endif
295         
296         // INVLPTR is returned from Heap_Allocate when the allocation
297         // size is zero.
298         if( Ptr == INVLPTR )    return;
299         
300         // Alignment Check
301         if( (Uint)Ptr & (sizeof(Uint)-1) ) {
302                 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
303                 return;
304         }
305         
306         // Sanity check
307         if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
308         {
309                 Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n",
310                         gHeapStart, Ptr, gHeapEnd);
311                 return;
312         }
313         
314         // Check memory block - Header
315         head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
316         if(head->Magic == MAGIC_FREE) {
317                 Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0));
318                 return;
319         }
320         if(head->Magic != MAGIC_USED) {
321                 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)", head, head->Magic);
322                 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
323                 return;
324         }
325         
326         // Check memory block - Footer
327         foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
328         if(foot->Head != head) {
329                 Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)", head, foot->Head);
330                 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
331                 return;
332         }
333         if(foot->Magic != MAGIC_FOOT) {
334                 Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)", head, &foot->Magic, foot->Magic);
335                 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
336                 return;
337         }
338         
339         // Lock
340         Mutex_Acquire( &glHeap );
341         
342         // Mark as free
343         head->Magic = MAGIC_FREE;
344         //head->File = NULL;
345         //head->Line = 0;
346         head->ValidSize = 0;
347         // Merge blocks
348         Heap_Merge( head );
349         
350         // Release
351         Mutex_Release( &glHeap );
352 }
353
354 /**
355  * \brief Increase/Decrease the size of an allocation
356  * \param File  Calling File
357  * \param Line  Calling Line
358  * \param __ptr Old memory
359  * \param __size        New Size
360  */
361 void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size)
362 {
363         tHeapHead       *head = (void*)( (Uint)__ptr-sizeof(tHeapHead) );
364         tHeapHead       *nextHead;
365         tHeapFoot       *foot;
366         Uint    newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1);
367         
368         // Check for reallocating NULL
369         if(__ptr == NULL)       return Heap_Allocate(File, Line, __size);
370         
371         // Check if resize is needed
372         if(newSize <= head->Size)       return __ptr;
373         
374         // Check if next block is free
375         nextHead = (void*)( (Uint)head + head->Size );
376         
377         // Extend into next block
378         if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
379         {
380                 Uint    size = nextHead->Size + head->Size;
381                 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
382                 // Exact Fit
383                 if(size == newSize) {
384                         head->Size = newSize;
385                         head->ValidSize = __size;
386                         head->File = File;
387                         head->Line = Line;
388                         foot->Head = head;
389                         nextHead->Magic = 0;
390                         nextHead->Size = 0;
391                         return __ptr;
392                 }
393                 // Create a new heap block
394                 nextHead = (void*)( (Uint)head + newSize );
395                 nextHead->Size = size - newSize;
396                 nextHead->Magic = MAGIC_FREE;
397                 foot->Head = nextHead;  // Edit 2nd footer
398                 head->Size = newSize;   // Edit first header
399                 head->File = File;
400                 head->Line = Line;
401                 head->ValidSize = __size;
402                 // Create new footer
403                 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
404                 foot->Head = head;
405                 foot->Magic = MAGIC_FOOT;
406                 return __ptr;
407         }
408         
409         // Extend downwards?
410         foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
411         nextHead = foot->Head;
412         if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
413         {
414                 Uint    size = nextHead->Size + head->Size;
415                 // Inexact fit, split things up
416                 if(size > newSize)
417                 {
418                         // TODO
419                         Warning("[Heap   ] TODO: Space efficient realloc when new size is smaller");
420                 }
421                 
422                 // Exact fit
423                 if(size >= newSize)
424                 {
425                         Uint    oldDataSize;
426                         // Set 1st (new/lower) header
427                         nextHead->Magic = MAGIC_USED;
428                         nextHead->Size = newSize;
429                         nextHead->File = File;
430                         nextHead->Line = Line;
431                         nextHead->ValidSize = __size;
432                         // Get 2nd (old) footer
433                         foot = (void*)( (Uint)nextHead + newSize );
434                         foot->Head = nextHead;
435                         // Save old data size
436                         oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
437                         // Clear old header
438                         head->Size = 0;
439                         head->Magic = 0;
440                         // Copy data
441                         memcpy(nextHead->Data, __ptr, oldDataSize);
442                         // Return
443                         return nextHead->Data;
444                 }
445                 // On to the expensive then
446         }
447         
448         // Well, darn
449         nextHead = Heap_Allocate( File, Line, __size );
450         nextHead -= 1;
451         nextHead->File = File;
452         nextHead->Line = Line;
453         nextHead->ValidSize = __size;
454         
455         memcpy(
456                 nextHead->Data,
457                 __ptr,
458                 head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead)
459                 );
460         
461         free(__ptr);
462         
463         return nextHead->Data;
464 }
465
466 /**
467  * \fn void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
468  * \brief Allocate and Zero a buffer in memory
469  * \param File  Allocating file
470  * \param Line  Line of allocation
471  * \param Bytes Size of the allocation
472  */
473 void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
474 {
475         void    *ret = Heap_Allocate(File, Line, Bytes);
476         if(ret == NULL) return NULL;
477         
478         memset( ret, 0, Bytes );
479         
480         return ret;
481 }
482
483 /**
484  * \fn int Heap_IsHeapAddr(void *Ptr)
485  * \brief Checks if an address is a heap pointer
486  */
487 int Heap_IsHeapAddr(void *Ptr)
488 {
489         tHeapHead       *head;
490         if((Uint)Ptr < (Uint)gHeapStart)        return 0;
491         if((Uint)Ptr > (Uint)gHeapEnd)  return 0;
492         if((Uint)Ptr & (sizeof(Uint)-1))        return 0;
493         
494         head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
495         if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
496                 return 0;
497         
498         return 1;
499 }
500
501 /**
502  */
503 void Heap_Validate(void)
504 {
505         Heap_Dump();
506 }
507
508 #if WARNINGS
509 void Heap_Dump(void)
510 {
511         tHeapHead       *head, *badHead;
512         tHeapFoot       *foot = NULL;
513         
514         head = gHeapStart;
515         while( (Uint)head < (Uint)gHeapEnd )
516         {               
517                 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
518                 #if VERBOSE_DUMP
519                 Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C",
520                         head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
521                 Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
522                 if(head->File) {
523                         Log_Log("Heap", "%sowned by %s:%i",
524                                 (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
525                 }
526                 #endif
527                 
528                 // Sanity Check Header
529                 if(head->Size == 0) {
530                         Log_Warning("Heap", "HALTED - Size is zero");
531                         break;
532                 }
533                 if(head->Size & (MIN_SIZE-1)) {
534                         Log_Warning("Heap", "HALTED - Size is malaligned");
535                         break;
536                 }
537                 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
538                         Log_Warning("Heap", "HALTED - Head Magic is Bad");
539                         break;
540                 }
541                 
542                 // Check footer
543                 if(foot->Magic != MAGIC_FOOT) {
544                         Log_Warning("Heap", "HALTED - Foot Magic is Bad");
545                         break;
546                 }
547                 if(head != foot->Head) {
548                         Log_Warning("Heap", "HALTED - Footer backlink is invalid");
549                         break;
550                 }
551                 
552                 #if VERBOSE_DUMP
553                 Log_Log("Heap", "");
554                 #endif
555                 
556                 // All OK? Go to next
557                 head = foot->NextHead;
558         }
559         
560         // If the heap is valid, ok!
561         if( (tVAddr)head == (tVAddr)gHeapEnd )
562                 return ;
563         
564         // Check for a bad return
565         if( (tVAddr)head >= (tVAddr)gHeapEnd )
566                 return ;
567
568         #if !VERBOSE_DUMP
569         Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C",
570                 head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
571         Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
572         if(head->File) {
573                 Log_Log("Heap", "%sowned by %s:%i",
574                         (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
575         }
576         Log_Log("Heap", "");
577         #endif
578         
579         
580         badHead = head;
581         
582         // Work backwards
583         foot = (void*)( (tVAddr)gHeapEnd - sizeof(tHeapFoot) );
584         Log_Log("Heap", "==== Going Backwards ==== (from %p)", foot);
585         head = foot->Head;
586         while( (tVAddr)head >= (tVAddr)badHead )
587         {
588                 Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C",
589                         head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
590                 Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
591                 if(head->File)
592                         Log_Log("Heap", "%sowned by %s:%i",
593                                 (head->Magic!=MAGIC_USED?"was ":""),
594                                 head->File, head->Line);
595                 Log_Log("Heap", "");
596                 
597                 // Sanity Check Header
598                 if(head->Size == 0) {
599                         Log_Warning("Heap", "HALTED - Size is zero");
600                         break;
601                 }
602                 if(head->Size & (MIN_SIZE-1)) {
603                         Log_Warning("Heap", " - Size is malaligned (&0x%x)", ~(MIN_SIZE-1));
604                         break ;
605                 }
606                 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
607                         Log_Warning("Heap", "HALTED - Head Magic is Bad");
608                         break;
609                 }
610                 
611                 // Check footer
612                 if(foot->Magic != MAGIC_FOOT) {
613                         Log_Warning("Heap", "HALTED - Foot Magic is Bad");
614                         break;
615                 }
616                 if(head != foot->Head) {
617                         Log_Warning("Heap", "HALTED - Footer backlink is invalid");
618                         break;
619                 }
620                 
621                 if(head == badHead)     break;
622                 
623                 foot = (void*)( (tVAddr)head - sizeof(tHeapFoot) );
624                 head = foot->Head;
625                 Log_Debug("Heap", "head=%p", head);
626         }
627         
628         Panic("Heap_Dump - Heap is corrupted, kernel panic!");
629 }
630 #endif
631
632 #if 1
633 void Heap_Stats(void)
634 {
635         tHeapHead       *head;
636          int    nBlocks = 0;
637          int    nFree = 0;
638          int    totalBytes = 0;
639          int    freeBytes = 0;
640          int    maxAlloc=0, minAlloc=-1;
641          int    avgAlloc, frag, overhead;
642         
643         for(head = gHeapStart;
644                 (Uint)head < (Uint)gHeapEnd;
645                 head = (void*)( (Uint)head + head->Size )
646                 )
647         {       
648                 nBlocks ++;
649                 totalBytes += head->Size;
650                 if( head->Magic == MAGIC_FREE )
651                 {
652                         nFree ++;
653                         freeBytes += head->Size;
654                 }
655                 else if( head->Magic == MAGIC_USED) {
656                         if(maxAlloc < head->Size)       maxAlloc = head->Size;
657                         if(minAlloc == -1 || minAlloc > head->Size)
658                                 minAlloc = head->Size;
659                 }
660                 else {
661                         Log_Warning("Heap", "Magic on %p invalid, skipping remainder of heap", head);
662                         break;
663                 }
664                 
665                 // Print the block info?
666                 #if 1
667                 Log_Debug("Heap", "%p (0x%x) - 0x%x (%i) Owned by %s:%i",
668                         head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size, head->ValidSize, head->File, head->Line);
669                 #endif
670         }
671
672         Log_Log("Heap", "%i blocks (0x%x bytes)", nBlocks, totalBytes);
673         Log_Log("Heap", "%i free blocks (0x%x bytes)", nFree, freeBytes);
674         frag = (nFree-1)*10000/nBlocks;
675         Log_Log("Heap", "%i.%02i%% Heap Fragmentation", frag/100, frag%100);
676         avgAlloc = (totalBytes-freeBytes)/(nBlocks-nFree);
677         overhead = (sizeof(tHeapFoot)+sizeof(tHeapHead))*10000/avgAlloc;
678         Log_Log("Heap", "Average allocation: %i bytes, Average Overhead: %i.%02i%%",
679                 avgAlloc, overhead/100, overhead%100
680                 );
681         Log_Log("Heap", "Smallest Block: %i bytes, Largest: %i bytes", 
682                 minAlloc, maxAlloc);
683         
684         // Scan and get distribution
685         #if 1
686         {
687                 struct {
688                         Uint    Size;
689                         Uint    Count;
690                 }       sizeCounts[nBlocks];
691                  int    i;
692                 
693                 memset(sizeCounts, 0, nBlocks*sizeof(sizeCounts[0]));
694                 
695                 for(head = gHeapStart;
696                         (Uint)head < (Uint)gHeapEnd;
697                         head = (void*)( (Uint)head + head->Size )
698                         )
699                 {
700                         for( i = 0; i < nBlocks; i ++ ) {
701                                 if( sizeCounts[i].Size == 0 )
702                                         break;
703                                 if( sizeCounts[i].Size == head->Size )
704                                         break;
705                         }
706                         // Should never reach this part (in a non-concurrent case)
707                         if( i == nBlocks )      continue;
708                         sizeCounts[i].Size = head->Size;
709                         sizeCounts[i].Count ++;
710                         #if 1
711                         //Log("Heap_Stats: %i %p - 0x%x bytes (%s) (%i)", nBlocks, head,
712                         //      head->Size, (head->Magic==MAGIC_FREE?"FREE":"used"), i
713                         //      );
714                         //Log("Heap_Stats: sizeCounts[%i] = {Size:0x%x, Count: %i}", i,
715                         //      sizeCounts[i].Size, sizeCounts[i].Count);
716                         #endif
717                 }
718                 
719                 for( i = 0; i < nBlocks && sizeCounts[i].Count; i ++ )
720                 {
721                         Log("Heap_Stats: 0x%x - %i blocks",
722                                 sizeCounts[i].Size, sizeCounts[i].Count
723                                 );
724                 }
725         }
726         #endif
727 }
728 #endif
729
730 // === EXPORTS ===
731 EXPORT(Heap_Allocate);
732 EXPORT(Heap_AllocateZero);
733 EXPORT(Heap_Reallocate);
734 EXPORT(Heap_Deallocate);
735 EXPORT(Heap_IsHeapAddr);

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