From c8268330be4de61541aa34eb82f7a93dc62e627c Mon Sep 17 00:00:00 2001 From: yatsen1 Date: Tue, 18 Jun 2019 02:47:18 +0800 Subject: [feature] Add boehm gc finalizer (#11446) --- lib/system/mmdisp.nim | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/system') diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index 905fdcc94..a56be3d23 100644 --- a/lib/system/mmdisp.nim +++ b/lib/system/mmdisp.nim @@ -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 -- cgit 1.4.1-2-gfad0