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

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