Skip to content

Commit 04e6a2f

Browse files
committed
refactor(allocator): improve documentation on pointer alignment (#12307)
Follow-on after #12299. Document better the logic around alignment of allocator chunk pointer. It's pretty confusing, and I made a mistake here before. Clarify the logic.
1 parent 5ecf5c4 commit 04e6a2f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

crates/oxc_allocator/src/fixed_size.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ impl FixedSizeAllocator {
6666
};
6767

6868
// Get pointer to use for allocator chunk, aligned to 4 GiB.
69-
// SAFETY: `offset` is either 0 or `TWO_GIB`.
70-
// We allocated 4 GiB of memory, so adding `offset` to `alloc_ptr` is in bounds.
71-
let chunk_ptr = unsafe {
72-
let offset = alloc_ptr.as_ptr() as usize % FOUR_GIB;
73-
alloc_ptr.add(offset)
74-
};
69+
// `alloc_ptr` is aligned on 2 GiB, so `alloc_ptr % FOUR_GIB` is either 0 or `TWO_GIB`.
70+
//
71+
// * If allocation is already aligned on 4 GiB, `offset == 0`.
72+
// Chunk occupies 1st half of the allocation.
73+
// * If allocation is not aligned on 4 GiB, `offset == TWO_GIB`.
74+
// Adding `offset` to `alloc_ptr` brings it up to 4 GiB alignment.
75+
// Chunk occupies 2nd half of the allocation.
76+
//
77+
// Either way, `chunk_ptr` is aligned on 4 GiB.
78+
let offset = alloc_ptr.as_ptr() as usize % FOUR_GIB;
79+
// SAFETY: We allocated 4 GiB of memory, so adding `offset` to `alloc_ptr` is in bounds
80+
let chunk_ptr = unsafe { alloc_ptr.add(offset) };
7581

7682
debug_assert!(chunk_ptr.as_ptr() as usize % BUFFER_ALIGN == 0);
7783

0 commit comments

Comments
 (0)