summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/ccgexprs.nim24
-rw-r--r--lib/system/arc.nim8
-rw-r--r--lib/system/assign.nim4
-rw-r--r--lib/system/cellseqs_v2.nim16
-rw-r--r--lib/system/memalloc.nim2
-rw-r--r--lib/system/seqs_v2.nim15
-rw-r--r--lib/system/seqs_v2_reimpl.nim7
-rw-r--r--lib/system/strs_v2.nim40
-rw-r--r--lib/system/widestrs.nim10
9 files changed, 85 insertions, 41 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index c1469fa60..004b8f41e 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -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)
diff --git a/lib/system/arc.nim b/lib/system/arc.nim
index 3e24c2506..46579eaef 100644
--- a/lib/system/arc.nim
+++ b/lib/system/arc.nim
@@ -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:
@@ -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:
@@ -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:
diff --git a/lib/system/assign.nim b/lib/system/assign.nim
index d332124cd..a809fa423 100644
--- a/lib/system/assign.nim
+++ b/lib/system/assign.nim
@@ -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)
diff --git a/lib/system/cellseqs_v2.nim b/lib/system/cellseqs_v2.nim
index fdd9e3099..59cf67dcd 100644
--- a/lib/system/cellseqs_v2.nim
+++ b/lib/system/cellseqs_v2.nim
@@ -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
@@ -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
diff --git a/lib/system/memalloc.nim b/lib/system/memalloc.nim
index 178d199b8..142762fe7 100644
--- a/lib/system/memalloc.nim
+++ b/lib/system/memalloc.nim
@@ -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``.
     ##
diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim
index 111299fff..d83a0009a 100644
--- a/lib/system/seqs_v2.nim
+++ b/lib/system/seqs_v2.nim
@@ -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:
@@ -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
 
diff --git a/lib/system/seqs_v2_reimpl.nim b/lib/system/seqs_v2_reimpl.nim
index 750f78c0d..09b7e7ac4 100644
--- a/lib/system/seqs_v2_reimpl.nim
+++ b/lib/system/seqs_v2_reimpl.nim
@@ -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)
\ No newline at end of file
diff --git a/lib/system/strs_v2.nim b/lib/system/strs_v2.nim
index 57fdd5ee1..f51791da2 100644
--- a/lib/system/strs_v2.nim
+++ b/lib/system/strs_v2.nim
@@ -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
@@ -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:
@@ -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.} =
@@ -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:
@@ -94,7 +106,10 @@ 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)
 
@@ -102,7 +117,10 @@ 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)
 
@@ -128,7 +146,10 @@ 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)
@@ -136,7 +157,10 @@ proc nimAsgnStrV2(a: var NimStringV2, b: NimStringV2) {.compilerRtl.} =
 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)
 
diff --git a/lib/system/widestrs.nim b/lib/system/widestrs.nim
index 83c11eb79..6d54bce9b 100644
--- a/lib/system/widestrs.nim
+++ b/lib/system/widestrs.nim
@@ -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.}
 
@@ -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