[all-commits] [llvm/llvm-project] 8866fa: [libc][malloc] Reuse the prev_ field for allocated...

Daniel Thornburgh via All-commits all-commits at lists.llvm.org
Mon Aug 5 11:52:13 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 8866fa15de2cf17bae3fe0b273d6b51daa9e37fd
      https://github.com/llvm/llvm-project/commit/8866fa15de2cf17bae3fe0b273d6b51daa9e37fd
  Author: Daniel Thornburgh <dthorn at google.com>
  Date:   2024-08-05 (Mon, 05 Aug 2024)

  Changed paths:
    M libc/src/__support/block.h
    M libc/src/__support/freelist_heap.h
    M libc/test/src/__support/block_test.cpp
    M libc/test/src/__support/freelist_heap_test.cpp
    M libc/test/src/__support/freelist_malloc_test.cpp

  Log Message:
  -----------
  [libc][malloc] Reuse the prev_ field for allocated blocks (#101265)

This applies a standard trick from Knuth for storing boundary tags with
only one word of overhead for allocated blocks. The prev_ block is now
only valid if the previous block is free.

This is safe, since only coalescing with a free node requires walking
the blocks backwards. To allow determining whether it's safe to traverse
backwards, the used flag is changed to a prev_free flag. Since it's
still possible to unconditionally traverse forward, the prev_free flag
for the next block can be used wherever the old used flag is, so long as
there is always a next block.

To ensure there is always a next block, a sentinel last block is added
at the end of the range of blocks. Due to the above, this costs only a
single word per heap. This sentinel essentially just stores whether the
last real block of the heap is free. The sentinel is always considered
used and to have a zero inner size.

This completes the block optimizations needed to address #98086. The
block structure should now be size-competitive with dlmalloc, although
there are still a couple of broader fragmentation concerns to address.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list