feat(runtime): eager heap initialization

This commit is contained in:
Leonardo de Moura 2019-04-25 17:01:01 -07:00
parent 9a39eb254a
commit 5c7849a869
4 changed files with 13 additions and 5 deletions

View file

@ -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];

View file

@ -7,6 +7,7 @@ Author: Leonardo de Moura
#include<cstddef>
#pragma once
namespace lean {
void init_thread_heap();
void * alloc(size_t sz);
void dealloc(void * o, size_t sz);
void initialize_alloc();

View file

@ -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<rc_type *>(r) = 1;
return static_cast<char *>(r) + sizeof(rc_type);
}

View file

@ -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<void()>;
static void thread_main(void * p) {
init_thread_heap();
std::unique_ptr<runnable> f;
f.reset(reinterpret_cast<runnable *>(p));