pool -> arena

This commit is contained in:
gingerBill
2026-06-23 12:21:29 +01:00
parent 82b2b293e9
commit 7d36866fe9

View File

@@ -4,17 +4,17 @@ import "core:testing"
import "core:mem"
expect_pool_allocation :: proc(t: ^testing.T, expected_used_bytes, num_bytes, alignment: int) {
pool: mem.Dynamic_Pool
mem.dynamic_pool_init(&pool, minimum_alignment = alignment)
pool_allocator := mem.dynamic_pool_allocator(&pool)
expect_arena_allocation :: proc(t: ^testing.T, expected_used_bytes, num_bytes, alignment: int) {
arena: mem.Dynamic_Arena
mem.dynamic_arena_init(&arena, minimum_alignment = alignment)
arena_allocator := mem.dynamic_arena_allocator(&arena)
element, err := mem.alloc(num_bytes, alignment, pool_allocator)
element, err := mem.alloc(num_bytes, alignment, arena_allocator)
testing.expect(t, err == .None)
testing.expect(t, element != nil)
expected_bytes_left := pool.block_size - expected_used_bytes
testing.expectf(t, pool.bytes_left == expected_bytes_left,
expected_bytes_left := arena.block_size - expected_used_bytes
testing.expectf(t, arena.bytes_left == expected_bytes_left,
`
Allocated data with size %v bytes, expected %v bytes left, got %v bytes left, off by %v bytes.
Pool:
@@ -28,57 +28,57 @@ expect_pool_allocation :: proc(t: ^testing.T, expected_used_bytes, num_bytes, al
current_pos = %v
bytes_left = %v
`,
num_bytes, expected_bytes_left, pool.bytes_left, expected_bytes_left - pool.bytes_left,
pool.block_size,
pool.out_band_size,
pool.minimum_alignment,
pool.unused_blocks,
pool.used_blocks,
pool.out_band_allocations,
pool.current_block,
pool.current_pos,
pool.bytes_left,
num_bytes, expected_bytes_left, arena.bytes_left, expected_bytes_left - arena.bytes_left,
arena.block_size,
arena.out_band_size,
arena.minimum_alignment,
arena.unused_blocks,
arena.used_blocks,
arena.out_band_allocations,
arena.current_block,
arena.current_pos,
arena.bytes_left,
)
testing.expectf(t, uintptr(element) % uint(alignment) == 0, "Expected allocation to be aligned to %d byte boundary, got %v", alignment, element)
mem.dynamic_pool_destroy(&pool)
testing.expect(t, pool.used_blocks == nil)
mem.dynamic_arena_destroy(&arena)
testing.expect(t, arena.used_blocks == nil)
}
expect_pool_allocation_out_of_band :: proc(t: ^testing.T, num_bytes, block_size, out_band_size: int) {
expect_arena_allocation_out_of_band :: proc(t: ^testing.T, num_bytes, block_size, out_band_size: int) {
testing.expect(t, num_bytes >= out_band_size, "Sanity check failed, your test call is flawed! Make sure that num_bytes >= out_band_size!")
pool: mem.Dynamic_Pool
mem.dynamic_pool_init(&pool, block_size = block_size, out_band_size = out_band_size)
pool_allocator := mem.dynamic_pool_allocator(&pool)
arena: mem.Dynamic_Arena
mem.dynamic_arena_init(&arena, block_size = block_size, out_band_size = out_band_size)
arena_allocator := mem.dynamic_arena_allocator(&arena)
element, err := mem.alloc(num_bytes, allocator = pool_allocator)
element, err := mem.alloc(num_bytes, allocator = arena_allocator)
testing.expect(t, err == .None)
testing.expect(t, element != nil)
testing.expectf(t, pool.out_band_allocations != nil,
"Allocated data with size %v bytes, which is >= out_of_band_size and it should be in pool.out_band_allocations, but isn't!",
testing.expectf(t, arena.out_band_allocations != nil,
"Allocated data with size %v bytes, which is >= out_of_band_size and it should be in arena.out_band_allocations, but isn't!",
)
mem.dynamic_pool_destroy(&pool)
testing.expect(t, pool.out_band_allocations == nil)
mem.dynamic_arena_destroy(&arena)
testing.expect(t, arena.out_band_allocations == nil)
}
@(test)
test_dynamic_pool_alloc_aligned :: proc(t: ^testing.T) {
expect_pool_allocation(t, expected_used_bytes = 16, num_bytes = 16, alignment=8)
test_dynamic_arena_alloc_aligned :: proc(t: ^testing.T) {
expect_arena_allocation(t, expected_used_bytes = 16, num_bytes = 16, alignment=8)
}
@(test)
test_dynamic_pool_alloc_unaligned :: proc(t: ^testing.T) {
expect_pool_allocation(t, expected_used_bytes = 8, num_bytes = 1, alignment = 8)
expect_pool_allocation(t, expected_used_bytes = 16, num_bytes = 9, alignment = 8)
test_dynamic_arena_alloc_unaligned :: proc(t: ^testing.T) {
expect_arena_allocation(t, expected_used_bytes = 8, num_bytes = 1, alignment = 8)
expect_arena_allocation(t, expected_used_bytes = 16, num_bytes = 9, alignment = 8)
}
@(test)
test_dynamic_pool_alloc_out_of_band :: proc(t: ^testing.T) {
expect_pool_allocation_out_of_band(t, num_bytes = 128, block_size = 512, out_band_size = 128)
expect_pool_allocation_out_of_band(t, num_bytes = 129, block_size = 512, out_band_size = 128)
expect_pool_allocation_out_of_band(t, num_bytes = 513, block_size = 512, out_band_size = 128)
test_dynamic_arena_alloc_out_of_band :: proc(t: ^testing.T) {
expect_arena_allocation_out_of_band(t, num_bytes = 128, block_size = 512, out_band_size = 128)
expect_arena_allocation_out_of_band(t, num_bytes = 129, block_size = 512, out_band_size = 128)
expect_arena_allocation_out_of_band(t, num_bytes = 513, block_size = 512, out_band_size = 128)
}
@(test)