summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-11-29 01:13:24 +0100
committerAndreas Rumpf <rumpf_a@web.de>2018-11-29 01:13:32 +0100
commit7d82df20be6db24d54ebe8963805f66e8f3c75f7 (patch)
tree4e5f741fc932045915180a4605245c6394a8cde9 /lib
parentab38c075f8864a8ddc129e3a4490f11f48d79d38 (diff)
downloadNim-7d82df20be6db24d54ebe8963805f66e8f3c75f7.tar.gz
gc:destructors further progress
Diffstat (limited to 'lib')
-rw-r--r--lib/core/allocators.nim15
-rw-r--r--lib/core/seqs.nim2
2 files changed, 15 insertions, 2 deletions
diff --git a/lib/core/allocators.nim b/lib/core/allocators.nim
index f652f0d85..9d79c9c32 100644
--- a/lib/core/allocators.nim
+++ b/lib/core/allocators.nim
@@ -11,7 +11,8 @@ type
   AllocatorFlag* {.pure.} = enum  ## flags describing the properties of the allocator
     ThreadLocal ## the allocator is thread local only.
     ZerosMem    ## the allocator always zeros the memory on an allocation
-  Allocator* = ptr object {.inheritable.}
+  Allocator* = ptr AllocatorObj
+  AllocatorObj* {.inheritable.} = object
     alloc*: proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.}
     dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall.}
     realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.}
@@ -21,9 +22,21 @@ type
 var
   localAllocator {.threadvar.}: Allocator
   sharedAllocator: Allocator
+  allocatorStorage {.threadvar.}: AllocatorObj
 
 proc getLocalAllocator*(): Allocator =
   result = localAllocator
+  if result == nil:
+    result = addr allocatorStorage
+    result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.} =
+      result = system.alloc(size)
+    result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall.} =
+      system.dealloc(p)
+    result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} =
+      result = system.realloc(p, newSize)
+    result.deallocAll = nil
+    result.flags = {ThreadLocal}
+    localAllocator = result
 
 proc setLocalAllocator*(a: Allocator) =
   localAllocator = a
diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim
index c62a05904..2d2b14c7b 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