summary refs log tree commit diff stats
path: root/lib/core/seqs.nim
diff options
context:
space:
mode:
authorAndrii Riabushenko <cdome@bk.ru>2018-11-29 23:41:38 +0000
committerAndrii Riabushenko <cdome@bk.ru>2018-11-29 23:41:38 +0000
commit4c327d9ae235e11ca94153a734f481b1f55e1789 (patch)
tree6cb869823f9dc4fa0f8df2e9b63f08ad6e8cc950 /lib/core/seqs.nim
parent9bba790534be76c6b50033d54d061eb076f35d9d (diff)
parent350396e1cab824ec69a9a06380ff15a7fa799819 (diff)
downloadNim-4c327d9ae235e11ca94153a734f481b1f55e1789.tar.gz
merge devel
Diffstat (limited to 'lib/core/seqs.nim')
-rw-r--r--lib/core/seqs.nim17
1 files changed, 9 insertions, 8 deletions
diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim
index c62a05904..a41ef10ab 100644
--- a/lib/core/seqs.nim
+++ b/lib/core/seqs.nim
@@ -85,7 +85,7 @@ type
 proc newSeqPayload(cap, elemSize: int): pointer {.compilerRtl.} =
   # 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:
+  if cap > 0:
     let region = getLocalAllocator()
     var p = cast[ptr PayloadBase](region.alloc(region, cap * elemSize + sizeof(int) + sizeof(Allocator)))
     p.region = region
@@ -126,18 +126,19 @@ proc grow*[T](x: var seq[T]; newLen: Natural; value: T) =
   let oldLen = x.len
   if newLen <= oldLen: return
   var xu = cast[ptr NimSeqV2[T]](addr x)
-
-  xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T)))
+  if xu.p == nil or xu.p.cap < newLen:
+    xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newLen - oldLen, sizeof(T)))
   xu.len = newLen
   for i in oldLen .. newLen-1:
     xu.p.data[i] = value
 
 proc setLen[T](s: var seq[T], newlen: Natural) =
-  if newlen < s.len:
-    shrink(s, newLen)
-  else:
-    var v: T # get the default value of 'v'
-    grow(s, newLen, v)
+  {.noSideEffect.}:
+    if newlen < s.len:
+      shrink(s, newLen)
+    else:
+      var v: T # get the default value of 'v'
+      grow(s, newLen, v)
 
 when false:
   proc resize[T](s: var NimSeqV2[T]) =