Skip to content

Commit bde7ae4

Browse files
committed
Add more usable space alignment assertions
1 parent 33fdf85 commit bde7ae4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

libc/src/__support/block.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,18 @@ class Block {
189189

190190
/// @returns A pointer to the usable space inside this block.
191191
///
192-
/// Unless specifically requested otherwise, this will be aligned to
193-
/// max_align_t.
192+
/// Aligned to some multiple of max_align_t.
194193
LIBC_INLINE cpp::byte *usable_space() {
195-
return reinterpret_cast<cpp::byte *>(this) + BLOCK_OVERHEAD;
194+
auto *s = reinterpret_cast<cpp::byte *>(this) + BLOCK_OVERHEAD;
195+
LIBC_ASSERT(reinterpret_cast<uintptr_t>(s) % alignof(max_align_t) == 0 &&
196+
"usable space must be aligned to a multiple of max_align_t");
197+
return s;
196198
}
197199
LIBC_INLINE const cpp::byte *usable_space() const {
198-
return reinterpret_cast<const cpp::byte *>(this) + BLOCK_OVERHEAD;
200+
const auto *s = reinterpret_cast<const cpp::byte *>(this) + BLOCK_OVERHEAD;
201+
LIBC_ASSERT(reinterpret_cast<uintptr_t>(s) % alignof(max_align_t) == 0 &&
202+
"usable space must be aligned to a multiple of max_align_t");
203+
return s;
199204
}
200205

201206
// @returns The region of memory the block manages, including the header.
@@ -455,6 +460,9 @@ optional<Block *> Block::split(size_t new_inner_size,
455460
Block *new_block = as_block(new_region);
456461
mark_free(); // Free status for this block is now stored in new_block.
457462
new_block->next()->prev_ = new_region.size();
463+
464+
LIBC_ASSERT(new_block->is_usable_space_aligned(usable_space_alignment) &&
465+
"usable space must have requested alignment");
458466
return new_block;
459467
}
460468

0 commit comments

Comments
 (0)