summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2021-09-02 17:16:03 +0200
committerGitHub <noreply@github.com>2021-09-02 17:16:03 +0200
commitf46569bafdfdd3290baf132f025d61a0e550dd58 (patch)
tree9ae114839336ce65eb7535a1ee67f49b08911a42
parenta7cae2bda24871c55348173816a43144b9e668a5 (diff)
downloadNim-f46569bafdfdd3290baf132f025d61a0e550dd58.tar.gz
fixes #18494 (#18783)
-rw-r--r--lib/system/alloc.nim42
1 files changed, 34 insertions, 8 deletions
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index 479458950..6d83a2c17 100644
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -1053,22 +1053,48 @@ template instantiateForRegion(allocator: untyped) {.dirty.} =
         inc(result, it.size)
         it = it.next
 
+  when hasThreadSupport:
+    var sharedHeap: MemRegion
+    var heapLock: SysLock
+    initSysLock(heapLock)
+
   proc getFreeMem(): int =
-    result = allocator.freeMem
     #sysAssert(result == countFreeMem())
+    when hasThreadSupport and defined(gcDestructors):
+      acquireSys(heapLock)
+      result = sharedHeap.freeMem
+      releaseSys(heapLock)
+    else:
+      result = allocator.freeMem
 
-  proc getTotalMem(): int = return allocator.currMem
-  proc getOccupiedMem(): int = return allocator.occ #getTotalMem() - getFreeMem()
-  proc getMaxMem*(): int = return getMaxMem(allocator)
+  proc getTotalMem(): int =
+    when hasThreadSupport and defined(gcDestructors):
+      acquireSys(heapLock)
+      result = sharedHeap.currMem
+      releaseSys(heapLock)
+    else:
+      result = allocator.currMem
+
+  proc getOccupiedMem(): int =
+    when hasThreadSupport and defined(gcDestructors):
+      acquireSys(heapLock)
+      result = sharedHeap.occ
+      releaseSys(heapLock)
+    else:
+      result = allocator.occ #getTotalMem() - getFreeMem()
+
+  proc getMaxMem*(): int =
+    when hasThreadSupport and defined(gcDestructors):
+      acquireSys(heapLock)
+      result = getMaxMem(sharedHeap)
+      releaseSys(heapLock)
+    else:
+      result = getMaxMem(allocator)
 
   when defined(nimTypeNames):
     proc getMemCounters*(): (int, int) = getMemCounters(allocator)
 
   # -------------------- shared heap region ----------------------------------
-  when hasThreadSupport:
-    var sharedHeap: MemRegion
-    var heapLock: SysLock
-    initSysLock(heapLock)
 
   proc allocSharedImpl(size: Natural): pointer =
     when hasThreadSupport: