[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:40 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 &&
+      block->outer_size() - new_outer_size >= FreeStore::MIN_OUTER_SIZE) {
+    // We must temporarily mark the block as free to allow splitting.
----------------
mysterymath wrote:

This comment is out of date with the change to `split()`; we are directly splitting the used block.

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


More information about the libc-commits mailing list