#define WARNINGS 1
#define DEBUG_TRACE 0
+#define VERBOSE_DUMP 0
// === CONSTANTS ===
#define HEAP_INIT_SIZE 0x8000 // 32 KiB
* \param Line Source line
* \param Bytes Size of region to allocate
*/
-void *Heap_Allocate(const char *File, int Line, size_t Bytes)
+void *Heap_Allocate(const char *File, int Line, size_t __Bytes)
{
tHeapHead *head, *newhead;
tHeapFoot *foot, *newfoot;
tHeapHead *best = NULL;
Uint bestSize = 0; // Speed hack
+ size_t Bytes;
// Get required size
#if POW2_SIZES
- Bytes = Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot);
- Bytes = 1UUL << LOG2(Bytes);
+ Bytes = __Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot);
+ Bytes = 1UUL << LOG2(__Bytes);
#else
- Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + MIN_SIZE-1) & ~(MIN_SIZE-1);
+ Bytes = (__Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + MIN_SIZE-1) & ~(MIN_SIZE-1);
#endif
// Lock Heap
newhead->Magic = MAGIC_FREE;
foot->Head = newhead; // Update backlink in old footer
best->Size = Bytes; // Update size in old header
+ best->ValidSize = __Bytes;
best->Magic = MAGIC_USED; // Mark block as used
best->File = File;
best->Line = Line;
// Mark as free
head->Magic = MAGIC_FREE;
- head->File = NULL;
- head->Line = 0;
+ //head->File = NULL;
+ //head->Line = 0;
head->ValidSize = 0;
// Merge blocks
Heap_Merge( head );
*/
void *Heap_Reallocate(const char *File, int Line, void *__ptr, size_t __size)
{
- tHeapHead *head = (void*)( (Uint)__ptr-8 );
+ tHeapHead *head = (void*)( (Uint)__ptr-sizeof(tHeapHead) );
tHeapHead *nextHead;
tHeapFoot *foot;
Uint newSize = (__size + sizeof(tHeapFoot)+sizeof(tHeapHead)+MIN_SIZE-1)&~(MIN_SIZE-1);
}
/**
- * \fn void *Heap_AllocateZero(const char *File, int Line, size_t size)
+ * \fn void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
* \brief Allocate and Zero a buffer in memory
* \param File Allocating file
* \param Line Line of allocation
- * \param size Size of the allocation
+ * \param Bytes Size of the allocation
*/
-void *Heap_AllocateZero(const char *File, int Line, size_t size)
+void *Heap_AllocateZero(const char *File, int Line, size_t Bytes)
{
- void *ret = Heap_Allocate(File, Line, size);
+ void *ret = Heap_Allocate(File, Line, Bytes);
if(ret == NULL) return NULL;
- memset( ret, 0, size );
+ memset( ret, 0, Bytes );
return ret;
}
return 1;
}
+/**
+ */
+void Heap_Validate(void)
+{
+ Heap_Dump();
+}
+
#if WARNINGS
void Heap_Dump(void)
{
tHeapHead *head, *badHead;
- tHeapFoot *foot;
+ tHeapFoot *foot = NULL;
head = gHeapStart;
while( (Uint)head < (Uint)gHeapEnd )
{
foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
+ #if VERBOSE_DUMP
Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C",
head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
Log_Log("Heap", "%sowned by %s:%i",
(head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
}
- Log_Log("Heap", "");
+ #endif
// Sanity Check Header
if(head->Size == 0) {
break;
}
+ #if VERBOSE_DUMP
+ Log_Log("Heap", "");
+ #endif
+
// All OK? Go to next
head = foot->NextHead;
}
+ // If the heap is valid, ok!
+ if( (tVAddr)head == (tVAddr)gHeapEnd )
+ return ;
+
// Check for a bad return
if( (tVAddr)head >= (tVAddr)gHeapEnd )
return ;
+
+ #if !VERBOSE_DUMP
+ Log_Log("Heap", "%p (0x%llx): 0x%08lx (%i) %4C",
+ head, MM_GetPhysAddr((Uint)head), head->Size, head->ValidSize, &head->Magic);
+ Log_Log("Heap", "%p %4C", foot->Head, &foot->Magic);
+ if(head->File) {
+ Log_Log("Heap", "%sowned by %s:%i",
+ (head->Magic==MAGIC_FREE?"was ":""), head->File, head->Line);
+ }
+ Log_Log("Heap", "");
+ #endif
- badHead = head;
- Log_Log("Heap", "==== Going Backwards ==== (from %p)", badHead);
+ badHead = head;
// Work backwards
foot = (void*)( (tVAddr)gHeapEnd - sizeof(tHeapFoot) );
+ Log_Log("Heap", "==== Going Backwards ==== (from %p)", foot);
head = foot->Head;
while( (tVAddr)head >= (tVAddr)badHead )
{
head = foot->Head;
Log_Debug("Heap", "head=%p", head);
}
+
+ Panic("Heap_Dump - Heap is corrupted, kernel panic!");
}
#endif