added as_allocator; heap bugfix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 Nov 2021 17:50:19 +0000 (11:50 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 24 Nov 2021 17:50:19 +0000 (11:50 -0600)
core/alloc.onyx
core/alloc/arena.onyx
core/alloc/fixed.onyx
core/alloc/heap.onyx
core/alloc/pool.onyx
core/alloc/ring.onyx
core/container/list.onyx

index f53b2223425440cfb0da62e8b2ecbd621402f46c..67e63d23074434e8ccb903aeb33ef9f4dc9b21d5 100644 (file)
@@ -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.
index 8b3da21c5f27871a1820db136028fa18f05517e6..aad48fb7d25c85da3d4ecb4e8a063940a9383795 100644 (file)
@@ -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,
index 3e73bf2cdbd16df27038c9f8902980ffaff8f3a3..7c65d131b9795644b269f8b0f698867c97fc8990 100644 (file)
@@ -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,
index 25dcd31f8de35015226a3e4304d952470d8355fa..f8e1c8bfc75594b7b30e8613f19a6dfdd53c4536 100644 (file)
@@ -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;
index b54e5b4e4fdf45736c710e2acaabf322d4a50fad..88f8d827690f68c53fbfe13da027596e859ba217 100644 (file)
@@ -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 },
index a0752f3353b2fd6761895847b5c709aaeddf5379..44d02a35a63b417269c19377d6e9339c3b9effc7 100644 (file)
@@ -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,
index a1c988247001eff1b7943673d2036fd5f60e4967..04c82d9315165da4f14816e89e8803a373ff02bb 100644 (file)
@@ -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,
     };
 }