diff --git a/headers/private/kernel/boot/heap.h b/headers/private/kernel/boot/heap.h index fad877baa6..a3d3dca01d 100644 --- a/headers/private/kernel/boot/heap.h +++ b/headers/private/kernel/boot/heap.h @@ -13,6 +13,7 @@ extern "C" { #endif extern void heap_release(struct stage2_args *args); +extern void heap_print_statistics(); extern status_t heap_init(struct stage2_args *args); #ifdef __cplusplus diff --git a/src/system/boot/loader/heap.cpp b/src/system/boot/loader/heap.cpp index 970ce7f272..013280aad6 100644 --- a/src/system/boot/loader/heap.cpp +++ b/src/system/boot/loader/heap.cpp @@ -8,6 +8,8 @@ #include #include +#include + #ifdef HEAP_TEST # include #endif @@ -41,6 +43,9 @@ #define DEBUG_ALLOCATIONS // if defined, freed memory is filled with 0xcc +#define DEBUG_MAX_HEAP_USAGE + // if defined, the maximum heap usage is determined and printed before + // entering the kernel class FreeChunk { public: @@ -72,7 +77,7 @@ const static uint32 kAlignment = 4; // all memory chunks will be a multiple of this static void* sHeapBase; -static uint32 /*sHeapSize,*/ sMaxHeapSize, sAvailable; +static uint32 /*sHeapSize,*/ sMaxHeapSize, sAvailable, sMaxHeapUsage; static FreeChunk sFreeAnchor; @@ -214,6 +219,15 @@ heap_release(stage2_args* args) } +void +heap_print_statistics() +{ +#ifdef DEBUG_MAX_HEAP_USAGE + dprintf("maximum boot loader heap usage: %" B_PRIu32 "\n", sMaxHeapUsage); +#endif +} + + status_t heap_init(stage2_args* args) { @@ -225,6 +239,9 @@ heap_init(stage2_args* args) sHeapBase = base; sMaxHeapSize = (uint8*)top - (uint8*)base; sAvailable = sMaxHeapSize - FreeChunk::NextOffset(); +#ifdef DEBUG_MAX_HEAP_USAGE + sMaxHeapUsage = sMaxHeapSize - sAvailable; +#endif // declare the whole heap as one chunk, and add it // to the free list @@ -323,6 +340,9 @@ malloc(size_t size) } sAvailable -= size + sizeof(uint32); +#ifdef DEBUG_MAX_HEAP_USAGE + sMaxHeapUsage = std::max(sMaxHeapUsage, sMaxHeapSize - sAvailable); +#endif TRACE("malloc(%lu) -> %p\n", size, chunk->AllocatedAddress()); return chunk->AllocatedAddress(); @@ -394,6 +414,9 @@ free(void* allocated) #endif sAvailable += freedChunk->CompleteSize(); +#ifdef DEBUG_MAX_HEAP_USAGE + sMaxHeapUsage = std::max(sMaxHeapUsage, sMaxHeapSize - sAvailable); +#endif // try to join the new free chunk with an existing one // it may be joined with up to two chunks diff --git a/src/system/boot/loader/main.cpp b/src/system/boot/loader/main.cpp index 52c1d43e30..01b4826ade 100644 --- a/src/system/boot/loader/main.cpp +++ b/src/system/boot/loader/main.cpp @@ -133,6 +133,7 @@ main(stage2_args *args) gKernelArgs.boot_volume_size = gBootVolume.ContentSize(); // ToDo: cleanup, heap_release() etc. + heap_print_statistics(); platform_start_kernel(); } }