From 5dbd89d2aaa03659bed6f31f5b050fcc11c1c502 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 26 Mar 2014 03:01:16 +0100 Subject: [PATCH] Delay heap initialization until first heap expansion. --- kernel/kernel.cpp | 3 --- libc/include/malloc.h | 4 +--- libc/init/init.cpp | 3 --- libc/stdlib/heap.cpp | 36 ++++++++++-------------------------- 4 files changed, 11 insertions(+), 35 deletions(-) diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index 3f5cfc41..6e61364e 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -295,9 +295,6 @@ extern "C" void KernelInit(unsigned long magic, multiboot_info_t* bootinfo) // Initialize the interrupt handler table and enable interrupts. Interrupt::Init(); - // Initialize the kernel heap. - _init_heap(); - // Load the kernel symbols if provided by the bootloader. do if ( bootinfo->flags & MULTIBOOT_INFO_ELF_SHDR ) { diff --git a/libc/include/malloc.h b/libc/include/malloc.h index 80bc06b7..2006ead4 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2012. + Copyright(C) Jonas 'Sortie' Termansen 2012, 2014. This file is part of the Sortix C Library. @@ -29,8 +29,6 @@ __BEGIN_DECLS -void _init_heap(); - __END_DECLS #endif diff --git a/libc/init/init.cpp b/libc/init/init.cpp index 44a7eadc..f9d42345 100644 --- a/libc/init/init.cpp +++ b/libc/init/init.cpp @@ -55,9 +55,6 @@ extern "C" void initialize_standard_library(int argc, char* argv[]) // Initialize pthreads and stuff like thread-local storage. pthread_initialize(); - // Initialize the dynamic heap. - _init_heap(); - // Initialize stdio. init_stdio(); diff --git a/libc/stdlib/heap.cpp b/libc/stdlib/heap.cpp index 36097a78..af5dd0ec 100644 --- a/libc/stdlib/heap.cpp +++ b/libc/stdlib/heap.cpp @@ -197,9 +197,9 @@ struct Chunk; struct Trailer; #if __STDC_HOSTED__ -pthread_mutex_t heaplock; +pthread_mutex_t heaplock = PTHREAD_MUTEX_INITIALIZER; #elif defined(__is_sortix_kernel) -Sortix::kthread_mutex_t heaplock; +Sortix::kthread_mutex_t heaplock = Sortix::KTHREAD_MUTEX_INITIALIZER; #endif // The location where the heap originally grows from. @@ -212,14 +212,14 @@ static uintptr_t wilderness; #endif // How many bytes remain in the wilderness. -size_t wildernesssize; +size_t wildernesssize = 0; // How many bytes are the heap allow to grow to (including wilderness). -size_t heapmaxsize; +size_t heapmaxsize = SIZE_MAX; // How many bytes are currently used for chunks in the heap, which // excludes the wilderness. -size_t heapsize; +size_t heapsize = 0; // bins[N] contain a linked list of chunks that are at least 2^(N+1) // bytes, but less than 2^(N+2) bytes. By selecting the proper bin in @@ -227,7 +227,7 @@ size_t heapsize; Chunk* bins[NUMBINS]; // Bit N is set if bin[N] contains a chunk. -size_t bincontainschunks; +size_t bincontainschunks = 0; static bool IsGoodHeapPointer(void* ptr, size_t size) { @@ -397,26 +397,6 @@ static bool ValidateHeap() return true; } -// -// This is where the actual memory allocation algorithm starts. -// - -extern "C" void _init_heap() -{ - heapstart = GetHeapStart(); - heapmaxsize = SIZE_MAX; - heapsize = 0; - wilderness = heapstart; - wildernesssize = 0; - for ( size_t i = 0; i < NUMBINS; i++ ) { bins[i] = NULL; } - bincontainschunks = 0; -#if __STDC_HOSTED__ - heaplock = PTHREAD_MUTEX_INITIALIZER; -#elif defined(__is_sortix_kernel) - heaplock = Sortix::KTHREAD_MUTEX_INITIALIZER; -#endif -} - // Attempts to expand the wilderness such that it contains at least // bytesneeded bytes. This is done by mapping new pages onto into the // virtual address-space. @@ -424,6 +404,10 @@ static bool ExpandWilderness(size_t bytesneeded) { if ( bytesneeded <= wildernesssize ) { return true; } + // Delayed initialization of the heap. + if ( heapstart == 0 && wilderness == 0 && !heapsize ) + heapstart = wilderness = GetHeapStart(); + bytesneeded -= wildernesssize; // Align the increase on page boundaries.