summary refs log tree commit diff stats
path: root/lib/core/allocators.nim
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/core/allocators.nim
parentab38c075f8864a8ddc129e3a4490f11f48d79d38 (diff)
downloadNim-7d82df20be6db24d54ebe8963805f66e8f3c75f7.tar.gz
gc:destructors further progress
Diffstat (limited to 'lib/core/allocators.nim')
-rw-r--r--lib/core/allocators.nim15
1 files changed, 14 insertions, 1 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