Skip to content

Commit cd92aed

Browse files
committed
[NFC][libc] Remove Block::ALIGNMENT and Block::BLOCK_OVERHEAD
1 parent 8a0c2e7 commit cd92aed

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

libc/src/__support/block.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ class Block {
109109
static constexpr size_t SIZE_MASK = ~(PREV_FREE_MASK | LAST_MASK);
110110

111111
public:
112-
static constexpr size_t ALIGNMENT = cpp::max(alignof(max_align_t), size_t{4});
113-
static const size_t BLOCK_OVERHEAD;
114-
115112
// No copy or move.
116113
Block(const Block &other) = delete;
117114
Block &operator=(const Block &other) = delete;
@@ -130,19 +127,19 @@ class Block {
130127
/// pointer will return a non-null pointer.
131128
LIBC_INLINE static Block *from_usable_space(void *usable_space) {
132129
auto *bytes = reinterpret_cast<cpp::byte *>(usable_space);
133-
return reinterpret_cast<Block *>(bytes - BLOCK_OVERHEAD);
130+
return reinterpret_cast<Block *>(bytes - sizeof(Block));
134131
}
135132
LIBC_INLINE static const Block *from_usable_space(const void *usable_space) {
136133
const auto *bytes = reinterpret_cast<const cpp::byte *>(usable_space);
137-
return reinterpret_cast<const Block *>(bytes - BLOCK_OVERHEAD);
134+
return reinterpret_cast<const Block *>(bytes - sizeof(Block));
138135
}
139136

140137
/// @returns The total size of the block in bytes, including the header.
141138
LIBC_INLINE size_t outer_size() const { return next_ & SIZE_MASK; }
142139

143140
LIBC_INLINE static size_t outer_size(size_t inner_size) {
144141
// The usable region includes the prev_ field of the next block.
145-
return inner_size - sizeof(prev_) + BLOCK_OVERHEAD;
142+
return inner_size - sizeof(prev_) + sizeof(Block);
146143
}
147144

148145
/// @returns The number of usable bytes inside the block were it to be
@@ -170,20 +167,20 @@ class Block {
170167
/// @returns The number of usable bytes inside a block with the given outer
171168
/// size if it remains free.
172169
LIBC_INLINE static size_t inner_size_free(size_t outer_size) {
173-
return outer_size - BLOCK_OVERHEAD;
170+
return outer_size - sizeof(Block);
174171
}
175172

176173
/// @returns A pointer to the usable space inside this block.
177174
///
178175
/// Aligned to some multiple of max_align_t.
179176
LIBC_INLINE cpp::byte *usable_space() {
180-
auto *s = reinterpret_cast<cpp::byte *>(this) + BLOCK_OVERHEAD;
177+
auto *s = reinterpret_cast<cpp::byte *>(this) + sizeof(Block);
181178
LIBC_ASSERT(reinterpret_cast<uintptr_t>(s) % alignof(max_align_t) == 0 &&
182179
"usable space must be aligned to a multiple of max_align_t");
183180
return s;
184181
}
185182
LIBC_INLINE const cpp::byte *usable_space() const {
186-
const auto *s = reinterpret_cast<const cpp::byte *>(this) + BLOCK_OVERHEAD;
183+
const auto *s = reinterpret_cast<const cpp::byte *>(this) + sizeof(Block);
187184
LIBC_ASSERT(reinterpret_cast<uintptr_t>(s) % alignof(max_align_t) == 0 &&
188185
"usable space must be aligned to a multiple of max_align_t");
189186
return s;
@@ -246,7 +243,8 @@ class Block {
246243
LIBC_INLINE void mark_last() { next_ |= LAST_MASK; }
247244

248245
LIBC_INLINE Block(size_t outer_size) : next_(outer_size) {
249-
LIBC_ASSERT(outer_size % ALIGNMENT == 0 && "block sizes must be aligned");
246+
LIBC_ASSERT(outer_size % alignof(max_align_t) == 0 &&
247+
"block sizes must be aligned");
250248
LIBC_ASSERT(is_usable_space_aligned(alignof(max_align_t)) &&
251249
"usable space must be aligned to a multiple of max_align_t");
252250
}
@@ -362,8 +360,8 @@ class Block {
362360
/// summarily considered used and has no next block.
363361
};
364362

365-
inline constexpr size_t Block::BLOCK_OVERHEAD =
366-
align_up(sizeof(Block), ALIGNMENT);
363+
static_assert(alignof(max_align_t) >= 4,
364+
"at least 2 bits must be available in block sizes for flags");
367365

368366
LIBC_INLINE
369367
optional<Block *> Block::init(ByteSpan region) {

libc/src/__support/freestore.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ class FreeStore {
4040
Block *remove_best_fit(size_t size);
4141

4242
private:
43-
static constexpr size_t ALIGNMENT = alignof(max_align_t);
4443
static constexpr size_t MIN_OUTER_SIZE =
45-
align_up(Block::BLOCK_OVERHEAD + sizeof(FreeList::Node), ALIGNMENT);
44+
align_up(sizeof(Block) + sizeof(FreeList::Node), alignof(max_align_t));
4645
static constexpr size_t MIN_LARGE_OUTER_SIZE =
47-
align_up(Block::BLOCK_OVERHEAD + sizeof(FreeTrie::Node), ALIGNMENT);
46+
align_up(sizeof(Block) + sizeof(FreeTrie::Node), alignof(max_align_t));
4847
static constexpr size_t NUM_SMALL_SIZES =
49-
(MIN_LARGE_OUTER_SIZE - MIN_OUTER_SIZE) / ALIGNMENT;
48+
(MIN_LARGE_OUTER_SIZE - MIN_OUTER_SIZE) / alignof(max_align_t);
5049

5150
LIBC_INLINE static bool too_small(Block *block) {
5251
return block->outer_size() < MIN_OUTER_SIZE;
@@ -99,7 +98,8 @@ LIBC_INLINE Block *FreeStore::remove_best_fit(size_t size) {
9998

10099
LIBC_INLINE FreeList &FreeStore::small_list(Block *block) {
101100
LIBC_ASSERT(is_small(block) && "only legal for small blocks");
102-
return small_lists[(block->outer_size() - MIN_OUTER_SIZE) / ALIGNMENT];
101+
return small_lists[(block->outer_size() - MIN_OUTER_SIZE) /
102+
alignof(max_align_t)];
103103
}
104104

105105
LIBC_INLINE FreeList *FreeStore::find_best_small_fit(size_t size) {

libc/test/src/__support/block_test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using LIBC_NAMESPACE::cpp::span;
2222

2323
TEST(LlvmLibcBlockTest, CanCreateSingleAlignedBlock) {
2424
constexpr size_t kN = 1024;
25-
alignas(Block::ALIGNMENT) array<byte, kN> bytes;
25+
alignas(max_align_t) array<byte, kN> bytes;
2626

2727
auto result = Block::init(bytes);
2828
ASSERT_TRUE(result.has_value());
@@ -52,7 +52,7 @@ TEST(LlvmLibcBlockTest, CanCreateUnalignedSingleBlock) {
5252
constexpr size_t kN = 1024;
5353

5454
// Force alignment, so we can un-force it below
55-
alignas(Block::ALIGNMENT) array<byte, kN> bytes;
55+
alignas(max_align_t) array<byte, kN> bytes;
5656
span<byte> aligned(bytes);
5757

5858
auto result = Block::init(aligned.subspan(1));
@@ -90,8 +90,7 @@ TEST(LlvmLibcBlockTest, CanSplitBlock) {
9090
auto *block2 = *result;
9191

9292
EXPECT_EQ(block1->inner_size(), kSplitN);
93-
EXPECT_EQ(block1->outer_size(),
94-
kSplitN - prev_field_size + Block::BLOCK_OVERHEAD);
93+
EXPECT_EQ(block1->outer_size(), kSplitN - prev_field_size + sizeof(Block));
9594

9695
EXPECT_EQ(block2->outer_size(), orig_size - block1->outer_size());
9796
EXPECT_FALSE(block2->used());

0 commit comments

Comments
 (0)