WIP: avoid using the old growObj in order to fix the newly introduced seq leaks
This commit is contained in:
parent
51db60afed
commit
1c80619ac5
1 changed files with 59 additions and 6 deletions
|
|
@ -184,6 +184,11 @@ proc addChar(s: NimString, c: char): NimString =
|
||||||
result = s
|
result = s
|
||||||
if result.len >= result.space:
|
if result.len >= result.space:
|
||||||
let r = resize(result.space)
|
let r = resize(result.space)
|
||||||
|
when defined(nimIncrSeqV3):
|
||||||
|
result = rawNewStringNoInit(r)
|
||||||
|
result.len = s.len
|
||||||
|
copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
|
||||||
|
else:
|
||||||
result = cast[NimString](growObj(result,
|
result = cast[NimString](growObj(result,
|
||||||
sizeof(TGenericSeq) + r + 1))
|
sizeof(TGenericSeq) + r + 1))
|
||||||
result.reserved = r
|
result.reserved = r
|
||||||
|
|
@ -229,6 +234,11 @@ proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
|
||||||
result = dest
|
result = dest
|
||||||
else: # slow path:
|
else: # slow path:
|
||||||
var sp = max(resize(dest.space), dest.len + addlen)
|
var sp = max(resize(dest.space), dest.len + addlen)
|
||||||
|
when defined(nimIncrSeqV3):
|
||||||
|
result = rawNewStringNoInit(sp)
|
||||||
|
result.len = dest.len
|
||||||
|
copyMem(addr result.data[0], unsafeAddr(dest.data[0]), dest.len+1)
|
||||||
|
else:
|
||||||
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
|
result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
|
||||||
result.reserved = sp
|
result.reserved = sp
|
||||||
#result = rawNewString(sp)
|
#result = rawNewString(sp)
|
||||||
|
|
@ -282,6 +292,9 @@ proc incrSeqV2(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
|
||||||
GenericSeqSize))
|
GenericSeqSize))
|
||||||
result.reserved = r
|
result.reserved = r
|
||||||
|
|
||||||
|
template `+!`(p: pointer, s: int): pointer =
|
||||||
|
cast[pointer](cast[int](p) +% s)
|
||||||
|
|
||||||
proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerProc.} =
|
proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerProc.} =
|
||||||
if s == nil:
|
if s == nil:
|
||||||
result = cast[PGenericSeq](newSeq(typ, 1))
|
result = cast[PGenericSeq](newSeq(typ, 1))
|
||||||
|
|
@ -290,6 +303,13 @@ proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerProc.} =
|
||||||
result = s
|
result = s
|
||||||
if result.len >= result.space:
|
if result.len >= result.space:
|
||||||
let r = resize(result.space)
|
let r = resize(result.space)
|
||||||
|
when defined(nimIncrSeqV3):
|
||||||
|
result = cast[PGenericSeq](newSeq(typ, r))
|
||||||
|
result.len = s.len
|
||||||
|
copyMem(result +! GenericSeqSize, s +! GenericSeqSize, s.len * typ.base.size)
|
||||||
|
# since we steal the content from 's', it's crucial to set s's len to 0.
|
||||||
|
s.len = 0
|
||||||
|
else:
|
||||||
result = cast[PGenericSeq](growObj(result, typ.base.size * r +
|
result = cast[PGenericSeq](growObj(result, typ.base.size * r +
|
||||||
GenericSeqSize))
|
GenericSeqSize))
|
||||||
result.reserved = r
|
result.reserved = r
|
||||||
|
|
@ -336,8 +356,41 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
|
||||||
|
|
||||||
proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
|
proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
|
||||||
compilerRtl.} =
|
compilerRtl.} =
|
||||||
|
sysAssert typ.kind == tySequence, "setLengthSeqV2: type is not a seq"
|
||||||
if s == nil:
|
if s == nil:
|
||||||
result = cast[PGenericSeq](newSeq(typ, newLen))
|
result = cast[PGenericSeq](newSeq(typ, newLen))
|
||||||
|
else:
|
||||||
|
when defined(nimIncrSeqV3):
|
||||||
|
let elemSize = typ.base.size
|
||||||
|
if s.space < newLen:
|
||||||
|
let r = max(resize(s.space), newLen)
|
||||||
|
result = cast[PGenericSeq](newSeq(typ, r))
|
||||||
|
copyMem(result +! GenericSeqSize, s +! GenericSeqSize, s.len * elemSize)
|
||||||
|
# since we steal the content from 's', it's crucial to set s's len to 0.
|
||||||
|
s.len = 0
|
||||||
|
elif newLen < s.len:
|
||||||
|
result = s
|
||||||
|
# we need to decref here, otherwise the GC leaks!
|
||||||
|
when not defined(boehmGC) and not defined(nogc) and
|
||||||
|
not defined(gcMarkAndSweep) and not defined(gogc) and
|
||||||
|
not defined(gcRegions):
|
||||||
|
if ntfNoRefs notin typ.base.flags:
|
||||||
|
for i in newLen..result.len-1:
|
||||||
|
forAllChildrenAux(cast[pointer](cast[ByteAddress](result) +%
|
||||||
|
GenericSeqSize +% (i*%elemSize)),
|
||||||
|
extGetCellType(result).base, waZctDecRef)
|
||||||
|
|
||||||
|
# XXX: zeroing out the memory can still result in crashes if a wiped-out
|
||||||
|
# cell is aliased by another pointer (ie proc parameter or a let variable).
|
||||||
|
# This is a tough problem, because even if we don't zeroMem here, in the
|
||||||
|
# presence of user defined destructors, the user will expect the cell to be
|
||||||
|
# "destroyed" thus creating the same problem. We can destoy the cell in the
|
||||||
|
# finalizer of the sequence, but this makes destruction non-deterministic.
|
||||||
|
zeroMem(cast[pointer](cast[ByteAddress](result) +% GenericSeqSize +%
|
||||||
|
(newLen*%elemSize)), (result.len-%newLen) *% elemSize)
|
||||||
|
else:
|
||||||
|
result = s
|
||||||
|
result.len = newLen
|
||||||
else:
|
else:
|
||||||
result = setLengthSeq(s, typ.base.size, newLen)
|
result = setLengthSeq(s, typ.base.size, newLen)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue