diff --git a/include/runtime/arena.h b/include/runtime/arena.h index afbfca21b..826db686c 100644 --- a/include/runtime/arena.h +++ b/include/runtime/arena.h @@ -19,12 +19,11 @@ size_t const HYPERBLOCK_SIZE = (size_t)BLOCK_SIZE * 1024 * 1024; class arena { public: arena(char id) - : allocation_semispace_id(id) { - initialize_semispace(); - } + : allocation_semispace_id(id) { } ~arena() { - munmap(current_addr_ptr, HYPERBLOCK_SIZE); + if (current_addr_ptr) + munmap(current_addr_ptr, HYPERBLOCK_SIZE); if (collection_addr_ptr) munmap(collection_addr_ptr, HYPERBLOCK_SIZE); } @@ -111,11 +110,12 @@ class arena { // // Current semispace where allocations are being made. // - char *current_addr_ptr; // pointer to start of current address space - char *allocation_ptr; // next available location in current semispace - char *tripwire; // allocating past this triggers slow allocation - mutable size_t - num_blocks; // notional number of BLOCK_SIZE blocks in current semispace + char *current_addr_ptr = nullptr; // pointer to start of current address space + char *allocation_ptr + = nullptr; // next available location in current semispace + char *tripwire = nullptr; // allocating past this triggers slow allocation + mutable size_t num_blocks + = 0; // notional number of BLOCK_SIZE blocks in current semispace char allocation_semispace_id; // id of current semispace // // Semispace where allocations will be made during and after garbage collect. @@ -155,18 +155,25 @@ extern thread_local bool time_for_collection; size_t get_gc_threshold(); inline void *arena::kore_arena_alloc(size_t requested) { - if (allocation_ptr + requested >= tripwire) { - // - // We got close to or past the last location accessed in this address range so far, - // depending on the requested size and tripwire setting. This triggers a garbage - // collect when allowed. + if (tripwire == nullptr) { // - time_for_collection = true; + // Semispace not yet initialized. // - // We move the tripwire to 1 past the end of our hyperblock so that we have - // a well defined comparison that will always be false until the next arena swap. - // - tripwire = current_addr_ptr + HYPERBLOCK_SIZE; + initialize_semispace(); + } else if (allocation_ptr + requested >= tripwire) { + { + // + // We got close to or past the last location accessed in this address range so far, + // depending on the requested size and tripwire setting. This triggers a garbage + // collect when allowed. + // + time_for_collection = true; + // + // We move the tripwire to 1 past the end of our hyperblock so that we have + // a well defined comparison that will always be false until the next arena swap. + // + tripwire = current_addr_ptr + HYPERBLOCK_SIZE; + } } void *result = allocation_ptr; allocation_ptr += requested;