gc:destructors: more progress

This commit is contained in:
Andreas Rumpf 2018-11-29 20:10:52 +01:00
commit 350396e1ca
4 changed files with 18 additions and 22 deletions

View file

@ -1728,7 +1728,7 @@ proc processMagicType(c: PContext, m: PSym) =
of mBool: setMagicType(c.config, m, tyBool, 1) of mBool: setMagicType(c.config, m, tyBool, 1)
of mChar: setMagicType(c.config, m, tyChar, 1) of mChar: setMagicType(c.config, m, tyChar, 1)
of mString: of mString:
setMagicType(c.config, m, tyString, c.config.target.ptrSize) setMagicType(c.config, m, tyString, szUncomputedSize)
rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar)) rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
when false: when false:
if c.config.selectedGc == gcDestructors: if c.config.selectedGc == gcDestructors:

View file

@ -220,7 +220,7 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
typ.align = int16(conf.target.ptrSize) typ.align = int16(conf.target.ptrSize)
of tyString: of tyString:
if tfHasAsgn in typ.flags: if conf.selectedGC == gcDestructors:
typ.size = conf.target.ptrSize * 2 typ.size = conf.target.ptrSize * 2
else: else:
typ.size = conf.target.ptrSize typ.size = conf.target.ptrSize
@ -243,7 +243,7 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
return return
typ.align = int16(conf.target.ptrSize) typ.align = int16(conf.target.ptrSize)
if typ.kind == tySequence and tfHasAsgn in typ.flags: if typ.kind == tySequence and conf.selectedGC == gcDestructors:
typ.size = conf.target.ptrSize * 2 typ.size = conf.target.ptrSize * 2
else: else:
typ.size = conf.target.ptrSize typ.size = conf.target.ptrSize

View file

@ -18,6 +18,8 @@ type
realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.}
deallocAll*: proc (a: Allocator) {.nimcall.} deallocAll*: proc (a: Allocator) {.nimcall.}
flags*: set[AllocatorFlag] flags*: set[AllocatorFlag]
allocCount: int
deallocCount: int
var var
localAllocator {.threadvar.}: Allocator localAllocator {.threadvar.}: Allocator
@ -30,8 +32,10 @@ proc getLocalAllocator*(): Allocator =
result = addr allocatorStorage result = addr allocatorStorage
result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.} = result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.} =
result = system.alloc(size) result = system.alloc(size)
inc a.allocCount
result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall.} = result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall.} =
system.dealloc(p) system.dealloc(p)
inc a.deallocCount
result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} = result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} =
result = system.realloc(p, newSize) result = system.realloc(p, newSize)
result.deallocAll = nil result.deallocAll = nil
@ -47,15 +51,6 @@ proc getSharedAllocator*(): Allocator =
proc setSharedAllocator*(a: Allocator) = proc setSharedAllocator*(a: Allocator) =
sharedAllocator = a sharedAllocator = a
when false: proc allocCounters*(): (int, int) =
proc alloc*(size: int; alignment: int = 8): pointer = let a = getLocalAllocator()
let a = getCurrentAllocator() result = (a.allocCount, a.deallocCount)
result = a.alloc(a, size, alignment)
proc dealloc*(p: pointer; size: int) =
let a = getCurrentAllocator()
a.dealloc(a, p, size)
proc realloc*(p: pointer; oldSize, newSize: int): pointer =
let a = getCurrentAllocator()
result = a.realloc(a, p, oldSize, newSize)

View file

@ -126,18 +126,19 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
let oldLen = x.len let oldLen = x.len
if newLen <= oldLen: return if newLen <= oldLen: return
var xu = cast[ptr NimSeqV2[T]](addr x) var xu = cast[ptr NimSeqV2[T]](addr x)
if xu.p == nil or xu.p.cap < newLen:
xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T))) xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T)))
xu.len = newLen xu.len = newLen
for i in oldLen .. newLen-1: for i in oldLen .. newLen-1:
xu.p.data[i] = value xu.p.data[i] = value
proc setLen[T](s: var seq[T], newlen: Natural) = proc setLen[T](s: var seq[T], newlen: Natural) =
if newlen < s.len: {.noSideEffect.}:
shrink(s, newLen) if newlen < s.len:
else: shrink(s, newLen)
var v: T # get the default value of 'v' else:
grow(s, newLen, v) var v: T # get the default value of 'v'
grow(s, newLen, v)
when false: when false:
proc resize[T](s: var NimSeqV2[T]) = proc resize[T](s: var NimSeqV2[T]) =