mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-28 18:24:01 +00:00
gc:destructors further progress
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user