kernel: Overhaul debug heaps system.

Instead of the kernel heap being a compile-time parameter, make it
a runtime parameter (if DEBUG_HEAPS is specified, at least; otherwise
we're hardwired to the slab heap as before.)

This is similar to what's done in userland: there LD_PRELOAD and
MALLOC_DEBUG control the heaps, here "kernel_malloc" and then
"guarded_heap_options" kernel settings control the heap.

While at it, apply fixes to the guarded and debug heaps to ensure
a minimum alignment of sizeof(void*), if no specific alignment
is requested.

Tested with a lot of build configurations (DEBUG_HEAPS 0, 1;
DEBUG_HEAPS_DEFAULT slab, guarded; USE_DEBUG_HEAPS_FOR_OBJECT_CACHE
0, 1) and with various parameters specified in the bootloader.
This commit is contained in:
Augustin Cavalier
2026-03-23 23:01:26 -04:00
parent 4fd16356e0
commit a277043c1f
12 changed files with 580 additions and 386 deletions
+2 -27
View File
@@ -8,37 +8,12 @@
#ifndef _KERNEL_HEAP_H
#define _KERNEL_HEAP_H
#include <OS.h>
#include "kernel_debug_config.h"
#if USE_GUARDED_HEAP_FOR_MALLOC && USE_GUARDED_HEAP_FOR_OBJECT_CACHE
// This requires a lot of up-front memory to boot at all...
#define INITIAL_HEAP_SIZE 128 * 1024 * 1024
// ... and a lot of reserves to keep running.
#define HEAP_GROW_SIZE 128 * 1024 * 1024
#elif USE_GUARDED_HEAP_FOR_MALLOC
#define INITIAL_HEAP_SIZE 32 * 1024 * 1024
#define HEAP_GROW_SIZE 32 * 1024 * 1024
#else
// allocate 16MB initial heap for the kernel
#define INITIAL_HEAP_SIZE 16 * 1024 * 1024
// grow by another 4MB each time the heap runs out of memory
#define HEAP_GROW_SIZE 4 * 1024 * 1024
// allocate a dedicated 1MB area for dynamic growing
#define HEAP_DEDICATED_GROW_SIZE 1 * 1024 * 1024
// use areas for allocations bigger than 1MB
#define HEAP_AREA_USE_THRESHOLD 1 * 1024 * 1024
#endif
// allocation/deallocation flags for {malloc,free}_etc()
#define HEAP_DONT_WAIT_FOR_MEMORY 0x01
#define HEAP_DONT_LOCK_KERNEL_SPACE 0x02
@@ -58,7 +33,7 @@ void* memalign(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
void deferred_free(void* block);
status_t heap_init(addr_t heapBase, size_t heapSize);
status_t heap_init(struct kernel_args* args);
status_t heap_init_post_area();
status_t heap_init_post_sem();
status_t heap_init_post_thread();