From: Brendan Hansen Date: Wed, 24 Nov 2021 17:50:19 +0000 (-0600) Subject: added as_allocator; heap bugfix X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2f75697de44c0789d2cb9e5c6ed67662eeecf040;p=onyx.git added as_allocator; heap bugfix --- diff --git a/core/alloc.onyx b/core/alloc.onyx index f53b2223..67e63d23 100644 --- a/core/alloc.onyx +++ b/core/alloc.onyx @@ -7,6 +7,10 @@ package core.alloc #load "./alloc/pool" #load "./alloc/logging" +as_allocator :: #match { + macro (a: Allocator) => a +} + TEMPORARY_ALLOCATOR_SIZE :: 1 << 12; // 4Kb // The global heap allocator, set up upon program intialization. diff --git a/core/alloc/arena.onyx b/core/alloc/arena.onyx index 8b3da21c..aad48fb7 100644 --- a/core/alloc/arena.onyx +++ b/core/alloc/arena.onyx @@ -76,6 +76,7 @@ make :: (backing: Allocator, arena_size: u32) -> ArenaState { }; } +#match (package core.alloc).as_allocator make_allocator make_allocator :: (rs: ^ArenaState) -> Allocator { return Allocator.{ func = arena_alloc_proc, diff --git a/core/alloc/fixed.onyx b/core/alloc/fixed.onyx index 3e73bf2c..7c65d131 100644 --- a/core/alloc/fixed.onyx +++ b/core/alloc/fixed.onyx @@ -32,6 +32,7 @@ make :: (ptr: rawptr, size: u32) -> FixedAllocatorData { }; } +#match (package core.alloc).as_allocator make_allocator make_allocator :: (fa_data: ^FixedAllocatorData) -> Allocator { return Allocator.{ func = fixed_allocator_proc, diff --git a/core/alloc/heap.onyx b/core/alloc/heap.onyx index 25dcd31f..f8e1c8bf 100644 --- a/core/alloc/heap.onyx +++ b/core/alloc/heap.onyx @@ -3,7 +3,7 @@ package core.alloc.heap // Enable this to enable checking for invalid blocks and other corruptions // that may happen on the heap, with the added overhead of checking that // on every alloc/resize/free. -Enable_Debug :: false +Enable_Debug :: false // This is the implementation for the general purpose heap allocator. // It is a simple bump allocator, with a free list. It is not very good @@ -80,7 +80,7 @@ get_freed_size :: () => { Allocated_Flag :: 0x1 Free_Block_Magic_Number :: 0xdeadbeef - Block_Split_Size :: 512 + Block_Split_Size :: 256 // FIX: This does not respect the choice of alignment heap_alloc :: (size_: u32, align: u32) -> rawptr { @@ -102,13 +102,12 @@ get_freed_size :: () => { } if hb.size - size >= Block_Split_Size { - hb.size = size; - new_block := cast(^heap_freed_block) (cast(uintptr) hb + size); new_block.size = hb.size - size; new_block.next = hb.next; new_block.prev = hb.prev; new_block.magic_number = Free_Block_Magic_Number; + hb.size = size; if hb.next != null do hb.next.prev = new_block; *prev = new_block; diff --git a/core/alloc/pool.onyx b/core/alloc/pool.onyx index b54e5b4e..88f8d827 100644 --- a/core/alloc/pool.onyx +++ b/core/alloc/pool.onyx @@ -75,6 +75,7 @@ make :: (buffer: [] $Elem) -> PoolAllocator(Elem) { }; } +#match (package core.alloc).as_allocator make_allocator make_allocator :: (pool: ^PoolAllocator($Elem)) -> Allocator { return Allocator.{ func = #solidify pool_allocator_proc { Elem = Elem }, diff --git a/core/alloc/ring.onyx b/core/alloc/ring.onyx index a0752f33..44d02a35 100644 --- a/core/alloc/ring.onyx +++ b/core/alloc/ring.onyx @@ -46,6 +46,7 @@ make :: (buffer: rawptr, length: u32) -> RingState { }; } +#match (package core.alloc).as_allocator make_allocator make_allocator :: (rs: ^RingState) -> Allocator { return .{ func = ring_alloc_proc, diff --git a/core/container/list.onyx b/core/container/list.onyx index a1c98824..04c82d93 100644 --- a/core/container/list.onyx +++ b/core/container/list.onyx @@ -91,9 +91,7 @@ fold :: (list: ^List($T), init: $R, f: (T, R) -> R) -> R { // } get_iterator :: (list: ^List($T)) -> Iterator(T) { - iterator_next :: ($T: type_expr, data: rawptr) -> (T, bool) { - list_iter := cast(^ListIterator(T)) data; - + iterator_next :: (list_iter: ^ListIterator($T)) -> (T, bool) { use package core.intrinsics.onyx { __zero_value } if list_iter.current == null do return __zero_value(T), false; @@ -101,10 +99,6 @@ get_iterator :: (list: ^List($T)) -> Iterator(T) { return list_iter.current.data, true; } - iterator_close :: (data: rawptr) { - cfree(data); - } - ListIterator :: struct (T: type_expr) { current: ^ListElem(T); } @@ -115,7 +109,7 @@ get_iterator :: (list: ^List($T)) -> Iterator(T) { return .{ data = list_iterator, next = #solidify iterator_next { T = T }, - close = iterator_close, + close = cfree, }; }