more checking for --gc:arc, no need for valgrind (#14467)

* sigmatch: removed dead code
This commit is contained in:
Andreas Rumpf 2020-05-29 12:08:17 +02:00 • committed by GitHub
commit e31ac81899
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 159 additions and 73 deletions

View file

@ -38,11 +38,13 @@ when defined(gcOrc):
const
rcIncrement = 0b10000 # so that lowest 4 bits are not touched
rcMask = 0b1111
rcShift = 4 # shift by rcShift to get the reference counter
else:
const
rcIncrement = 0b1000 # so that lowest 3 bits are not touched
rcMask = 0b111
rcShift = 3 # shift by rcShift to get the reference counter
type
RefHeader = object
@ -52,6 +54,8 @@ type
when defined(gcOrc):
rootIdx: int # thanks to this we can delete potential cycle roots
# in O(1) without doubly linked lists
when defined(nimArcDebug):
refId: int
Cell = ptr RefHeader
@ -67,6 +71,14 @@ template head(p: pointer): Cell =
const
traceCollector = defined(traceArc)
when defined(nimArcDebug):
include cellsets
const traceId = 7739 # 1037
var gRefId: int
var freedCells: CellSet
proc nimNewObj(size: int): pointer {.compilerRtl.} =
let s = size + sizeof(RefHeader)
when defined(nimscript):
@ -79,6 +91,9 @@ proc nimNewObj(size: int): pointer {.compilerRtl.} =
result = allocShared0(s) +! sizeof(RefHeader)
else:
result = alloc0(s) +! sizeof(RefHeader)
when defined(nimArcDebug):
head(result).refId = gRefId
atomicInc gRefId
when traceCollector:
cprintf("[Allocated] %p result: %p\n", result -! sizeof(RefHeader), result)
@ -98,6 +113,9 @@ proc nimNewObjUninit(size: int): pointer {.compilerRtl.} =
when defined(gcOrc):
orig.rootIdx = 0
result = orig +! sizeof(RefHeader)
when defined(nimArcDebug):
head(result).refId = gRefId
atomicInc gRefId
when traceCollector:
cprintf("[Allocated] %p result: %p\n", result -! sizeof(RefHeader), result)
@ -105,10 +123,25 @@ proc nimDecWeakRef(p: pointer) {.compilerRtl, inl.} =
dec head(p).rc, rcIncrement
proc nimIncRef(p: pointer) {.compilerRtl, inl.} =
when defined(nimArcDebug):
if head(p).refId == traceId:
writeStackTrace()
cfprintf(cstderr, "[IncRef] %p %ld\n", p, head(p).rc shr rcShift)
inc head(p).rc, rcIncrement
when traceCollector:
cprintf("[INCREF] %p\n", head(p))
when not defined(nimscript) and defined(nimArcDebug):
proc deallocatedRefId*(p: pointer): int =
## Returns the ref's ID if the ref was already deallocated. This
## is a memory corruption check. Returns 0 if there is no error.
let c = head(p)
if freedCells.data != nil and freedCells.contains(c):
result = c.refId
else:
result = 0
proc nimRawDispose(p: pointer) {.compilerRtl.} =
when not defined(nimscript):
when traceCollector:
@ -117,7 +150,11 @@ proc nimRawDispose(p: pointer) {.compilerRtl.} =
if head(p).rc >= rcIncrement:
cstderr.rawWrite "[FATAL] dangling references exist\n"
quit 1
when defined(useMalloc):
when defined(nimArcDebug):
# we do NOT really free the memory here in order to reliably detect use-after-frees
if freedCells.data == nil: init(freedCells)
freedCells.incl head(p)
elif defined(useMalloc):
c_free(p -! sizeof(RefHeader))
elif compileOption("threads"):
deallocShared(p -! sizeof(RefHeader))
@ -150,6 +187,12 @@ when defined(gcOrc):
proc nimDecRefIsLast(p: pointer): bool {.compilerRtl, inl.} =
if p != nil:
var cell = head(p)
when defined(nimArcDebug):
if cell.refId == traceId:
writeStackTrace()
cfprintf(cstderr, "[DecRef] %p %ld\n", p, cell.rc shr rcShift)
if (cell.rc and not rcMask) == 0:
result = true
when traceCollector: