summary refs log tree commit diff stats
path: root/lib/core/allocators.nim
diff options
context:
space:
mode:
authorDaniil Yarancev <21169548+Yardanico@users.noreply.github.com>2018-01-07 21:02:00 +0300
committerGitHub <noreply@github.com>2018-01-07 21:02:00 +0300
commitfb44c522e6173528efa8035ecc459c84887d0167 (patch)
treea2f5e98606be265981a5f72748896967033e23d7 /lib/core/allocators.nim
parentccf99fa5ce4fe992fb80dc89271faa51456c3fa5 (diff)
parente23ea64c41e101d4e1d933f0b015f51cc6c2f7de (diff)
downloadNim-fb44c522e6173528efa8035ecc459c84887d0167.tar.gz
Merge pull request #1 from nim-lang/devel
upstream
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)