[libc-commits] [libc] [libc] add shrink in-place support for reallocations (PR #200272)

Daniel Thornburgh via libc-commits libc-commits at lists.llvm.org
Mon Jun 1 15:55:39 PDT 2026


================
@@ -158,6 +160,44 @@ LIBC_INLINE void FreeListHeap::free(void *ptr) {
   free_store.insert(block);
 }
 
+LIBC_INLINE bool FreeListHeap::shrink_in_place(Block *block, size_t size) {
+  size_t min_outer_size = Block::outer_size(cpp::max(size, sizeof(size_t)));
+  uintptr_t next_block_start = Block::next_possible_block_start(
+      reinterpret_cast<uintptr_t>(block) + min_outer_size, Block::MIN_ALIGN);
+  size_t new_outer_size = next_block_start - reinterpret_cast<uintptr_t>(block);
+  // only split the block if the trailing part can be inserted into freelist
+  if (block->outer_size() >= new_outer_size &&
----------------
mysterymath wrote:

I'd still wager we should split the block even if it cannot be inserted into the freelist. There's still the possibility that the rump block could be coalesced with the next block if it is freed.

https://github.com/llvm/llvm-project/pull/200272


More information about the libc-commits mailing list