mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-21 18:36:50 +00:00
[feature] Add boehm gc finalizer (#11446)
This commit is contained in:
@@ -99,6 +99,8 @@ when defined(boehmgc):
|
||||
## Return the total number of bytes allocated in this process.
|
||||
## Never decreases.
|
||||
|
||||
proc boehmRegisterFinalizer(obj, ff, cd, off, ocd: pointer) {.importc: "GC_register_finalizer", boehmGC.}
|
||||
|
||||
proc allocAtomic(size: int): pointer =
|
||||
result = boehmAllocAtomic(size)
|
||||
zeroMem(result, size)
|
||||
@@ -155,9 +157,14 @@ when defined(boehmgc):
|
||||
when hasThreadSupport:
|
||||
boehmGC_allow_register_threads()
|
||||
|
||||
proc boehmgc_finalizer(obj: pointer, typedFinalizer: (proc(x: pointer) {.cdecl.})) =
|
||||
typedFinalizer(obj)
|
||||
|
||||
proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
|
||||
if ntfNoRefs in typ.flags: result = allocAtomic(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.} =
|
||||
result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
|
||||
cast[PGenericSeq](result).len = len
|
||||
|
||||
Reference in New Issue
Block a user