Skip to content

Commit

Permalink
arc allocation method aligned (#15588)
Browse files Browse the repository at this point in the history
* progress

* fix typo
  • Loading branch information
cooldome authored Oct 19, 2020
1 parent 67afe72 commit 833035b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 41 deletions.
24 changes: 16 additions & 8 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2180,17 +2180,25 @@ proc genDestroy(p: BProc; n: PNode) =
of tyString:
var a: TLoc
initLocExpr(p, arg, a)
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #deallocShared($1.p);$n" &
"}$n",
[rdLoc(a)])
if optThreads in p.config.globalOptions:
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #deallocShared($1.p);$n" &
"}$n", [rdLoc(a)])
else:
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #dealloc($1.p);$n" &
"}$n", [rdLoc(a)])
of tySequence:
var a: TLoc
initLocExpr(p, arg, a)
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #deallocShared($1.p);$n" &
"}$n",
[rdLoc(a), getTypeDesc(p.module, t.lastSon)])
if optThreads in p.config.globalOptions:
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #deallocShared($1.p);$n" &
"}$n", [rdLoc(a)])
else:
linefmt(p, cpsStmts, "if ($1.p && !($1.p->cap & NIM_STRLIT_FLAG)) {$n" &
" #dealloc($1.p);$n" &
"}$n", [rdLoc(a)])
else: discard "nothing to do"
else:
let t = n[1].typ.skipTypes(abstractVar)
Expand Down
8 changes: 0 additions & 8 deletions lib/system/arc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ proc nimNewObj(size: int): pointer {.compilerRtl.} =
let s = size + sizeof(RefHeader)
when defined(nimscript):
discard
elif defined(useMalloc):
var orig = c_malloc(cuint s)
nimZeroMem(orig, s)
result = orig +! sizeof(RefHeader)
elif compileOption("threads"):
result = allocShared0(s) +! sizeof(RefHeader)
else:
Expand All @@ -102,8 +98,6 @@ proc nimNewObjUninit(size: int): pointer {.compilerRtl.} =
let s = size + sizeof(RefHeader)
when defined(nimscript):
discard
elif defined(useMalloc):
var orig = cast[ptr RefHeader](c_malloc(cuint s))
elif compileOption("threads"):
var orig = cast[ptr RefHeader](allocShared(s))
else:
Expand Down Expand Up @@ -171,8 +165,6 @@ proc nimRawDispose(p: pointer) {.compilerRtl.} =
# 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))
else:
Expand Down
4 changes: 1 addition & 3 deletions lib/system/assign.nim
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ proc genericReset(dest: pointer, mt: PNimType) =
unsureAsgnRef(cast[PPointer](dest), nil)
of tySequence:
when defined(nimSeqsV2):
var s = cast[ptr NimSeqV2Reimpl](dest)
if s.p != nil:
deallocShared(s.p)
frees(cast[ptr NimSeqV2Reimpl](dest)[])
zeroMem(dest, mt.size)
else:
unsureAsgnRef(cast[PPointer](dest), nil)
Expand Down
16 changes: 8 additions & 8 deletions lib/system/cellseqs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ type
proc add(s: var CellSeq, c: PT; t: PNimTypeV2) {.inline.} =
if s.len >= s.cap:
s.cap = s.cap * 3 div 2
when defined(useMalloc):
var d = cast[CellArray](c_malloc(uint(s.cap * sizeof(CellTuple))))
when compileOption("threads"):
var d = cast[CellArray](allocShared(uint(s.cap * sizeof(CellTuple))))
else:
var d = cast[CellArray](alloc(s.cap * sizeof(CellTuple)))
copyMem(d, s.d, s.len * sizeof(CellTuple))
when defined(useMalloc):
c_free(s.d)
when compileOption("threads"):
deallocShared(s.d)
else:
dealloc(s.d)
s.d = d
Expand All @@ -36,15 +36,15 @@ proc add(s: var CellSeq, c: PT; t: PNimTypeV2) {.inline.} =
proc init(s: var CellSeq, cap: int = 1024) =
s.len = 0
s.cap = cap
when defined(useMalloc):
s.d = cast[CellArray](c_malloc(uint(s.cap * sizeof(CellTuple))))
when compileOption("threads"):
s.d = cast[CellArray](allocShared(uint(s.cap * sizeof(CellTuple))))
else:
s.d = cast[CellArray](alloc(s.cap * sizeof(CellTuple)))

proc deinit(s: var CellSeq) =
if s.d != nil:
when defined(useMalloc):
c_free(s.d)
when compileOption("threads"):
deallocShared(s.d)
else:
dealloc(s.d)
s.d = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/system/memalloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ when hasAlloc and not defined(js):
## from a shared heap.
cast[ptr T](realloc(p, T.sizeof * newSize))

template dealloc*(p: pointer) =
proc dealloc*(p: pointer) {.noconv, compilerproc, rtl, benign, raises: [], tags: [].} =
## Frees the memory allocated with ``alloc``, ``alloc0`` or
## ``realloc``.
##
Expand Down
15 changes: 12 additions & 3 deletions lib/system/seqs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ proc newSeqPayload(cap, elemSize, elemAlign: int): pointer {.compilerRtl, raises
# we have to use type erasure here as Nim does not support generic
# compilerProcs. Oh well, this will all be inlined anyway.
if cap > 0:
var p = cast[ptr NimSeqPayloadBase](allocShared0(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize))
when compileOption("threads"):
var p = cast[ptr NimSeqPayloadBase](allocShared0(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize))
else:
var p = cast[ptr NimSeqPayloadBase](alloc0(align(sizeof(NimSeqPayloadBase), elemAlign) + cap * elemSize))
p.cap = cap
result = p
else:
Expand All @@ -62,14 +65,20 @@ proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize, elemAlign: int): poin
let oldCap = p.cap and not strlitFlag
let newCap = max(resize(oldCap), len+addlen)
if (p.cap and strlitFlag) == strlitFlag:
var q = cast[ptr NimSeqPayloadBase](allocShared0(headerSize + elemSize * newCap))
when compileOption("threads"):
var q = cast[ptr NimSeqPayloadBase](allocShared0(headerSize + elemSize * newCap))
else:
var q = cast[ptr NimSeqPayloadBase](alloc0(headerSize + elemSize * newCap))
copyMem(q +! headerSize, p +! headerSize, len * elemSize)
q.cap = newCap
result = q
else:
let oldSize = headerSize + elemSize * oldCap
let newSize = headerSize + elemSize * newCap
var q = cast[ptr NimSeqPayloadBase](reallocShared0(p, oldSize, newSize))
when compileOption("threads"):
var q = cast[ptr NimSeqPayloadBase](reallocShared0(p, oldSize, newSize))
else:
var q = cast[ptr NimSeqPayloadBase](realloc0(p, oldSize, newSize))
q.cap = newCap
result = q

Expand Down
7 changes: 7 additions & 0 deletions lib/system/seqs_v2_reimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ type
NimSeqV2Reimpl = object
len: int
p: ptr NimSeqPayloadReimpl

template frees(s: NimSeqV2Reimpl) =
if s.p != nil and (s.p.cap and strlitFlag) != strlitFlag:
when compileOption("threads"):
deallocShared(s.p)
else:
dealloc(s.p)
40 changes: 32 additions & 8 deletions lib/system/strs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ template contentSize(cap): int = cap + 1 + sizeof(NimStrPayloadBase)

template frees(s) =
if not isLiteral(s):
deallocShared(s.p)
when compileOption("threads"):
deallocShared(s.p)
else:
dealloc(s.p)

proc resize(old: int): int {.inline.} =
if old <= 0: result = 4
Expand All @@ -40,7 +43,10 @@ proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
if isLiteral(s):
let oldP = s.p
# can't mutate a literal, so we need a fresh copy here:
s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len + addlen)))
when compileOption("threads"):
s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len + addlen)))
else:
s.p = cast[ptr NimStrPayload](alloc0(contentSize(s.len + addlen)))
s.p.cap = s.len + addlen
if s.len > 0:
# we are about to append, so there is no need to copy the \0 terminator:
Expand All @@ -49,7 +55,10 @@ proc prepareAdd(s: var NimStringV2; addlen: int) {.compilerRtl.} =
let oldCap = s.p.cap and not strlitFlag
if s.len + addlen > oldCap:
let newCap = max(s.len + addlen, resize(oldCap))
s.p = cast[ptr NimStrPayload](reallocShared0(s.p, contentSize(oldCap), contentSize(newCap)))
when compileOption("threads"):
s.p = cast[ptr NimStrPayload](reallocShared0(s.p, contentSize(oldCap), contentSize(newCap)))
else:
s.p = cast[ptr NimStrPayload](realloc0(s.p, contentSize(oldCap), contentSize(newCap)))
s.p.cap = newCap

proc nimAddCharV1(s: var NimStringV2; c: char) {.compilerRtl, inline.} =
Expand All @@ -63,7 +72,10 @@ proc toNimStr(str: cstring, len: int): NimStringV2 {.compilerproc.} =
if len <= 0:
result = NimStringV2(len: 0, p: nil)
else:
var p = cast[ptr NimStrPayload](allocShared0(contentSize(len)))
when compileOption("threads"):
var p = cast[ptr NimStrPayload](allocShared0(contentSize(len)))
else:
var p = cast[ptr NimStrPayload](alloc0(contentSize(len)))
p.cap = len
if len > 0:
# we are about to append, so there is no need to copy the \0 terminator:
Expand Down Expand Up @@ -94,15 +106,21 @@ proc rawNewString(space: int): NimStringV2 {.compilerproc.} =
if space <= 0:
result = NimStringV2(len: 0, p: nil)
else:
var p = cast[ptr NimStrPayload](allocShared0(contentSize(space)))
when compileOption("threads"):
var p = cast[ptr NimStrPayload](allocShared0(contentSize(space)))
else:
var p = cast[ptr NimStrPayload](alloc0(contentSize(space)))
p.cap = space
result = NimStringV2(len: 0, p: p)

proc mnewString(len: int): NimStringV2 {.compilerproc.} =
if len <= 0:
result = NimStringV2(len: 0, p: nil)
else:
var p = cast[ptr NimStrPayload](allocShared0(contentSize(len)))
when compileOption("threads"):
var p = cast[ptr NimStrPayload](allocShared0(contentSize(len)))
else:
var p = cast[ptr NimStrPayload](alloc0(contentSize(len)))
p.cap = len
result = NimStringV2(len: len, p: p)

Expand All @@ -128,15 +146,21 @@ proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} =
# 'let y = newStringOfCap(); var x = y'
# on the other hand... These get turned into moves now.
frees(a)
a.p = cast[ptr NimStrPayload](allocShared0(contentSize(b.len)))
when compileOption("threads"):
a.p = cast[ptr NimStrPayload](allocShared0(contentSize(b.len)))
else:
a.p = cast[ptr NimStrPayload](alloc0(contentSize(b.len)))
a.p.cap = b.len
a.len = b.len
copyMem(unsafeAddr a.p.data[0], unsafeAddr b.p.data[0], b.len+1)

proc nimPrepareStrMutationImpl(s: var NimStringV2) =
let oldP = s.p
# can't mutate a literal, so we need a fresh copy here:
s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len)))
when compileOption("threads"):
s.p = cast[ptr NimStrPayload](allocShared0(contentSize(s.len)))
else:
s.p = cast[ptr NimStrPayload](alloc0(contentSize(s.len)))
s.p.cap = s.len
copyMem(unsafeAddr s.p.data[0], unsafeAddr oldP.data[0], s.len+1)

Expand Down
10 changes: 8 additions & 2 deletions lib/system/widestrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ when defined(nimv2):

proc `=destroy`(a: var WideCStringObj) =
if a.data != nil:
deallocShared(a.data)
when compileOption("threads"):
deallocShared(a.data)
else:
dealloc(a.data)

proc `=`(a: var WideCStringObj; b: WideCStringObj) {.error.}

Expand All @@ -37,7 +40,10 @@ when defined(nimv2):

proc createWide(a: var WideCStringObj; bytes: int) =
a.bytes = bytes
a.data = cast[typeof(a.data)](allocShared0(bytes))
when compileOption("threads"):
a.data = cast[typeof(a.data)](allocShared0(bytes))
else:
a.data = cast[typeof(a.data)](alloc0(bytes))

template `[]`*(a: WideCStringObj; idx: int): Utf16Char = a.data[idx]
template `[]=`*(a: WideCStringObj; idx: int; val: Utf16Char) = a.data[idx] = val
Expand Down

0 comments on commit 833035b

Please sign in to comment.