summary refs log tree commit diff stats
path: root/lib/core/allocators.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/core/allocators.nim')
-rw-r--r--lib/core/allocators.nim35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/core/allocators.nim b/lib/core/allocators.nim
new file mode 100644
index 000000000..d6608a203
--- /dev/null
+++ b/lib/core/allocators.nim
@@ -0,0 +1,35 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2017 Nim contributors
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+type
+  Allocator* {.inheritable.} = ptr object
+    alloc*: proc (a: Allocator; size: int; alignment = 8): pointer {.nimcall.}
+    dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall.}
+    realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.}
+
+var
+  currentAllocator {.threadvar.}: Allocator
+
+proc getCurrentAllocator*(): Allocator =
+  result = currentAllocator
+
+proc setCurrentAllocator*(a: Allocator) =
+  currentAllocator = a
+
+proc alloc*(size: int): pointer =
+  let a = getCurrentAllocator()
+  result = a.alloc(a, size)
+
+proc dealloc*(p: pointer; size: int) =
+  let a = getCurrentAllocator()
+  a.dealloc(a, size)
+
+proc realloc*(p: pointer; oldSize, newSize: int): pointer =
+  let a = getCurrentAllocator()
+  result = a.realloc(a, oldSize, newSize)