summary refs log tree commit diff stats
path: root/lib/core
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/allocators.nim21
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/core/allocators.nim b/lib/core/allocators.nim
index 984e83690..c4302fc2c 100644
--- a/lib/core/allocators.nim
+++ b/lib/core/allocators.nim
@@ -27,18 +27,33 @@ var
   sharedAllocator: Allocator
   allocatorStorage {.threadvar.}: AllocatorObj
 
+when defined(useMalloc) and not defined(nimscript):
+  import "system/ansi_c"
+  import "system/memory"
+
 proc getLocalAllocator*(): Allocator =
   result = localAllocator
   if result == nil:
     result = addr allocatorStorage
     result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [].} =
-      result = system.alloc(size)
+      when defined(useMalloc) and not defined(nimscript):
+        result = c_malloc(size)
+        # XXX do we need this?
+        nimZeroMem(result, size)
+      else:
+        result = system.alloc(size)
       inc a.allocCount
     result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [].} =
-      system.dealloc(p)
+      when defined(useMalloc) and not defined(nimscript):
+        c_free(p)
+      else:
+        system.dealloc(p)
       inc a.deallocCount
     result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [].} =
-      result = system.realloc(p, newSize)
+      when defined(useMalloc) and not defined(nimscript):
+        result = c_realloc(p, newSize)
+      else:
+        result = system.realloc(p, newSize)
     result.deallocAll = nil
     result.flags = {ThreadLocal}
     result.name = "nim_local"