[feature] Add boehm gc finalizer (#11446)

This commit is contained in:
yatsen1 2019-06-18 02:47:18 +08:00 • committed by Andreas Rumpf
commit c8268330be

View file

@ -99,6 +99,8 @@ when defined(boehmgc):
## Return the total number of bytes allocated in this process. ## Return the total number of bytes allocated in this process.
## Never decreases. ## Never decreases.
proc boehmRegisterFinalizer(obj, ff, cd, off, ocd: pointer) {.importc: "GC_register_finalizer", boehmGC.}
proc allocAtomic(size: int): pointer = proc allocAtomic(size: int): pointer =
result = boehmAllocAtomic(size) result = boehmAllocAtomic(size)
zeroMem(result, size) zeroMem(result, size)
@ -155,9 +157,14 @@ when defined(boehmgc):
when hasThreadSupport: when hasThreadSupport:
boehmGC_allow_register_threads() boehmGC_allow_register_threads()
proc boehmgc_finalizer(obj: pointer, typedFinalizer: (proc(x: pointer) {.cdecl.})) =
typedFinalizer(obj)
proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} = proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
if ntfNoRefs in typ.flags: result = allocAtomic(size) if ntfNoRefs in typ.flags: result = allocAtomic(size)
else: result = alloc(size) else: result = alloc(size)
if typ.finalizer != nil:
boehmRegisterFinalizer(result, boehmgc_finalizer, typ.finalizer, nil, nil)
proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} = proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize)) result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
cast[PGenericSeq](result).len = len cast[PGenericSeq](result).len = len