gc:destructors further progress

This commit is contained in:
Andreas Rumpf
2018-11-29 01:13:24 +01:00
parent ab38c075f8
commit 7d82df20be
6 changed files with 52 additions and 24 deletions

View File

@@ -11,7 +11,8 @@ type
AllocatorFlag* {.pure.} = enum ## flags describing the properties of the allocator
ThreadLocal ## the allocator is thread local only.
ZerosMem ## the allocator always zeros the memory on an allocation
Allocator* = ptr object {.inheritable.}
Allocator* = ptr AllocatorObj
AllocatorObj* {.inheritable.} = object
alloc*: proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.}
dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall.}
realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.}
@@ -21,9 +22,21 @@ type
var
localAllocator {.threadvar.}: Allocator
sharedAllocator: Allocator
allocatorStorage {.threadvar.}: AllocatorObj
proc getLocalAllocator*(): Allocator =
result = localAllocator
if result == nil:
result = addr allocatorStorage
result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.} =
result = system.alloc(size)
result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall.} =
system.dealloc(p)
result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} =
result = system.realloc(p, newSize)
result.deallocAll = nil
result.flags = {ThreadLocal}
localAllocator = result
proc setLocalAllocator*(a: Allocator) =
localAllocator = a

View File

@@ -85,7 +85,7 @@ type
proc newSeqPayload(cap, elemSize: int): pointer {.compilerRtl.} =
# we have to use type erasure here as Nim does not support generic
# compilerProcs. Oh well, this will all be inlined anyway.
if cap <= 0:
if cap > 0:
let region = getLocalAllocator()
var p = cast[ptr PayloadBase](region.alloc(region, cap * elemSize + sizeof(int) + sizeof(Allocator)))
p.region = region