summary refs log tree commit diff stats
path: root/lib/core/allocators.nim
blob: 962a99fd60e3255d584406fceb88fe91ef49498d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#
#
#            Nim's Runtime Library
#        (c) Copyright 2017 Nim contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

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 AllocatorObj
  AllocatorObj* {.inheritable, compilerproc.} = object
    alloc*: proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [], tags: [].}
    dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [], tags: [].}
    realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [], tags: [].}
    deallocAll*: proc (a: Allocator) {.nimcall, raises: [], tags: [].}
    flags*: set[AllocatorFlag]
    name*: cstring
    allocCount: int
    deallocCount: int

var
  localAllocator {.threadvar.}: Allocator
  sharedAllocator: Allocator
  allocatorStorage {.threadvar.}: AllocatorObj

when defined(useMalloc) and not defined(nimscript):
  import "system/ansi_c"

import "system/memory"

template `+!`(p: pointer, s: int): pointer =
  cast[pointer](cast[int](p) +% s)

proc getLocalAllocator*(): Allocator =
  result = localAllocator
  if result == nil:
    result = addr allocatorStorage
    result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [].} =
      when defined(useMalloc) and not defined(nimscript):
        result = c_malloc(size)
        # XXX do we need this?
        nimZeroMem(result, size)
      else:
        result = system.alloc0(size)
      inc a.allocCount
    result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [].} =
      when defined(useMalloc) and not defined(nimscript):
        c_free(p)
      else:
        system.dealloc(p)
      inc a.deallocCount
    result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [].} =
      when defined(useMalloc) and not defined(nimscript):
        result = c_realloc(p, newSize)
      else:
        result = system.realloc(p, newSize)
      nimZeroMem(result +! oldSize, newSize - oldSize)
    result.deallocAll = nil
    result.flags = {ThreadLocal, ZerosMem}
    result.name = "nim_local"
    localAllocator = result

proc setLocalAllocator*(a: Allocator) =
  localAllocator = a

proc getSharedAllocator*(): Allocator =
  result = sharedAllocator

proc setSharedAllocator*(a: Allocator) =
  sharedAllocator = a

proc allocCounters*(): (int, int) =
  let a = getLocalAllocator()
  result = (a.allocCount, a.deallocCount)