gc:arc: support GC_ref/unref for ref T

This commit is contained in:
Araq 2019-11-12 17:05:32 +01:00 • committed by Andreas Rumpf
commit 25c724d38b
3 changed files with 16 additions and 5 deletions

View file

@ -28,8 +28,8 @@ hash of ``package & "." & module & "." & name`` to save space.
type
RefHeader = object
rc: int # the object header is now a single RC field.
# we could remove it in non-debug builds but this seems
# unwise.
# we could remove it in non-debug builds for the 'owned ref'
# design but this seems unwise.
template `+!`(p: pointer, s: int): pointer =
cast[pointer](cast[int](p) +% s)
@ -121,6 +121,17 @@ proc nimDecRefIsLast(p: pointer): bool {.compilerRtl, inl.} =
else:
dec head(p).rc
proc GC_unref*[T](x: ref T) =
## New runtime only supports this operation for 'ref T'.
if nimDecRefIsLast(cast[pointer](x)):
# XXX this does NOT work for virtual destructors!
`=destroy`(x[])
nimRawDispose(cast[pointer](x))
proc GC_ref*[T](x: ref T) =
## New runtime only supports this operation for 'ref T'.
if x != nil: nimIncRef(cast[pointer](x))
proc isObj(obj: PNimType, subclass: cstring): bool {.compilerRtl, inl.} =
proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}