mem: Fix Buddy_Allocator size calculation to truly include alignment

This didn't take into account the size of the header plus the size of
the allocation itself by virtue of `align_forward_uint`; this could
result in no change if `size` was equal to `b.alignment` because the
number is aligned, and if `actual_size` and `size` ended up being equal,
no additional space would be requested.

This meant that a block would end up being allocated on top of its
buddy's head.

Fixes #3435
This commit is contained in:
Feoramund
2025-06-19 19:41:38 -04:00
parent 4e9f15965a
commit 57e2d8f1dd
+8 -6
View File
@@ -2247,12 +2247,14 @@ Get required block size to fit in the allocation as well as the alignment paddin
*/ */
@(require_results) @(require_results)
buddy_block_size_required :: proc(b: ^Buddy_Allocator, size: uint) -> uint { buddy_block_size_required :: proc(b: ^Buddy_Allocator, size: uint) -> uint {
size := size assert(size > 0)
actual_size := b.alignment // NOTE: `size_of(Buddy_Block)` will be accounted for in `b.alignment`.
size += size_of(Buddy_Block) // This calculation is also previously guarded against being given a `size`
size = align_forward_uint(size, b.alignment) // 0 by `buddy_allocator_alloc_bytes_non_zeroed` checking for that.
for size > actual_size { actual_size := b.alignment + size
actual_size <<= 1 if intrinsics.count_ones(actual_size) != 1 {
// We're not a power of two. Let's fix that.
actual_size = 1 << (size_of(uint) * 8 - intrinsics.count_leading_zeros(actual_size))
} }
return actual_size return actual_size
} }