From 3c2819aaca41f37a54bf9089adba43cb5c49b598 Mon Sep 17 00:00:00 2001 From: Steven Eker Date: Fri, 13 Dec 2024 08:17:53 -0800 Subject: [PATCH] Release unused address space (#1186) Use munmap() to release unused part of mmap()ed address space in arena::initialize_semispace() to reduce the risk of running out of mmap()able address space in a multithreaded situation. --- runtime/alloc/arena.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/runtime/alloc/arena.cpp b/runtime/alloc/arena.cpp index d10bcaad9..fbfd7e527 100644 --- a/runtime/alloc/arena.cpp +++ b/runtime/alloc/arena.cpp @@ -41,15 +41,31 @@ void arena::initialize_semispace() { abort(); } // + // std::align() may modify addr and request. + // + auto *start_block = reinterpret_cast(addr); + auto *end_block = start_block + request; + // // We allocated 2 * HYPERBLOCK_SIZE worth of address space but we're only going to use 1, aligned on a // HYPERBLOCK_SIZE boundry. This is so we can get end of the hyperblock by setting the low bits of any // address within the space to 1. - // We don't worry about unused address space either side of our aligned address space because there will be no - // memory mapped to it. // current_addr_ptr = reinterpret_cast( std::align(HYPERBLOCK_SIZE, HYPERBLOCK_SIZE, addr, request)); // + // Release any unused address space at the start of the mmap()ed block. + // + if (size_t front_slop = current_addr_ptr - start_block) { + munmap(start_block, front_slop); + } + // + // Release any unused address space at the end of the mmap()ed block. + // + auto *end_aligned = current_addr_ptr + HYPERBLOCK_SIZE; + if (size_t back_slop = end_block - end_aligned) { + munmap(end_aligned, back_slop); + } + // // We put a semispace id in the last byte of the hyperblock so we can identify which semispace an address // belongs to by setting the low bits to 1 to access this id. //