mmdisp.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Nim high-level memory manager: It supports Boehm's GC, Go's GC, no GC and the
  10. # native Nim GC. The native Nim GC is the default.
  11. #{.push checks:on, assertions:on.}
  12. {.push checks:off.}
  13. const
  14. debugGC = false # we wish to debug the GC...
  15. logGC = false
  16. traceGC = false # extensive debugging
  17. alwaysCycleGC = defined(nimSmokeCycles)
  18. alwaysGC = defined(nimFulldebug) # collect after every memory
  19. # allocation (for debugging)
  20. leakDetector = defined(nimLeakDetector)
  21. overwriteFree = defined(nimBurnFree) # overwrite memory with 0xFF before free
  22. trackAllocationSource = leakDetector
  23. cycleGC = true # (de)activate the cycle GC
  24. reallyDealloc = true # for debugging purposes this can be set to false
  25. reallyOsDealloc = true
  26. coalescRight = true
  27. coalescLeft = true
  28. logAlloc = false
  29. useCellIds = defined(nimCorruption)
  30. type
  31. PPointer = ptr pointer
  32. ByteArray = UncheckedArray[byte]
  33. PByte = ptr ByteArray
  34. PString = ptr string
  35. # Page size of the system; in most cases 4096 bytes. For exotic OS or
  36. # CPU this needs to be changed:
  37. const
  38. PageShift = when defined(cpu16): 8 else: 12 # \
  39. # my tests showed no improvements for using larger page sizes.
  40. PageSize = 1 shl PageShift
  41. PageMask = PageSize-1
  42. MemAlign = 8 # also minimal allocatable memory block
  43. BitsPerPage = PageSize div MemAlign
  44. UnitsPerPage = BitsPerPage div (sizeof(int)*8)
  45. # how many ints do we need to describe a page:
  46. # on 32 bit systems this is only 16 (!)
  47. TrunkShift = 9
  48. BitsPerTrunk = 1 shl TrunkShift # needs to be power of 2 and divisible by 64
  49. TrunkMask = BitsPerTrunk - 1
  50. IntsPerTrunk = BitsPerTrunk div (sizeof(int)*8)
  51. IntShift = 5 + ord(sizeof(int) == 8) # 5 or 6, depending on int width
  52. IntMask = 1 shl IntShift - 1
  53. proc raiseOutOfMem() {.noinline.} =
  54. if outOfMemHook != nil: outOfMemHook()
  55. cstderr.rawWrite("out of memory\n")
  56. quit(1)
  57. when defined(boehmgc):
  58. include system / mm / boehm
  59. elif defined(gogc):
  60. include system / mm / go
  61. elif (defined(nogc) or defined(gcDestructors)) and defined(useMalloc):
  62. include system / mm / malloc
  63. elif defined(nogc):
  64. include system / mm / none
  65. else:
  66. when not defined(gcRegions):
  67. include "system/alloc"
  68. when not usesDestructors:
  69. include "system/cellsets"
  70. when not leakDetector and not useCellIds:
  71. sysAssert(sizeof(Cell) == sizeof(FreeCell), "sizeof FreeCell")
  72. when compileOption("gc", "v2"):
  73. include "system/gc2"
  74. elif defined(gcRegions):
  75. # XXX due to bootstrapping reasons, we cannot use compileOption("gc", "stack") here
  76. include "system/gc_regions"
  77. elif defined(nimV2) or usesDestructors:
  78. var allocator {.rtlThreadVar.}: MemRegion
  79. instantiateForRegion(allocator)
  80. when defined(gcHooks):
  81. include "system/gc_hooks"
  82. elif defined(gcMarkAndSweep):
  83. # XXX use 'compileOption' here
  84. include "system/gc_ms"
  85. else:
  86. include "system/gc"
  87. when not declared(nimNewSeqOfCap) and not defined(nimSeqsV2):
  88. {.push overflowChecks: on.}
  89. proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
  90. when defined(gcRegions):
  91. let s = cap * typ.base.size # newStr already adds GenericSeqSize
  92. result = newStr(typ, s, ntfNoRefs notin typ.base.flags)
  93. else:
  94. let s = cap * typ.base.size + GenericSeqSize
  95. when declared(newObjNoInit):
  96. result = if ntfNoRefs in typ.base.flags: newObjNoInit(typ, s) else: newObj(typ, s)
  97. else:
  98. result = newObj(typ, s)
  99. cast[PGenericSeq](result).len = 0
  100. cast[PGenericSeq](result).reserved = cap
  101. {.pop.}
  102. {.pop.}
  103. when not declared(ForeignCell):
  104. type ForeignCell* = object
  105. data*: pointer
  106. proc protect*(x: pointer): ForeignCell = ForeignCell(data: x)
  107. proc dispose*(x: ForeignCell) = discard
  108. proc isNotForeign*(x: ForeignCell): bool = false