3 * - By John Hodge (thePowersGang)
6 * - Dynamic memory allocation
12 #include <debug_hooks.h>
14 #define WARNINGS 1 // Warn and dump on heap errors
15 #define DEBUG_TRACE 0 // Enable tracing of allocations
16 #define VERBOSE_DUMP 0 // Set to 1 to enable a verbose dump when heap errors are encountered
17 #define VALIDATE_ON_ALLOC 1 // Set to 1 to enable validation of the heap on all malloc() calls
20 #define HEAP_INIT_SIZE 0x8000 // 32 KiB
21 #define MIN_SIZE (sizeof(void*))*8 // 8 Machine Words
23 #define COMPACT_HEAP 0 // Use 4 byte header?
26 //#define MAGIC_FOOT 0x2ACE5505
27 //#define MAGIC_FREE 0xACE55000
28 //#define MAGIC_USED 0x862B0505 // MAGIC_FOOT ^ MAGIC_FREE
29 #define MAGIC_FOOT 0x544F4F46 // 'FOOT'
30 #define MAGIC_FREE 0x45455246 // 'FREE'
31 #define MAGIC_USED 0x44455355 // 'USED'
34 void Heap_Install(void);
35 void *Heap_Extend(size_t Bytes);
36 void *Heap_Merge(tHeapHead *Head);
37 static const size_t Heap_int_GetBlockSize(size_t AllocSize);
38 //void *Heap_Allocate(const char *File, int Line, size_t Bytes);
39 //void *Heap_AllocateZero(const char *File, int Line, size_t Bytes);
40 //void *Heap_Reallocate(const char *File, int Line, void *Ptr, size_t Bytes);
41 //void Heap_Deallocate(const char *File, int Line, void *Ptr);
42 int Heap_int_ApplyWatchpont(void *Addr, bool Enabled);
43 void Heap_int_WatchBlock(tHeapHead *Head, bool Enabled);
44 //void Heap_Dump(void);
45 void Heap_ValidateDump(int bVerbose);
46 //void Heap_Stats(void);
50 tHeapHead *gHeapStart;
54 void Heap_Install(void)
56 gHeapStart = (void*)MM_KHEAP_BASE;
57 gHeapEnd = gHeapStart;
58 Heap_Extend(HEAP_INIT_SIZE);
61 static inline tHeapHead *Heap_NextHead(tHeapHead *Head) {
62 return (void*)( (char*)Head + Head->Size );
64 static inline tHeapFoot *Heap_ThisFoot(tHeapHead *Head) {
65 return (void*)( (char*)Head + Head->Size - sizeof(tHeapFoot) );
67 static inline tHeapFoot *Heap_PrevFoot(tHeapHead *Head) {
68 //ASSERT(Head != gHeapStart);
69 return (void*)( (char*)Head - sizeof(tHeapFoot) );
73 * \brief Extend the size of the heap
75 void *Heap_Extend(size_t Bytes)
77 //Debug("Heap_Extend(0x%x)", Bytes);
80 if( gHeapEnd == (tHeapHead*)MM_KHEAP_MAX ) {
81 Log_Error("Heap", "Heap limit reached (%p)", (void*)MM_KHEAP_MAX);
86 Log_Warning("Heap", "Heap_Extend called with Bytes=%i", Bytes);
90 const Uint pages = (Bytes + 0xFFF) >> 12;
91 tHeapHead *new_end = (void*)( (tVAddr)gHeapEnd + (pages << 12) );
93 if( new_end > (tHeapHead*)MM_KHEAP_MAX )
95 Log_Error("Heap", "Heap limit exceeded (%p)", (void*)new_end);
96 // TODO: Clip allocation to available space, and have caller check returned block
100 // Heap expands in pages
101 for( Uint i = 0; i < pages; i ++ )
103 if( !MM_Allocate( (tPage*)gHeapEnd + i ) )
105 Warning("OOM - Heap_Extend (%i bytes)");
112 tHeapHead *head = gHeapEnd;
116 head->Size = (Bytes+0xFFF)&~0xFFF;
117 head->Magic = MAGIC_FREE;
118 tHeapFoot *foot = Heap_ThisFoot(head);
120 foot->Magic = MAGIC_FOOT;
122 return Heap_Merge(head); // Merge with previous block
126 * \brief Merges two adjacent heap blocks
128 void *Heap_Merge(tHeapHead *Head)
130 //Log("Heap_Merge: (Head=%p)", Head);
131 tHeapFoot *thisFoot = Heap_ThisFoot(Head);
133 ASSERT( Heap_NextHead(Head) <= gHeapEnd );
136 tHeapFoot *foot = Heap_PrevFoot(Head);
137 if( Head > gHeapStart && foot->Head->Magic == MAGIC_FREE)
139 foot->Head->Size += Head->Size; // Increase size
140 thisFoot->Head = foot->Head; // Change backlink
141 Head->Magic = 0; // Clear old head
143 Head = foot->Head; // Save new head address
144 foot->Head = NULL; // Clear central footer
148 // Merge Right (Upwards)
149 tHeapHead *nexthead = Heap_NextHead(Head);
150 if(nexthead < gHeapEnd && nexthead->Magic == MAGIC_FREE)
152 Head->Size += nexthead->Size;
153 foot = Heap_ThisFoot(Head);
154 foot->Head = Head; // Update Backlink
155 thisFoot->Head = NULL; // Clear old footer
157 nexthead->Size = 0; // Clear old header
161 // Return new address
165 static const size_t Heap_int_GetBlockSize(size_t AllocSize)
169 Bytes = AllocSize + sizeof(tHeapHead) + sizeof(tHeapFoot);
170 Bytes = 1UUL << LOG2(Bytes);
172 Bytes = (AllocSize + sizeof(tHeapHead) + sizeof(tHeapFoot) + MIN_SIZE-1) & ~(MIN_SIZE-1);
178 * \param File Allocating source file
179 * \param Line Source line
180 * \param __Bytes Size of region to allocate
182 void *Heap_Allocate(const char *File, int Line, size_t __Bytes)
185 tHeapFoot *foot, *newfoot;
186 tHeapHead *best = NULL;
187 Uint bestSize = UINT_MAX; // Speed hack
191 return NULL; // TODO: Return a known un-mapped range.
195 #if VALIDATE_ON_ALLOC
200 Bytes = Heap_int_GetBlockSize(__Bytes);
203 Mutex_Acquire(&glHeap);
206 for( tHeapHead *head = gHeapStart; head < gHeapEnd; head = Heap_NextHead(head) )
210 if( head->Size != 1UUL << LOG2(head->Size) ) {
212 if( head->Size & (MIN_SIZE-1) ) {
214 Mutex_Release(&glHeap); // Release spinlock
216 Log_Warning("Heap", "Size of heap address %p is invalid"
217 " - not aligned (0x%x) [at paddr 0x%x]",
218 head, head->Size, MM_GetPhysAddr(&head->Size));
219 Heap_ValidateDump(VERBOSE_DUMP);
223 if( head->Size < MIN_SIZE ) {
224 Mutex_Release(&glHeap);
226 Log_Warning("Heap", "Size of heap address %p is invalid"
227 " - Too small (0x%x) [at paddr 0x%x]",
228 head, head->Size, MM_GetPhysAddr(&head->Size));
229 Heap_ValidateDump(VERBOSE_DUMP);
233 if( head->Size > (2<<30) ) {
234 Mutex_Release(&glHeap);
236 Log_Warning("Heap", "Size of heap address %p is invalid"
237 " - Over 2GiB (0x%x) [at paddr 0x%x]",
238 head, head->Size, MM_GetPhysAddr(&head->Size));
239 Heap_ValidateDump(VERBOSE_DUMP);
244 // Check if allocated
245 if(head->Magic == MAGIC_USED) continue;
247 if(head->Magic != MAGIC_FREE) {
248 Mutex_Release(&glHeap); // Release spinlock
250 Log_Warning("Heap", "Magic of heap address %p is invalid (%p = 0x%x)",
251 head, &head->Magic, head->Magic);
252 Heap_ValidateDump(VERBOSE_DUMP);
258 if(head->Size < Bytes) continue;
261 if(head->Size == Bytes) {
262 head->Magic = MAGIC_USED;
265 head->ValidSize = __Bytes;
266 head->AllocateTime = now();
267 Mutex_Release(&glHeap); // Release spinlock
269 Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i",
270 head->Data, head->Size, File, Line);
278 bestSize = head->Size;
281 // or check if the block is the best size
282 if(bestSize > head->Size) {
284 bestSize = head->Size;
289 // If no block large enough is found, create one
292 best = Heap_Extend( Bytes );
295 Warning("OOM when allocating 0x%x bytes", Bytes);
296 Mutex_Release(&glHeap); // Release spinlock
300 if(best->Size == Bytes) {
301 best->Magic = MAGIC_USED; // Mark block as used
304 best->ValidSize = __Bytes;
305 best->AllocateTime = now();
306 Mutex_Release(&glHeap); // Release spinlock
308 Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i",
309 best->Data, best->Size, File, Line);
316 // - Save size for new block
317 size_t newsize = best->Size - Bytes;
318 // - Allocate beginning of old block
319 best->Size = Bytes; // Update size in old header
320 best->ValidSize = __Bytes;
321 best->Magic = MAGIC_USED; // Mark block as used
324 best->AllocateTime = now();
325 // - Create a new foot on old block
326 newfoot = Heap_ThisFoot(best);
327 newfoot->Head = best; // Create new footer
328 newfoot->Magic = MAGIC_FOOT;
329 // - Create a new header after resized old
330 newhead = Heap_NextHead(best);
331 newhead->Size = newsize;
332 newhead->Magic = MAGIC_FREE;
333 newhead->ValidSize = 0;
334 newhead->File = NULL;
337 foot = Heap_ThisFoot(newhead);
338 foot->Head = newhead;
340 Mutex_Release(&glHeap); // Release spinlock
342 Log_Debug("Heap", "Malloc'd %p (0x%x bytes), returning to %s:%i",
343 best->Data, best->Size, File, Line);
349 * \brief Free an allocated memory block
351 void Heap_Deallocate(const char *File, int Line, void *Ptr)
353 // INVLPTR is returned from Heap_Allocate when the allocation
355 if( Ptr == INVLPTR ) return;
358 if( (tVAddr)Ptr % sizeof(void*) != 0 ) {
359 Log_Warning("Heap", "free - Passed a non-aligned address (%p)", Ptr);
364 if((Uint)Ptr < (Uint)gHeapStart || (Uint)Ptr > (Uint)gHeapEnd)
366 Log_Warning("Heap", "free - Passed a non-heap address by %p (%p < %p < %p)",
367 __builtin_return_address(0), gHeapStart, Ptr, gHeapEnd);
371 // Check memory block - Header
372 tHeapHead *head = (tHeapHead*)Ptr - 1;
373 if(head->Magic == MAGIC_FREE) {
374 Log_Warning("Heap", "free - Passed a freed block (%p) by %s:%i (was freed by %s:%i)",
376 head->File, head->Line);
377 Proc_PrintBacktrace();
380 if(head->Magic != MAGIC_USED) {
381 Log_Warning("Heap", "free - Magic value is invalid (%p, 0x%x)", head, head->Magic);
382 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
386 // Check memory block - Footer
387 tHeapFoot *foot = Heap_ThisFoot(head);
388 if(foot->Head != head) {
389 Log_Warning("Heap", "free - Footer backlink is incorrect (%p, 0x%x)", head, foot->Head);
390 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
393 if(foot->Magic != MAGIC_FOOT) {
394 Log_Warning("Heap", "free - Footer magic is invalid (%p, %p = 0x%x)",
395 head, &foot->Magic, foot->Magic);
396 Log_Notice("Heap", "Allocated by %s:%i", head->File, head->Line);
401 Log_Debug("Heap", "free: %p freed by %s:%i (%i old)",
402 Ptr, File, Line, now()-head->AllocateTime);
406 Mutex_Acquire( &glHeap );
408 Heap_int_WatchBlock(head, false);
411 head->Magic = MAGIC_FREE;
419 Mutex_Release( &glHeap );
423 * \brief Increase/Decrease the size of an allocation
424 * \param File Calling File
425 * \param Line Calling Line
426 * \param __ptr Old memory
427 * \param __size New Size
429 void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size)
431 tHeapHead *head = (tHeapHead*)__ptr - 1;
434 Uint newSize = Heap_int_GetBlockSize(__size);
436 // Check for reallocating NULL
438 return Heap_Allocate(File, Line, __size);
440 if( !Heap_IsHeapAddr(__ptr)) {
441 Log_Error("Heap", "%s:%i passed non-heap address %p when reallocating to %zi",
442 File, Line, __ptr, __size);
446 // Check if resize is needed
447 // TODO: Reduce size of block if needed
448 if(newSize <= head->Size) {
450 Log_Debug("Heap", "realloc maintain %p (0x%x >= 0x%x), returning to %s:%i",
451 head->Data, head->Size, newSize, File, Line);
456 #if VALIDATE_ON_ALLOC
460 Heap_int_WatchBlock(head, false);
462 // Check if next block is free
463 nextHead = Heap_NextHead(head);
465 // Extend into next block
466 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
469 Log_Debug("Heap", "realloc expand %p (0x%x to 0x%x), returning to %s:%i",
470 head->Data, head->Size, newSize, File, Line);
472 Uint size = nextHead->Size + head->Size;
473 foot = Heap_ThisFoot(nextHead);
475 if(size == newSize) {
476 head->Size = newSize;
477 head->ValidSize = __size;
485 // Create a new heap block
486 // - Update old with new information
487 head->Size = newSize;
488 head->File = File; // Kinda counts as a new allocation
490 head->ValidSize = __size;
491 // - Create new footer
492 foot = Heap_ThisFoot(head);
494 foot->Magic = MAGIC_FOOT;
495 // - Create new header
496 nextHead = Heap_NextHead(head);
497 nextHead->Size = size - newSize;
498 nextHead->Magic = MAGIC_FREE;
499 // - Update old footer
500 Heap_ThisFoot(nextHead)->Head = nextHead;
505 foot = Heap_PrevFoot(head);
506 nextHead = foot->Head;
507 if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
509 Uint size = nextHead->Size + head->Size;
510 // Inexact fit, split things up
513 // TODO: Handle splitting of downwards blocks
514 Warning("[Heap ] TODO: Space efficient realloc when new size is smaller");
518 Log_Debug("Heap", "realloc expand down %p (0x%x to 0x%x), returning to %s:%i",
519 head->Data, head->Size, newSize, File, Line);
524 // Set 1st (new/lower) header
525 nextHead->Magic = MAGIC_USED;
526 nextHead->Size = newSize;
527 nextHead->File = File;
528 nextHead->Line = Line;
529 nextHead->ValidSize = __size;
530 // Get 2nd (old) footer
531 foot = Heap_ThisFoot(nextHead);
532 foot->Head = nextHead;
533 // Save old data size
534 oldDataSize = head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead);
539 memmove(nextHead->Data, __ptr, oldDataSize);
541 return nextHead->Data;
545 nextHead = Heap_Allocate( File, Line, __size );
546 if(!nextHead) return NULL;
548 nextHead->File = File;
549 nextHead->Line = Line;
550 nextHead->ValidSize = __size;
552 ASSERTC(head->Size, <, nextHead->Size);
553 ASSERTC(__ptr, ==, head->Data);
557 head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead)
563 return nextHead->Data;
567 * \fn void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
568 * \brief Allocate and Zero a buffer in memory
569 * \param File Allocating file
570 * \param Line Line of allocation
571 * \param Bytes Size of the allocation
573 void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
575 void *ret = Heap_Allocate(File, Line, Bytes);
576 if(ret == NULL) return NULL;
578 memset( ret, 0, Bytes );
584 * \fn int Heap_IsHeapAddr(void *Ptr)
585 * \brief Checks if an address is a heap pointer
587 int Heap_IsHeapAddr(void *Ptr)
590 if((Uint)Ptr < (Uint)gHeapStart) return 0;
591 if((Uint)Ptr > (Uint)gHeapEnd) return 0;
592 if((Uint)Ptr & (sizeof(Uint)-1)) return 0;
594 head = (void*)( (Uint)Ptr - sizeof(tHeapHead) );
595 if(head->Magic != MAGIC_USED && head->Magic != MAGIC_FREE)
601 int Heap_int_ApplyWatchpont(void *Word, bool enabled)
604 static void *active_wps[4];
606 for( dr = 2; dr < 4; dr ++ )
608 if( (enabled && active_wps[dr] == NULL) || active_wps[dr] == Word)
616 active_wps[dr] = Word;
619 //case 0: ASM("mov %0, %%dr0" : : "r" (Word)); break;
620 //case 1: ASM("mov %0, %%dr1" : : "r" (Word)); break;
621 case 2: ASM("mov %0, %%dr2" : : "r" (Word)); break;
622 case 3: ASM("mov %0, %%dr3" : : "r" (Word)); break;
623 default: ASSERTC(dr,<,4); return 1;
628 active_wps[dr] = NULL;
631 ASM("MOV %%dr7, %0" : "=r" (dr7flag));
632 dr7flag &= ~(0x2 << (dr*2));
633 dr7flag &= ~(0xF000 << (dr*4));
635 dr7flag |= 0x2 << (dr*2);
636 dr7flag |= 0xD000 << (dr*4); // 4 bytes, write
637 Debug("Heap_int_ApplyWatchpont: Watchpoint #%i %p ENABLED", dr, Word);
640 Debug("Heap_int_ApplyWatchpont: Watchpoint #%i %p disabled", dr, Word);
642 ASM("MOV %0, %%dr7" : : "r" (dr7flag));
649 void Heap_int_WatchBlock(tHeapHead *Head, bool Enabled)
652 rv = Heap_int_ApplyWatchpont( &Head->Size, Enabled );
653 //rv = Heap_int_ApplyWatchpont( &Head->Magic, Enabled );
654 rv = rv + Heap_int_ApplyWatchpont( &Heap_ThisFoot(Head)->Head, Enabled );
656 Warning("Can't apply watch on %p", Head);
660 int Heap_WatchBlock(void *Ptr)
662 //Heap_int_ApplyWatchpont();
664 if((Uint)Ptr < (Uint)gHeapStart) return 0;
665 if((Uint)Ptr > (Uint)gHeapEnd) return 0;
666 if((Uint)Ptr & (sizeof(Uint)-1)) return 0;
668 head = (tHeapHead*)Ptr - 1;
670 Heap_int_WatchBlock( head, true );
677 void Heap_Validate(void)
679 // Call dump non-verbosely.
680 // - If a heap error is detected, it'll print
681 Heap_ValidateDump(0);
686 Heap_ValidateDump(1);
689 void Heap_ValidateDump(int bVerbose)
691 tHeapHead *head, *badHead;
692 tHeapFoot *foot = NULL;
693 static int in_heap_dump;
695 if( in_heap_dump ) return;
700 while( (Uint)head < (Uint)gHeapEnd )
702 foot = Heap_ThisFoot(head);
706 Log_Log("Heap", "%p (%P): 0x%08x (%i) %4C",
707 head, MM_GetPhysAddr(head), head->Size, head->ValidSize, &head->Magic);
708 Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
710 Log_Log("Heap", "%sowned by %s:%i",
711 (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
715 // Sanity Check Header
716 if(head->Size == 0) {
717 Log_Warning("Heap", "HALTED - Size is zero");
720 if(head->Size & (MIN_SIZE-1)) {
721 Log_Warning("Heap", "HALTED - Size is malaligned");
724 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
725 Log_Warning("Heap", "HALTED - Head Magic is Bad");
730 if(foot->Magic != MAGIC_FOOT) {
731 Log_Warning("Heap", "HALTED - Foot Magic is Bad");
734 if(head != foot->Head) {
735 Log_Warning("Heap", "HALTED - Footer backlink is invalid");
744 // All OK? Go to next
745 head = foot->NextHead;
748 // If the heap is valid, ok!
749 if( (tVAddr)head == (tVAddr)gHeapEnd ) {
754 // Check for a bad return
755 if( (tVAddr)head >= (tVAddr)gHeapEnd ) {
760 // If not verbose, we need to dump the failing block
763 Log_Log("Heap", "%p (%P): 0x%08lx %i %4C",
764 head, MM_GetPhysAddr(head), head->Size, head->ValidSize, &head->Magic);
766 Log_Log("Heap", "Foot %p = {Head:%p,Magic:%4C}", foot, foot->Head, &foot->Magic);
768 Log_Log("Heap", "%sowned by %s:%i",
769 (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
777 foot = Heap_PrevFoot(gHeapEnd);
778 Log_Log("Heap", "==== Going Backwards ==== (from %p)", foot);
780 while( (tVAddr)head >= (tVAddr)badHead )
782 Log_Log("Heap", "%p (%P): 0x%08lx %i %4C",
783 head, MM_GetPhysAddr(head), head->Size, head->ValidSize, &head->Magic);
784 Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
786 Log_Log("Heap", "%sowned by %s:%i",
787 (head->Magic!=MAGIC_USED?"was ":""),
788 head->File, head->Line);
791 // Sanity Check Header
792 if(head->Size == 0) {
793 Log_Warning("Heap", "HALTED - Size is zero");
796 if(head->Size & (MIN_SIZE-1)) {
797 Log_Warning("Heap", " - Size is malaligned (&0x%x)", ~(MIN_SIZE-1));
800 if(head->Magic != MAGIC_FREE && head->Magic != MAGIC_USED) {
801 Log_Warning("Heap", "HALTED - Head Magic is Bad");
806 if(foot->Magic != MAGIC_FOOT) {
807 Log_Warning("Heap", "HALTED - Foot Magic is Bad");
810 if(head != foot->Head) {
811 Log_Warning("Heap", "HALTED - Footer backlink is invalid");
815 if(head == badHead) break;
817 foot = Heap_PrevFoot(head);
819 Log_Debug("Heap", "head=%p", head);
822 Panic("Heap_Dump - Heap is corrupted, kernel panic! (%p)", badHead);
825 void Heap_Stats(void)
831 int maxAlloc=0, minAlloc=-1;
832 int avgAlloc, frag, overhead;
834 for( tHeapHead *head = gHeapStart; head < gHeapEnd; head = Heap_NextHead(head) )
837 totalBytes += head->Size;
838 if( head->Magic == MAGIC_FREE )
841 freeBytes += head->Size;
843 else if( head->Magic == MAGIC_USED) {
844 if(maxAlloc < head->Size) maxAlloc = head->Size;
845 if(minAlloc == -1 || minAlloc > head->Size)
846 minAlloc = head->Size;
849 Log_Warning("Heap", "Magic on %p invalid, skipping remainder of heap", head);
853 // Print the block info?
855 if( head->Magic == MAGIC_FREE )
856 Log_Debug("Heap", "%p (%P) - 0x%x free",
857 head->Data, MM_GetPhysAddr(&head->Data), head->Size);
859 Log_Debug("Heap", "%p (%P) - 0x%x (%i) Owned by %s:%i (%lli ms old)",
860 head->Data, MM_GetPhysAddr(&head->Data), head->Size,
861 head->ValidSize, head->File, head->Line,
862 now() - head->AllocateTime
867 Log_Log("Heap", "%i blocks (0x%x bytes)", nBlocks, totalBytes);
868 Log_Log("Heap", "%i free blocks (0x%x bytes)", nFree, freeBytes);
870 frag = (nFree-1)*10000/nBlocks;
873 Log_Log("Heap", "%i.%02i%% Heap Fragmentation", frag/100, frag%100);
877 avgAlloc = (totalBytes-freeBytes)/(nBlocks-nFree);
879 overhead = (sizeof(tHeapFoot)+sizeof(tHeapHead))*10000/avgAlloc;
882 Log_Log("Heap", "Average allocation: %i bytes, Average Overhead: %i.%02i%%",
883 avgAlloc, overhead/100, overhead%100
885 Log_Log("Heap", "Smallest Block: %i bytes, Largest: %i bytes",
888 // Scan and get distribution
895 } sizeCounts[nBlocks];
898 memset(sizeCounts, 0, nBlocks*sizeof(sizeCounts[0]));
900 for(tHeapHead *head = gHeapStart; head < gHeapEnd; head = (void*)( (tVAddr)head + head->Size ) )
902 for( i = 0; i < nBlocks; i ++ ) {
903 if( sizeCounts[i].Size == 0 )
905 if( sizeCounts[i].Size == head->Size )
908 // Should never reach this part (in a non-concurrent case)
909 if( i == nBlocks ) continue;
910 sizeCounts[i].Size = head->Size;
911 sizeCounts[i].Count ++;
913 Log("Heap_Stats: %i %p - 0x%x bytes (%s) (%i)", nBlocks, head,
914 head->Size, (head->Magic==MAGIC_FREE?"FREE":"used"), i
916 Log("Heap_Stats: sizeCounts[%i] = {Size:0x%x, Count: %i}", i,
917 sizeCounts[i].Size, sizeCounts[i].Count);
921 for( i = 0; i < nBlocks && sizeCounts[i].Count; i ++ )
923 Log("Heap_Stats: 0x%x - %i blocks",
924 sizeCounts[i].Size, sizeCounts[i].Count
932 EXPORT(Heap_Allocate);
933 EXPORT(Heap_AllocateZero);
934 EXPORT(Heap_Reallocate);
935 EXPORT(Heap_Deallocate);
936 EXPORT(Heap_IsHeapAddr);