From 6efa7ecf188c3658828875a8c2426f7db3c6f0ac Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 8 Apr 2019 17:09:27 +0200 Subject: respect -d:useMalloc everywhere; turn ansi_c and memory into proper Nim modules --- lib/core/allocators.nim | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'lib/core') 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" -- cgit 1.4.1-2-gfad0