From 4ae021cd4cc8fb8a1cf27c0b7c2272ddd025dde0 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 18 Dec 2023 15:17:27 +0100 Subject: [PATCH] add other failing test and fix them --- core/runtime/default_allocators_arena.odin | 8 ++++---- tests/core/runtime/test_core_runtime.odin | 13 ++++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/core/runtime/default_allocators_arena.odin b/core/runtime/default_allocators_arena.odin index 1c36c4f7c..3e78e7f20 100644 --- a/core/runtime/default_allocators_arena.odin +++ b/core/runtime/default_allocators_arena.odin @@ -102,14 +102,14 @@ arena_alloc :: proc(arena: ^Arena, size, alignment: uint, loc := #caller_locatio if size == 0 { return } - - if arena.curr_block == nil || (safe_add(arena.curr_block.used, size) or_else 0) > arena.curr_block.capacity { - size = align_forward_uint(size, alignment) + + needed := align_forward_uint(size, alignment) + if arena.curr_block == nil || (safe_add(arena.curr_block.used, needed) or_else 0) > arena.curr_block.capacity { if arena.minimum_block_size == 0 { arena.minimum_block_size = DEFAULT_ARENA_GROWING_MINIMUM_BLOCK_SIZE } - block_size := max(size, arena.minimum_block_size) + block_size := max(needed, arena.minimum_block_size) if arena.backing_allocator.procedure == nil { arena.backing_allocator = default_allocator() diff --git a/tests/core/runtime/test_core_runtime.odin b/tests/core/runtime/test_core_runtime.odin index 6ec1e78d9..5ae07ffe2 100644 --- a/tests/core/runtime/test_core_runtime.odin +++ b/tests/core/runtime/test_core_runtime.odin @@ -30,6 +30,7 @@ main :: proc() { test_temp_allocator_big_alloc_and_alignment(&t) test_temp_allocator_alignment_boundary(&t) + test_temp_allocator_returns_correct_size(&t) fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) if TEST_fail > 0 { @@ -56,6 +57,16 @@ test_temp_allocator_big_alloc_and_alignment :: proc(t: ^testing.T) { context.allocator = runtime.arena_allocator(&arena) mappy: map[[8]int]int - err := reserve(&mappy, 50000) + err := reserve(&mappy, 50000) expect_value(t, err, nil) } + +@(test) +test_temp_allocator_returns_correct_size :: proc(t: ^testing.T) { + arena: runtime.Arena + context.allocator = runtime.arena_allocator(&arena) + + bytes, err := mem.alloc_bytes(10, 16) + expect_value(t, err, nil) + expect_value(t, len(bytes), 10) +}