[libc-commits] [libc] f9cf539 - [libc][malloc] Align blocks to max_align_t. (#100279)

via libc-commits libc-commits at lists.llvm.org
Tue Jul 23 17:16:44 PDT 2024


Author: Daniel Thornburgh
Date: 2024-07-23T17:16:41-07:00
New Revision: f9cf5393dc9bc5e01f53e7906e7bebc3f3f30382

URL: https://github.com/llvm/llvm-project/commit/f9cf5393dc9bc5e01f53e7906e7bebc3f3f30382
DIFF: https://github.com/llvm/llvm-project/commit/f9cf5393dc9bc5e01f53e7906e7bebc3f3f30382.diff

LOG: [libc][malloc] Align blocks to max_align_t. (#100279)

Since there are two offsets from block start to usable area, this
ensures that the usable area is maximally aligned, so long as the offset
type size is no less than half the max alignment. This is true on at
least typical 32-bit and 64-bit targets.

Previously, there was a roughly 50-50 chance a given block's usable area
would be misaligned for a malloc on a 32-bit system. The half that were
misaligned would require at least one block of additional padding,
costing 12 bytes. With this change, the only cost is 0-4 bytes at the
beginning of the heap to reach an initial 8-byte alignment.

See #98096

Added: 
    

Modified: 
    libc/src/__support/block.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/block.h b/libc/src/__support/block.h
index af3ce181f1c99..86cb4bd7ad582 100644
--- a/libc/src/__support/block.h
+++ b/libc/src/__support/block.h
@@ -102,9 +102,12 @@ using cpp::optional;
 ///                       types can address more memory, but consume greater
 ///                       overhead.
 /// @tparam   kAlign      Sets the overall alignment for blocks. Minimum is
-///                       `alignof(OffsetType)` (the default). Larger values
-///                       cause greater overhead.
-template <typename OffsetType = uintptr_t, size_t kAlign = alignof(OffsetType)>
+///                       `alignof(OffsetType)`, but the default is max_align_t,
+///                       since the usable space will then already be
+///                       aligned to max_align_t if the size of OffsetType is no
+///                       less than half of max_align_t. Larger values cause
+///                       greater overhead.
+template <typename OffsetType = uintptr_t, size_t kAlign = alignof(max_align_t)>
 class Block {
   // Masks for the contents of the next_ field.
   static constexpr size_t USED_MASK = 1 << 0;


        


More information about the libc-commits mailing list