boehm.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. proc boehmGCinit {.importc: "GC_init", boehmGC.}
  2. proc boehmGC_disable {.importc: "GC_disable", boehmGC.}
  3. proc boehmGC_enable {.importc: "GC_enable", boehmGC.}
  4. proc boehmGCincremental {.
  5. importc: "GC_enable_incremental", boehmGC.}
  6. proc boehmGCfullCollect {.importc: "GC_gcollect", boehmGC.}
  7. proc boehmGC_set_all_interior_pointers(flag: cint) {.
  8. importc: "GC_set_all_interior_pointers", boehmGC.}
  9. proc boehmAlloc(size: int): pointer {.importc: "GC_malloc", boehmGC.}
  10. proc boehmAllocAtomic(size: int): pointer {.
  11. importc: "GC_malloc_atomic", boehmGC.}
  12. proc boehmRealloc(p: pointer, size: int): pointer {.
  13. importc: "GC_realloc", boehmGC.}
  14. proc boehmDealloc(p: pointer) {.importc: "GC_free", boehmGC.}
  15. when hasThreadSupport:
  16. proc boehmGC_allow_register_threads {.
  17. importc: "GC_allow_register_threads", boehmGC.}
  18. proc boehmGetHeapSize: int {.importc: "GC_get_heap_size", boehmGC.}
  19. ## Return the number of bytes in the heap. Excludes collector private
  20. ## data structures. Includes empty blocks and fragmentation loss.
  21. ## Includes some pages that were allocated but never written.
  22. proc boehmGetFreeBytes: int {.importc: "GC_get_free_bytes", boehmGC.}
  23. ## Return a lower bound on the number of free bytes in the heap.
  24. proc boehmGetBytesSinceGC: int {.importc: "GC_get_bytes_since_gc", boehmGC.}
  25. ## Return the number of bytes allocated since the last collection.
  26. proc boehmGetTotalBytes: int {.importc: "GC_get_total_bytes", boehmGC.}
  27. ## Return the total number of bytes allocated in this process.
  28. ## Never decreases.
  29. proc boehmRegisterFinalizer(obj, ff, cd, off, ocd: pointer) {.importc: "GC_register_finalizer", boehmGC.}
  30. proc allocAtomic(size: int): pointer =
  31. result = boehmAllocAtomic(size)
  32. zeroMem(result, size)
  33. when not defined(useNimRtl):
  34. proc allocImpl(size: Natural): pointer =
  35. result = boehmAlloc(size)
  36. if result == nil: raiseOutOfMem()
  37. proc alloc0Impl(size: Natural): pointer =
  38. result = alloc(size)
  39. proc reallocImpl(p: pointer, newSize: Natural): pointer =
  40. result = boehmRealloc(p, newSize)
  41. if result == nil: raiseOutOfMem()
  42. proc realloc0Impl(p: pointer, oldSize, newSize: Natural): pointer =
  43. result = boehmRealloc(p, newSize)
  44. if result == nil: raiseOutOfMem()
  45. if newSize > oldSize:
  46. zeroMem(cast[pointer](cast[int](result) + oldSize), newSize - oldSize)
  47. proc deallocImpl(p: pointer) = boehmDealloc(p)
  48. proc allocSharedImpl(size: Natural): pointer = allocImpl(size)
  49. proc allocShared0Impl(size: Natural): pointer = alloc0Impl(size)
  50. proc reallocSharedImpl(p: pointer, newSize: Natural): pointer = reallocImpl(p, newSize)
  51. proc reallocShared0Impl(p: pointer, oldSize, newSize: Natural): pointer = realloc0Impl(p, oldSize, newSize)
  52. proc deallocSharedImpl(p: pointer) = deallocImpl(p)
  53. when hasThreadSupport:
  54. proc getFreeSharedMem(): int =
  55. boehmGetFreeBytes()
  56. proc getTotalSharedMem(): int =
  57. boehmGetHeapSize()
  58. proc getOccupiedSharedMem(): int =
  59. getTotalSharedMem() - getFreeSharedMem()
  60. #boehmGCincremental()
  61. proc GC_disable() = boehmGC_disable()
  62. proc GC_enable() = boehmGC_enable()
  63. proc GC_fullCollect() = boehmGCfullCollect()
  64. proc GC_setStrategy(strategy: GC_Strategy) = discard
  65. proc GC_enableMarkAndSweep() = discard
  66. proc GC_disableMarkAndSweep() = discard
  67. proc GC_getStatistics(): string = return ""
  68. proc getOccupiedMem(): int = return boehmGetHeapSize()-boehmGetFreeBytes()
  69. proc getFreeMem(): int = return boehmGetFreeBytes()
  70. proc getTotalMem(): int = return boehmGetHeapSize()
  71. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  72. proc initGC() =
  73. when defined(boehmNoIntPtr):
  74. # See #12286
  75. boehmGC_set_all_interior_pointers(0)
  76. boehmGCinit()
  77. when hasThreadSupport:
  78. boehmGC_allow_register_threads()
  79. proc boehmgc_finalizer(obj: pointer, typedFinalizer: (proc(x: pointer) {.cdecl.})) =
  80. typedFinalizer(obj)
  81. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  82. if ntfNoRefs in typ.flags: result = allocAtomic(size)
  83. else: result = alloc(size)
  84. if typ.finalizer != nil:
  85. boehmRegisterFinalizer(result, boehmgc_finalizer, typ.finalizer, nil, nil)
  86. {.push overflowChecks: on.}
  87. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  88. result = newObj(typ, align(GenericSeqSize, typ.base.align) + len * typ.base.size)
  89. cast[PGenericSeq](result).len = len
  90. cast[PGenericSeq](result).reserved = len
  91. {.pop.}
  92. proc growObj(old: pointer, newsize: int): pointer =
  93. result = realloc(old, newsize)
  94. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  95. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  96. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  97. dest[] = src
  98. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  99. dest[] = src
  100. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  101. deprecated: "old compiler compat".} = asgnRef(dest, src)
  102. type
  103. MemRegion = object
  104. proc alloc(r: var MemRegion, size: int): pointer =
  105. result = boehmAlloc(size)
  106. if result == nil: raiseOutOfMem()
  107. proc alloc0(r: var MemRegion, size: int): pointer =
  108. result = alloc(size)
  109. zeroMem(result, size)
  110. proc dealloc(r: var MemRegion, p: pointer) = boehmDealloc(p)
  111. proc deallocOsPages(r: var MemRegion) {.inline.} = discard
  112. proc deallocOsPages() {.inline.} = discard
  113. include "system/cellsets"