bootloader heap: Actually implement DEBUG_ALLOCATIONS.

The old define was really "check heap integrity", so it's split off.
In the userland test harness, this takes the total time spent in
the allocator from around 1ms to 14ms for all the allocations used
during a boot with "nightly" profile, so it seems acceptable to
enable it under KDEBUG.

Tested with both BIOS and EFI bootloaders, still works.
This commit is contained in:
Augustin Cavalier
2025-01-07 17:30:28 -05:00
parent 0f843fe15a
commit 7137fc03b2
2 changed files with 14 additions and 2 deletions
@@ -266,6 +266,9 @@ public:
#ifdef DEBUG_MAX_HEAP_USAGE #ifdef DEBUG_MAX_HEAP_USAGE
fMaxHeapUsage = std::max(fMaxHeapUsage, fMaxHeapSize - fAvailable); fMaxHeapUsage = std::max(fMaxHeapUsage, fMaxHeapSize - fAvailable);
#endif #endif
#ifdef DEBUG_ALLOCATIONS
memset(allocated, 0xcc, chunk->Size());
#endif
return allocated; return allocated;
} }
@@ -306,7 +309,7 @@ public:
FreeChunk* freedChunk = FreeChunk::SetToAllocated(allocated); FreeChunk* freedChunk = FreeChunk::SetToAllocated(allocated);
#ifdef DEBUG_ALLOCATIONS #ifdef DEBUG_VALIDATE_HEAP_ON_FREE
if (freedChunk->Size() > (fMaxHeapSize - fAvailable)) { if (freedChunk->Size() > (fMaxHeapSize - fAvailable)) {
panic("freed chunk %p clobbered (%#zx)!\n", freedChunk, panic("freed chunk %p clobbered (%#zx)!\n", freedChunk,
freedChunk->Size()); freedChunk->Size());
@@ -321,6 +324,10 @@ public:
} }
} }
#endif #endif
#ifdef DEBUG_ALLOCATIONS
for (uint32 i = 0; i < (freedChunk->Size() / 4); i++)
((uint32*)allocated)[i] = 0xdeadbeef;
#endif
// try to join the new free chunk with an existing one // try to join the new free chunk with an existing one
// it may be joined with up to two chunks // it may be joined with up to two chunks
+6 -1
View File
@@ -16,11 +16,16 @@
#include <util/OpenHashTable.h> #include <util/OpenHashTable.h>
#if KDEBUG
#define DEBUG_ALLOCATIONS #define DEBUG_ALLOCATIONS
// if defined, freed memory is filled with 0xcc // if defined, allocated memory is filled with 0xcc and freed memory with 0xdeadbeef
#define DEBUG_MAX_HEAP_USAGE #define DEBUG_MAX_HEAP_USAGE
// if defined, the maximum heap usage is determined and printed before // if defined, the maximum heap usage is determined and printed before
// entering the kernel // entering the kernel
#define DEBUG_VALIDATE_HEAP_ON_FREE
// if defined, the heap integrity is checked on every free
// (must have DEBUG_MAX_HEAP_USAGE defined also)
#endif
#include <util/SimpleAllocator.h> #include <util/SimpleAllocator.h>