summary refs log blame commit diff stats
path: root/lib/core/allocators.nim
blob: 62f5e9756b4b7146d1683ab762adfb02da97c733 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                   
                                         
                                                                                   

                                                                                         









                                           
                                                     
                               
                                      


                                      
                       


                                                           
                                            
#
#
#            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* = ptr object {.inheritable.}
    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.}

var
  currentAllocator {.threadvar.}: Allocator

proc getCurrentAllocator*(): Allocator =
  result = currentAllocator

proc setCurrentAllocator*(a: Allocator) =
  currentAllocator = a

proc alloc*(size: int; alignment: int = 8): pointer =
  let a = getCurrentAllocator()
  result = a.alloc(a, size, alignment)

proc dealloc*(p: pointer; size: int) =
  let a = getCurrentAllocator()
  a.dealloc(a, p, size)

proc realloc*(p: pointer; oldSize, newSize: int): pointer =
  let a = getCurrentAllocator()
  result = a.realloc(a, p, oldSize, newSize)