Misc - x86_64 port related changes
[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                         head->ValidSize = __Bytes;
215                         Mutex_Release(&glHeap); // Release spinlock
216                         #if DEBUG_TRACE
217                         Log("[Heap   ] Malloc'd %p (%i bytes), returning to %p", head->Data, head->Size,  __builtin_return_address(0));
218                         #endif
219                         return head->Data;
220                 }
221                 
222                 // Break out of loop
223                 #if FIRST_FIT
224                 best = head;
225                 bestSize = head->Size;
226                 break;
227                 #else
228                 // or check if the block is the best size
229                 if(bestSize > head->Size) {
230                         best = head;
231                         bestSize = head->Size;
232                 }
233                 #endif
234         }
235         
236         // If no block large enough is found, create one
237         if(!best)
238         {
239                 best = Heap_Extend( Bytes );
240                 // Check for errors
241                 if(!best) {
242                         Mutex_Release(&glHeap); // Release spinlock
243                         return NULL;
244                 }
245                 // Check size
246                 if(best->Size == Bytes) {
247                         best->Magic = MAGIC_USED;       // Mark block as used
248                         best->File = File;
249                         best->Line = Line;
250                         head->ValidSize = __Bytes;
251                         Mutex_Release(&glHeap); // Release spinlock
252                         #if DEBUG_TRACE
253                         Log("[Heap   ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
254                         #endif
255                         return best->Data;
256                 }
257         }
258         
259         // Split Block
260         newhead = (void*)( (Uint)best + Bytes );
261         newfoot = (void*)( (Uint)best + Bytes - sizeof(tHeapFoot) );
262         foot = (void*)( (Uint)best + best->Size - sizeof(tHeapFoot) );
263         
264         newfoot->Head = best;   // Create new footer
265         newfoot->Magic = MAGIC_FOOT;
266         newhead->Size = best->Size - Bytes;     // Create new header
267         newhead->Magic = MAGIC_FREE;
268         foot->Head = newhead;   // Update backlink in old footer
269         best->Size = Bytes;             // Update size in old header
270         best->ValidSize = __Bytes;
271         best->Magic = MAGIC_USED;       // Mark block as used
272         best->File = File;
273         best->Line = Line;
274         
275         Mutex_Release(&glHeap); // Release spinlock
276         #if DEBUG_TRACE
277         Log_Debug("Heap", "newhead(%p)->Size = 0x%x", newhead, newhead->Size);
278         Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i",
279                 best->Data, best->Size, File, Line);
280         #endif
281         return best->Data;
282 }
283
284 /**
285  * \fn void Heap_Deallocate(void *Ptr)
286  * \brief Free an allocated memory block
287  */
288 void Heap_Deallocate(void *Ptr)
289 {
290         tHeapHead       *head;
291         tHeapFoot       *foot;
292         
293         #if DEBUG_TRACE
294         Log_Log("Heap", "free: Ptr = %p", Ptr);
295         Log_Log("Heap", "free: Returns to %p", __builtin_return_address(0));
296         #endif
297         
298         // INVLPTR is returned from Heap_Allocate when the allocation
299         // size is zero.
300         if( Ptr == INVLPTR )    return;
301         
302         // Alignment Check
303         if( (Uint)Ptr & (sizeof(Uint)-1) ) {
304                 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
305                 return;
306         }
307         
308         // Sanity check
309         if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
310         {
311                 Log_Warning("Heap", "free - Passed a non-heap address (%p < %p < %p)\n",
312                         gHeapStart, Ptr, gHeapEnd);
313                 return;
314         }
315         
316         // Check memory block - Header
317         head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
318         if(head->Magic == MAGIC_FREE) {
319                 Log_Warning("Heap", "free - Passed a freed block (%p) by %p", head, __builtin_return_address(0));
320                 return;
321         }
322         if(head->Magic != MAGIC_USED) {
323                 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)", head, head->Magic);
324                 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
325                 return;
326         }
327         
328         // Check memory block - Footer
329         foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
330         if(foot->Head != head) {
331                 Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)", head, foot->Head);
332                 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
333                 return;
334         }
335         if(foot->Magic != MAGIC_FOOT) {
336                 Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)", head, &foot->Magic, foot->Magic);
337                 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
338                 return;
339         }
340         
341         // Lock
342         Mutex_Acquire( &glHeap );
343         
344         // Mark as free
345         head->Magic = MAGIC_FREE;
346         //head->File = NULL;
347         //head->Line = 0;
348         head->ValidSize = 0;
349         // Merge blocks
350         Heap_Merge( head );
351         
352         // Release
353         Mutex_Release( &glHeap );
354 }
355
356 /**
357  * \brief Increase/Decrease the size of an allocation
358  * \param File  Calling File
359  * \param Line  Calling Line
360  * \param __ptr Old memory
361  * \param __size        New Size
362  */
363 void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size)
364 {
365         tHeapHead       *head = (void*)( (Uint)__ptr-sizeof(tHeapHead) );
366         tHeapHead       *nextHead;
367         tHeapFoot       *foot;
368         Uint    newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1);
369         
370         // Check for reallocating NULL
371         if(__ptr == NULL)       return Heap_Allocate(File, Line, __size);
372         
373         // Check if resize is needed
374         if(newSize <= head->Size)       return __ptr;
375         
376         // Check if next block is free
377         nextHead = (void*)( (Uint)head + head->Size );
378         
379         // Extend into next block
380         if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
381         {
382                 Uint    size = nextHead->Size + head->Size;
383                 foot = (void*)( (Uint)nextHead + nextHead->Size - sizeof(tHeapFoot) );
384                 // Exact Fit
385                 if(size == newSize) {
386                         head->Size = newSize;
387                         head->ValidSize = __size;
388                         head->File = File;
389                         head->Line = Line;
390                         foot->Head = head;
391                         nextHead->Magic = 0;
392                         nextHead->Size = 0;
393                         return __ptr;
394                 }
395                 // Create a new heap block
396                 nextHead = (void*)( (Uint)head + newSize );
397                 nextHead->Size = size - newSize;
398                 nextHead->Magic = MAGIC_FREE;
399                 foot->Head = nextHead;  // Edit 2nd footer
400                 head->Size = newSize;   // Edit first header
401                 head->File = File;
402                 head->Line = Line;
403                 head->ValidSize = __size;
404                 // Create new footer
405                 foot = (void*)( (Uint)head + newSize - sizeof(tHeapFoot) );
406                 foot->Head = head;
407                 foot->Magic = MAGIC_FOOT;
408                 return __ptr;
409         }
410         
411         // Extend downwards?
412         foot = (void*)( (Uint)head - sizeof(tHeapFoot) );
413         nextHead = foot->Head;
414         if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
415         {
416                 Uint    size = nextHead->Size + head->Size;
417                 // Inexact fit, split things up
418                 if(size > newSize)
419                 {
420                         // TODO
421                         Warning("[Heap   ] TODO: Space efficient realloc when new size is smaller");
422                 }
423                 
424                 // Exact fit
425                 if(size >= newSize)
426                 {
427                         Uint    oldDataSize;
428                         // Set 1st (new/lower) header
429                         nextHead->Magic = MAGIC_USED;
430                         nextHead->Size = newSize;
431                         nextHead->File = File;
432                         nextHead->Line = Line;
433                         nextHead->ValidSize = __size;
434                         // Get 2nd (old) footer
435                         foot = (void*)( (Uint)nextHead + newSize );
436                         foot->Head = nextHead;
437                         // Save old data size
438                         oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
439                         // Clear old header
440                         head->Size = 0;
441                         head->Magic = 0;
442                         // Copy data
443                         memcpy(nextHead->Data, __ptr, oldDataSize);
444                         // Return
445                         return nextHead->Data;
446                 }
447                 // On to the expensive then
448         }
449         
450         // Well, darn
451         nextHead = Heap_Allocate( File, Line, __size );
452         nextHead -= 1;
453         nextHead->File = File;
454         nextHead->Line = Line;
455         nextHead->ValidSize = __size;
456         
457         memcpy(
458                 nextHead->Data,
459                 __ptr,
460                 head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead)
461                 );
462         
463         free(__ptr);
464         
465         return nextHead->Data;
466 }
467
468 /**
469  * \fn void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
470  * \brief Allocate and Zero a buffer in memory
471  * \param File  Allocating file
472  * \param Line  Line of allocation
473  * \param Bytes Size of the allocation
474  */
475 void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
476 {
477         void    *ret = Heap_Allocate(File, Line, Bytes);
478         if(ret == NULL) return NULL;
479         
480         memset( ret, 0, Bytes );
481         
482         return ret;
483 }
484
485 /**
486  * \fn int Heap_IsHeapAddr(void *Ptr)
487  * \brief Checks if an address is a heap pointer
488  */
489 int Heap_IsHeapAddr(void *Ptr)
490 {
491         tHeapHead       *head;
492         if((Uint)Ptr < (Uint)gHeapStart)        return 0;
493         if((Uint)Ptr > (Uint)gHeapEnd)  return 0;
494         if((Uint)Ptr & (sizeof(Uint)-1))        return 0;
495         
496         head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
497         if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
498                 return 0;
499         
500         return 1;
501 }
502
503 /**
504  */
505 void Heap_Validate(void)
506 {
507         Heap_Dump();
508 }
509
510 #if WARNINGS
511 void Heap_Dump(void)
512 {
513         tHeapHead       *head, *badHead;
514         tHeapFoot       *foot = NULL;
515         
516         head = gHeapStart;
517         while( (Uint)head < (Uint)gHeapEnd )
518         {               
519                 foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
520                 #if VERBOSE_DUMP
521                 Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C",
522                         head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
523                 Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
524                 if(head->File) {
525                         Log_Log("Heap", "%sowned by %s:%i",
526                                 (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
527                 }
528                 #endif
529                 
530                 // Sanity Check Header
531                 if(head->Size == 0) {
532                         Log_Warning("Heap", "HALTED - Size is zero");
533                         break;
534                 }
535                 if(head->Size & (MIN_SIZE-1)) {
536                         Log_Warning("Heap", "HALTED - Size is malaligned");
537                         break;
538                 }
539                 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
540                         Log_Warning("Heap", "HALTED - Head Magic is Bad");
541                         break;
542                 }
543                 
544                 // Check footer
545                 if(foot->Magic != MAGIC_FOOT) {
546                         Log_Warning("Heap", "HALTED - Foot Magic is Bad");
547                         break;
548                 }
549                 if(head != foot->Head) {
550                         Log_Warning("Heap", "HALTED - Footer backlink is invalid");
551                         break;
552                 }
553                 
554                 #if VERBOSE_DUMP
555                 Log_Log("Heap", "");
556                 #endif
557                 
558                 // All OK? Go to next
559                 head = foot->NextHead;
560         }
561         
562         // If the heap is valid, ok!
563         if( (tVAddr)head == (tVAddr)gHeapEnd )
564                 return ;
565         
566         // Check for a bad return
567         if( (tVAddr)head >= (tVAddr)gHeapEnd )
568                 return ;
569
570         #if !VERBOSE_DUMP
571         Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C",
572                 head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
573         Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
574         if(head->File) {
575                 Log_Log("Heap", "%sowned by %s:%i",
576                         (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
577         }
578         Log_Log("Heap", "");
579         #endif
580         
581         
582         badHead = head;
583         
584         // Work backwards
585         foot = (void*)( (tVAddr)gHeapEnd - sizeof(tHeapFoot) );
586         Log_Log("Heap", "==== Going Backwards ==== (from %p)", foot);
587         head = foot->Head;
588         while( (tVAddr)head >= (tVAddr)badHead )
589         {
590                 Log_Log("Heap", "%p (0x%llx): 0x%08lx %i %4C",
591                         head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
592                 Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
593                 if(head->File)
594                         Log_Log("Heap", "%sowned by %s:%i",
595                                 (head->Magic!=MAGIC_USED?"was ":""),
596                                 head->File, head->Line);
597                 Log_Log("Heap", "");
598                 
599                 // Sanity Check Header
600                 if(head->Size == 0) {
601                         Log_Warning("Heap", "HALTED - Size is zero");
602                         break;
603                 }
604                 if(head->Size & (MIN_SIZE-1)) {
605                         Log_Warning("Heap", " - Size is malaligned (&0x%x)", ~(MIN_SIZE-1));
606                         break ;
607                 }
608                 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
609                         Log_Warning("Heap", "HALTED - Head Magic is Bad");
610                         break;
611                 }
612                 
613                 // Check footer
614                 if(foot->Magic != MAGIC_FOOT) {
615                         Log_Warning("Heap", "HALTED - Foot Magic is Bad");
616                         break;
617                 }
618                 if(head != foot->Head) {
619                         Log_Warning("Heap", "HALTED - Footer backlink is invalid");
620                         break;
621                 }
622                 
623                 if(head == badHead)     break;
624                 
625                 foot = (void*)( (tVAddr)head - sizeof(tHeapFoot) );
626                 head = foot->Head;
627                 Log_Debug("Heap", "head=%p", head);
628         }
629         
630         Panic("Heap_Dump - Heap is corrupted, kernel panic!");
631 }
632 #endif
633
634 #if 1
635 void Heap_Stats(void)
636 {
637         tHeapHead       *head;
638          int    nBlocks = 0;
639          int    nFree = 0;
640          int    totalBytes = 0;
641          int    freeBytes = 0;
642          int    maxAlloc=0, minAlloc=-1;
643          int    avgAlloc, frag, overhead;
644         
645         for(head = gHeapStart;
646                 (Uint)head < (Uint)gHeapEnd;
647                 head = (void*)( (Uint)head + head->Size )
648                 )
649         {       
650                 nBlocks ++;
651                 totalBytes += head->Size;
652                 if( head->Magic == MAGIC_FREE )
653                 {
654                         nFree ++;
655                         freeBytes += head->Size;
656                 }
657                 else if( head->Magic == MAGIC_USED) {
658                         if(maxAlloc < head->Size)       maxAlloc = head->Size;
659                         if(minAlloc == -1 || minAlloc > head->Size)
660                                 minAlloc = head->Size;
661                 }
662                 else {
663                         Log_Warning("Heap", "Magic on %p invalid, skipping remainder of heap", head);
664                         break;
665                 }
666                 
667                 // Print the block info?
668                 #if 1
669                 if( head->Magic == MAGIC_FREE )
670                         Log_Debug("Heap", "%p (0x%llx) - 0x%x free",
671                                 head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size);
672                 else
673                         Log_Debug("Heap", "%p (0x%llx) - 0x%x (%i) Owned by %s:%i",
674                                 head->Data, MM_GetPhysAddr((tVAddr)&head->Data), head->Size, head->ValidSize, head->File, head->Line);
675                 #endif
676         }
677
678         Log_Log("Heap", "%i blocks (0x%x bytes)", nBlocks, totalBytes);
679         Log_Log("Heap", "%i free blocks (0x%x bytes)", nFree, freeBytes);
680         frag = (nFree-1)*10000/nBlocks;
681         Log_Log("Heap", "%i.%02i%% Heap Fragmentation", frag/100, frag%100);
682         avgAlloc = (totalBytes-freeBytes)/(nBlocks-nFree);
683         overhead = (sizeof(tHeapFoot)+sizeof(tHeapHead))*10000/avgAlloc;
684         Log_Log("Heap", "Average allocation: %i bytes, Average Overhead: %i.%02i%%",
685                 avgAlloc, overhead/100, overhead%100
686                 );
687         Log_Log("Heap", "Smallest Block: %i bytes, Largest: %i bytes", 
688                 minAlloc, maxAlloc);
689         
690         // Scan and get distribution
691         #if 1
692         {
693                 struct {
694                         Uint    Size;
695                         Uint    Count;
696                 }       sizeCounts[nBlocks];
697                  int    i;
698                 
699                 memset(sizeCounts, 0, nBlocks*sizeof(sizeCounts[0]));
700                 
701                 for(head = gHeapStart;
702                         (Uint)head < (Uint)gHeapEnd;
703                         head = (void*)( (Uint)head + head->Size )
704                         )
705                 {
706                         for( i = 0; i < nBlocks; i ++ ) {
707                                 if( sizeCounts[i].Size == 0 )
708                                         break;
709                                 if( sizeCounts[i].Size == head->Size )
710                                         break;
711                         }
712                         // Should never reach this part (in a non-concurrent case)
713                         if( i == nBlocks )      continue;
714                         sizeCounts[i].Size = head->Size;
715                         sizeCounts[i].Count ++;
716                         #if 1
717                         //Log("Heap_Stats: %i %p - 0x%x bytes (%s) (%i)", nBlocks, head,
718                         //      head->Size, (head->Magic==MAGIC_FREE?"FREE":"used"), i
719                         //      );
720                         //Log("Heap_Stats: sizeCounts[%i] = {Size:0x%x, Count: %i}", i,
721                         //      sizeCounts[i].Size, sizeCounts[i].Count);
722                         #endif
723                 }
724                 
725                 for( i = 0; i < nBlocks && sizeCounts[i].Count; i ++ )
726                 {
727                         Log("Heap_Stats: 0x%x - %i blocks",
728                                 sizeCounts[i].Size, sizeCounts[i].Count
729                                 );
730                 }
731         }
732         #endif
733 }
734 #endif
735
736 // === EXPORTS ===
737 EXPORT(Heap_Allocate);
738 EXPORT(Heap_AllocateZero);
739 EXPORT(Heap_Reallocate);
740 EXPORT(Heap_Deallocate);
741 EXPORT(Heap_IsHeapAddr);

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