summary refs log tree commit diff stats
path: root/lib/system/alloc.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/system/alloc.nim')
-rwxr-xr-xlib/system/alloc.nim25
1 files changed, 21 insertions, 4 deletions
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index c385aa6fe..2280415e1 100755
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -525,22 +525,39 @@ proc isAllocatedPtr(a: TAllocator, p: pointer): bool =
 # ---------------------- interface to programs -------------------------------
 
 when not defined(useNimRtl):
-  proc alloc(size: int): pointer =
+  var heapLock: TSysLock
+  InitSysLock(HeapLock)
+
+  proc unlockedAlloc(size: int): pointer {.inline.} =
     result = rawAlloc(allocator, size+sizeof(TFreeCell))
     cast[ptr TFreeCell](result).zeroField = 1 # mark it as used
     assert(not isAllocatedPtr(allocator, result))
     result = cast[pointer](cast[TAddress](result) +% sizeof(TFreeCell))
 
-  proc alloc0(size: int): pointer =
-    result = alloc(size)
+  proc unlockedAlloc0(size: int): pointer {.inline.} =
+    result = unlockedAlloc(size)
     zeroMem(result, size)
 
-  proc dealloc(p: pointer) =
+  proc unlockedDealloc(p: pointer) {.inline.} =
     var x = cast[pointer](cast[TAddress](p) -% sizeof(TFreeCell))
     assert(cast[ptr TFreeCell](x).zeroField == 1)
     rawDealloc(allocator, x)
     assert(not isAllocatedPtr(allocator, x))
 
+  proc alloc(size: int): pointer =
+    when hasThreadSupport: AquireSys(HeapLock)
+    result = unlockedAlloc(size)
+    when hasThreadSupport: ReleaseSys(HeapLock)
+
+  proc alloc0(size: int): pointer =
+    result = alloc(size)
+    zeroMem(result, size)
+
+  proc dealloc(p: pointer) =
+    when hasThreadSupport: AquireSys(HeapLock)
+    unlockedDealloc(p)
+    when hasThreadSupport: ReleaseSys(HeapLock)
+
   proc ptrSize(p: pointer): int =
     var x = cast[pointer](cast[TAddress](p) -% sizeof(TFreeCell))
     result = pageAddr(x).size - sizeof(TFreeCell)