respect -d:useMalloc everywhere; turn ansi_c and memory into proper Nim modules

This commit is contained in:
Araq 2019-04-08 17:09:27 +02:00
commit 6efa7ecf18
4 changed files with 83 additions and 66 deletions

View file

@ -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"