From 903b34399b5ef26c853691ffa73d63f17c123616 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 20 Oct 2021 06:39:30 -0500 Subject: [PATCH] heap bugfixes; changed static if to not capture defer statements --- core/alloc/heap.onyx | 14 ++++---------- core/sync/mutex.onyx | 1 + src/checker.c | 10 ++++++++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/core/alloc/heap.onyx b/core/alloc/heap.onyx index ea17f995..2ef8848c 100644 --- a/core/alloc/heap.onyx +++ b/core/alloc/heap.onyx @@ -77,9 +77,7 @@ get_watermark :: () => cast(u32) heap_state.next_alloc; heap_alloc :: (size_: u32, align: u32) -> rawptr { if size_ == 0 do return null; - #if runtime.Multi_Threading_Enabled { - sync.scoped_mutex(^heap_mutex); - } + #if runtime.Multi_Threading_Enabled do sync.scoped_mutex(^heap_mutex); size := size_ + sizeof heap_block; size = math.max(size, sizeof heap_freed_block); @@ -152,9 +150,7 @@ get_watermark :: () => cast(u32) heap_state.next_alloc; heap_free :: (ptr: rawptr) { #if Enable_Debug do assert(ptr != null, "Trying to free a null pointer."); - #if runtime.Multi_Threading_Enabled { - sync.scoped_mutex(^heap_mutex); - } + #if runtime.Multi_Threading_Enabled do sync.scoped_mutex(^heap_mutex); 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."); @@ -193,14 +189,12 @@ get_watermark :: () => cast(u32) heap_state.next_alloc; heap_resize :: (ptr: rawptr, new_size_: u32, align: u32) -> rawptr { if ptr == null do return null; - #if runtime.Multi_Threading_Enabled { - sync.scoped_mutex(^heap_mutex); - } + #if runtime.Multi_Threading_Enabled do sync.scoped_mutex(^heap_mutex); new_size := new_size_ + sizeof heap_block; new_size = math.max(new_size, sizeof heap_freed_block); 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."); hb_ptr.size &= ~Allocated_Flag; diff --git a/core/sync/mutex.onyx b/core/sync/mutex.onyx index 4960870f..982fcb00 100644 --- a/core/sync/mutex.onyx +++ b/core/sync/mutex.onyx @@ -30,6 +30,7 @@ mutex_destroy :: (m: ^Mutex) { mutex_lock :: (m: ^Mutex) { while __atomic_cmpxchg(^m.lock, 0, 1) == 1 { + if m.owner == context.thread_id do return; __atomic_wait(^m.lock, 1); } diff --git a/src/checker.c b/src/checker.c index ef35a7f6..72283227 100644 --- a/src/checker.c +++ b/src/checker.c @@ -147,10 +147,16 @@ CheckStatus check_if(AstIfWhile* ifnode) { } if (static_if_resolution(ifnode)) { - if (ifnode->true_stmt != NULL) CHECK(statement, (AstNode **) &ifnode->true_stmt); + if (ifnode->true_stmt != NULL) { + CHECK(statement, (AstNode **) &ifnode->true_stmt); + ifnode->true_stmt->rules = Block_Rule_Macro; + } } else { - if (ifnode->false_stmt != NULL) CHECK(statement, (AstNode **) &ifnode->false_stmt); + if (ifnode->false_stmt != NULL) { + CHECK(statement, (AstNode **) &ifnode->false_stmt); + ifnode->false_stmt->rules = Block_Rule_Macro; + } } } else { -- 2.25.1