From: Brendan Hansen Date: Fri, 4 Jun 2021 00:59:04 +0000 (-0500) Subject: small bugfix and made enabling debugging in heap.onyx compile time X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c96cae340659dab0e61d668e02a20b66434dc4d0;p=onyx.git small bugfix and made enabling debugging in heap.onyx compile time --- diff --git a/bin/onyx b/bin/onyx index 95c26567..934d7e56 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/alloc/heap.onyx b/core/alloc/heap.onyx index 7861f070..e76ad66b 100644 --- a/core/alloc/heap.onyx +++ b/core/alloc/heap.onyx @@ -1,6 +1,9 @@ package core.alloc.heap -enable_debug := false +// 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 // 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 @@ -62,7 +65,7 @@ heap_alloc :: (size_: u32, align: u32) -> rawptr { hb := heap_state.free_list; while hb != null { if hb.size >= size { - if enable_debug { + #if Enable_Debug { assert(hb.size & Allocated_Flag == 0, "Allocated block in free list."); assert(hb.magic_number == Free_Block_Magic_Number, "Malformed free block in free list."); } @@ -125,15 +128,15 @@ heap_alloc :: (size_: u32, align: u32) -> rawptr { #private_file heap_free :: (ptr: rawptr) { - if enable_debug do assert(ptr != null, "Trying to free a null pointer."); + #if Enable_Debug do assert(ptr != null, "Trying to free a null pointer."); hb_ptr := cast(^heap_freed_block) (cast(uintptr) ptr - sizeof heap_allocated_block); - if enable_debug do assert(hb_ptr.size & Allocated_Flag == Allocated_Flag, "Corrupted heap on free. This could be due to a double free, or using memory past were you allocated it."); + #if Enable_Debug do assert(hb_ptr.size & Allocated_Flag == Allocated_Flag, "Corrupted heap on free. This could be due to a double free, or using memory past were you allocated it."); hb_ptr.size &= ~Allocated_Flag; orig_size := hb_ptr.size - sizeof heap_allocated_block; - if enable_debug do memory_fill(ptr, ~~0xcc, orig_size); + #if Enable_Debug do memory_fill(ptr, ~~0xcc, orig_size); // CLEANUP: This is not complete. This only checks if the block after the freed block is also free. // There are three other cases related to the block before this one that need to be handled for @@ -170,7 +173,7 @@ heap_resize :: (ptr: rawptr, new_size_: u32, align: u32) -> rawptr { new_size = ~~memory.align(cast(u64) new_size, ~~align); hb_ptr := cast(^heap_allocated_block) (cast(uintptr) ptr - sizeof heap_allocated_block); - if enable_debug do assert(hb_ptr.size & Allocated_Flag == Allocated_Flag, "Corrupted heap on resize."); + #if Enable_Debug do assert(hb_ptr.size & Allocated_Flag == Allocated_Flag, "Corrupted heap on resize."); hb_ptr.size &= ~Allocated_Flag; old_size := hb_ptr.size; diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 4307a085..78efb99a 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -1692,7 +1692,7 @@ CheckStatus check_struct(AstStructType* s_node) { // NOTE: fills in the stcache type_build_from_ast(context.ast_alloc, (AstType *) s_node); - if (s_node->stcache == NULL) return Check_Yield_Macro; + if (s_node->stcache == NULL || !s_node->stcache_is_valid) return Check_Yield_Macro; bh_arr_each(StructMember *, smem, s_node->stcache->Struct.memarr) { if ((*smem)->type->kind == Type_Kind_Compound) {