diff --git a/src/runtime/alloc.cpp b/src/runtime/alloc.cpp index 5009e8b81a..72e3279079 100644 --- a/src/runtime/alloc.cpp +++ b/src/runtime/alloc.cpp @@ -347,6 +347,7 @@ static void finalize_heap(void * _h) { } static void init_heap(bool main) { + lean_assert(g_heap == nullptr); g_heap = new heap(); g_heap->alloc_segment(); unsigned obj_size = LEAN_OBJECT_SIZE_DELTA; @@ -360,15 +361,19 @@ static void init_heap(bool main) { register_thread_finalizer(finalize_heap, g_heap); } +void init_thread_heap() { + init_heap(false); +} + void * alloc(size_t sz) { sz = align(sz, LEAN_OBJECT_SIZE_DELTA); LEAN_RUNTIME_STAT_CODE(g_num_alloc++); if (LEAN_UNLIKELY(sz > LEAN_MAX_SMALL_OBJECT_SIZE)) { - return malloc(sz); - } - if (LEAN_UNLIKELY(g_heap == nullptr)) { - init_heap(false); + void * r = malloc(sz); + if (r == nullptr) throw std::bad_alloc(); + return r; } + lean_assert(g_heap); LEAN_RUNTIME_STAT_CODE(g_num_small_alloc++); unsigned slot_idx = get_slot_idx(sz); page * p = g_heap->m_curr_page[slot_idx]; diff --git a/src/runtime/alloc.h b/src/runtime/alloc.h index d0276535c1..467b0acda9 100644 --- a/src/runtime/alloc.h +++ b/src/runtime/alloc.h @@ -7,6 +7,7 @@ Author: Leonardo de Moura #include #pragma once namespace lean { +void init_thread_heap(); void * alloc(size_t sz); void dealloc(void * o, size_t sz); void initialize_alloc(); diff --git a/src/runtime/object.cpp b/src/runtime/object.cpp index 69fa9b35e0..8dfbe39807 100644 --- a/src/runtime/object.cpp +++ b/src/runtime/object.cpp @@ -327,8 +327,8 @@ void * alloc_heap_object(size_t sz) { void * r = alloc(sizeof(rc_type) + sz); #else void * r = malloc(sizeof(rc_type) + sz); -#endif if (r == nullptr) throw std::bad_alloc(); +#endif *static_cast(r) = 1; return static_cast(r) + sizeof(rc_type); } diff --git a/src/runtime/thread.cpp b/src/runtime/thread.cpp index 1141f77367..1d55f95c30 100644 --- a/src/runtime/thread.cpp +++ b/src/runtime/thread.cpp @@ -15,6 +15,7 @@ Author: Leonardo de Moura #include "runtime/thread.h" #include "runtime/interrupt.h" #include "runtime/exception.h" +#include "runtime/alloc.h" #ifndef LEAN_DEFAULT_THREAD_STACK_SIZE #define LEAN_DEFAULT_THREAD_STACK_SIZE 8*1024*1024 // 8Mb @@ -44,6 +45,7 @@ void reset_thread_local() { using runnable = std::function; static void thread_main(void * p) { + init_thread_heap(); std::unique_ptr f; f.reset(reinterpret_cast(p));