heap bugfixes; changed static if to not capture defer statements
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 20 Oct 2021 11:39:30 +0000 (06:39 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 20 Oct 2021 11:39:30 +0000 (06:39 -0500)
core/alloc/heap.onyx
core/sync/mutex.onyx
src/checker.c

index ea17f995e072f8a501b20d609a8e1d7d532e9c9d..2ef8848c9e03aacf3837d883d31ad16bee7ba382 100644 (file)
@@ -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;
index 4960870f18dfe95f59898a68387455a1c3141121..982fcb00bfcff1f0c604e83f390a5a3459451d25 100644 (file)
@@ -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);
     }
 
index ef35a7f672d19aa65a1b4e9c45c66efa854ec712..72283227b563bf9d51367f52c5d03986582e766d 100644 (file)
@@ -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 {