mmdisp.nim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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(smokeCycles)
  18. alwaysGC = defined(fulldebug) # collect after every memory
  19. # allocation (for debugging)
  20. leakDetector = defined(leakDetector)
  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(corruption)
  30. type
  31. PPointer = ptr pointer
  32. ByteArray = UncheckedArray[byte]
  33. PByte = ptr ByteArray
  34. PString = ptr string
  35. {.deprecated: [TByteArray: ByteArray].}
  36. # Page size of the system; in most cases 4096 bytes. For exotic OS or
  37. # CPU this needs to be changed:
  38. const
  39. PageShift = when defined(cpu16): 8 else: 12
  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. echo("out of memory")
  56. quit(1)
  57. when defined(boehmgc):
  58. proc boehmGCinit {.importc: "GC_init", boehmGC.}
  59. proc boehmGC_disable {.importc: "GC_disable", boehmGC.}
  60. proc boehmGC_enable {.importc: "GC_enable", boehmGC.}
  61. proc boehmGCincremental {.
  62. importc: "GC_enable_incremental", boehmGC.}
  63. proc boehmGCfullCollect {.importc: "GC_gcollect", boehmGC.}
  64. proc boehmAlloc(size: int): pointer {.importc: "GC_malloc", boehmGC.}
  65. proc boehmAllocAtomic(size: int): pointer {.
  66. importc: "GC_malloc_atomic", boehmGC.}
  67. proc boehmRealloc(p: pointer, size: int): pointer {.
  68. importc: "GC_realloc", boehmGC.}
  69. proc boehmDealloc(p: pointer) {.importc: "GC_free", boehmGC.}
  70. when hasThreadSupport:
  71. proc boehmGC_allow_register_threads {.
  72. importc: "GC_allow_register_threads", boehmGC.}
  73. proc boehmGetHeapSize: int {.importc: "GC_get_heap_size", boehmGC.}
  74. ## Return the number of bytes in the heap. Excludes collector private
  75. ## data structures. Includes empty blocks and fragmentation loss.
  76. ## Includes some pages that were allocated but never written.
  77. proc boehmGetFreeBytes: int {.importc: "GC_get_free_bytes", boehmGC.}
  78. ## Return a lower bound on the number of free bytes in the heap.
  79. proc boehmGetBytesSinceGC: int {.importc: "GC_get_bytes_since_gc", boehmGC.}
  80. ## Return the number of bytes allocated since the last collection.
  81. proc boehmGetTotalBytes: int {.importc: "GC_get_total_bytes", boehmGC.}
  82. ## Return the total number of bytes allocated in this process.
  83. ## Never decreases.
  84. proc allocAtomic(size: int): pointer =
  85. result = boehmAllocAtomic(size)
  86. zeroMem(result, size)
  87. when not defined(useNimRtl):
  88. proc alloc(size: Natural): pointer =
  89. result = boehmAlloc(size)
  90. if result == nil: raiseOutOfMem()
  91. proc alloc0(size: Natural): pointer =
  92. result = alloc(size)
  93. zeroMem(result, size)
  94. proc realloc(p: pointer, newsize: Natural): pointer =
  95. result = boehmRealloc(p, newsize)
  96. if result == nil: raiseOutOfMem()
  97. proc dealloc(p: pointer) = boehmDealloc(p)
  98. proc allocShared(size: Natural): pointer =
  99. result = boehmAlloc(size)
  100. if result == nil: raiseOutOfMem()
  101. proc allocShared0(size: Natural): pointer =
  102. result = alloc(size)
  103. zeroMem(result, size)
  104. proc reallocShared(p: pointer, newsize: Natural): pointer =
  105. result = boehmRealloc(p, newsize)
  106. if result == nil: raiseOutOfMem()
  107. proc deallocShared(p: pointer) = boehmDealloc(p)
  108. when hasThreadSupport:
  109. proc getFreeSharedMem(): int =
  110. boehmGetFreeBytes()
  111. proc getTotalSharedMem(): int =
  112. boehmGetHeapSize()
  113. proc getOccupiedSharedMem(): int =
  114. getTotalSharedMem() - getFreeSharedMem()
  115. #boehmGCincremental()
  116. proc GC_disable() = boehmGC_disable()
  117. proc GC_enable() = boehmGC_enable()
  118. proc GC_fullCollect() = boehmGCfullCollect()
  119. proc GC_setStrategy(strategy: GC_Strategy) = discard
  120. proc GC_enableMarkAndSweep() = discard
  121. proc GC_disableMarkAndSweep() = discard
  122. proc GC_getStatistics(): string = return ""
  123. proc getOccupiedMem(): int = return boehmGetHeapSize()-boehmGetFreeBytes()
  124. proc getFreeMem(): int = return boehmGetFreeBytes()
  125. proc getTotalMem(): int = return boehmGetHeapSize()
  126. proc setStackBottom(theStackBottom: pointer) = discard
  127. proc initGC() =
  128. boehmGCinit()
  129. when hasThreadSupport:
  130. boehmGC_allow_register_threads()
  131. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  132. if ntfNoRefs in typ.flags: result = allocAtomic(size)
  133. else: result = alloc(size)
  134. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  135. result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
  136. cast[PGenericSeq](result).len = len
  137. cast[PGenericSeq](result).reserved = len
  138. proc growObj(old: pointer, newsize: int): pointer =
  139. result = realloc(old, newsize)
  140. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  141. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  142. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  143. dest[] = src
  144. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  145. dest[] = src
  146. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  147. dest[] = src
  148. type
  149. MemRegion = object {.final, pure.}
  150. {.deprecated: [TMemRegion: MemRegion].}
  151. proc alloc(r: var MemRegion, size: int): pointer =
  152. result = boehmAlloc(size)
  153. if result == nil: raiseOutOfMem()
  154. proc alloc0(r: var MemRegion, size: int): pointer =
  155. result = alloc(size)
  156. zeroMem(result, size)
  157. proc dealloc(r: var MemRegion, p: pointer) = boehmDealloc(p)
  158. proc deallocOsPages(r: var MemRegion) {.inline.} = discard
  159. proc deallocOsPages() {.inline.} = discard
  160. include "system/cellsets"
  161. elif defined(gogc):
  162. when defined(windows):
  163. const goLib = "libgo.dll"
  164. elif defined(macosx):
  165. const goLib = "libgo.dylib"
  166. else:
  167. const goLib = "libgo.so"
  168. proc roundup(x, v: int): int {.inline.} =
  169. result = (x + (v-1)) and not (v-1)
  170. proc initGC() = discard
  171. # runtime_setgcpercent is only available in GCC 5
  172. proc GC_disable() = discard
  173. proc GC_enable() = discard
  174. proc goRuntimeGC(force: int32) {.importc: "runtime_gc", dynlib: goLib.}
  175. proc GC_fullCollect() = goRuntimeGC(2)
  176. proc GC_setStrategy(strategy: GC_Strategy) = discard
  177. proc GC_enableMarkAndSweep() = discard
  178. proc GC_disableMarkAndSweep() = discard
  179. const
  180. goNumSizeClasses = 67
  181. type
  182. cbool {.importc: "_Bool", nodecl.} = bool
  183. goMStats_inner_struct = object
  184. size: uint32
  185. nmalloc: uint64
  186. nfree: uint64
  187. goMStats = object
  188. # General statistics.
  189. alloc: uint64 # bytes allocated and still in use
  190. total_alloc: uint64 # bytes allocated (even if freed)
  191. sys: uint64 # bytes obtained from system (should be sum of xxx_sys below, no locking, approximate)
  192. nlookup: uint64 # number of pointer lookups
  193. nmalloc: uint64 # number of mallocs
  194. nfree: uint64 # number of frees
  195. # Statistics about malloc heap.
  196. # protected by mheap.Lock
  197. heap_alloc: uint64 # bytes allocated and still in use
  198. heap_sys: uint64 # bytes obtained from system
  199. heap_idle: uint64 # bytes in idle spans
  200. heap_inuse: uint64 # bytes in non-idle spans
  201. heap_released: uint64 # bytes released to the OS
  202. heap_objects: uint64 # total number of allocated objects
  203. # Statistics about allocation of low-level fixed-size structures.
  204. # Protected by FixAlloc locks.
  205. stacks_inuse: uint64 # bootstrap stacks
  206. stacks_sys: uint64
  207. mspan_inuse: uint64 # MSpan structures
  208. mspan_sys: uint64
  209. mcache_inuse: uint64 # MCache structures
  210. mcache_sys: uint64
  211. buckhash_sys: uint64 # profiling bucket hash table
  212. gc_sys: uint64
  213. other_sys: uint64
  214. # Statistics about garbage collector.
  215. # Protected by mheap or stopping the world during GC.
  216. next_gc: uint64 # next GC (in heap_alloc time)
  217. last_gc: uint64 # last GC (in absolute time)
  218. pause_total_ns: uint64
  219. pause_ns: array[256, uint64] # circular buffer of recent gc pause lengths
  220. pause_end: array[256, uint64] # circular buffer of recent gc end times (nanoseconds since 1970)
  221. numgc: uint32
  222. numforcedgc: uint32 # number of user-forced GCs
  223. gc_cpu_fraction: float64 # fraction of CPU time used by GC
  224. enablegc: cbool
  225. debuggc: cbool
  226. # Statistics about allocation size classes.
  227. by_size: array[goNumSizeClasses, goMStats_inner_struct]
  228. # Statistics below here are not exported to MemStats directly.
  229. tinyallocs: uint64 # number of tiny allocations that didn't cause actual allocation; not exported to go directly
  230. gc_trigger: uint64
  231. heap_live: uint64
  232. heap_scan: uint64
  233. heap_marked: uint64
  234. proc goRuntime_ReadMemStats(a2: ptr goMStats) {.cdecl,
  235. importc: "runtime_ReadMemStats",
  236. codegenDecl: "$1 $2$3 __asm__ (\"runtime.ReadMemStats\");\n$1 $2$3",
  237. dynlib: goLib.}
  238. proc GC_getStatistics(): string =
  239. var mstats: goMStats
  240. goRuntime_ReadMemStats(addr mstats)
  241. result = "[GC] total allocated memory: " & $(mstats.total_alloc) & "\n" &
  242. "[GC] total memory obtained from system: " & $(mstats.sys) & "\n" &
  243. "[GC] occupied memory: " & $(mstats.alloc) & "\n" &
  244. "[GC] number of pointer lookups: " & $(mstats.nlookup) & "\n" &
  245. "[GC] number of mallocs: " & $(mstats.nmalloc) & "\n" &
  246. "[GC] number of frees: " & $(mstats.nfree) & "\n" &
  247. "[GC] heap objects: " & $(mstats.heap_objects) & "\n" &
  248. "[GC] numgc: " & $(mstats.numgc) & "\n" &
  249. "[GC] enablegc: " & $(mstats.enablegc) & "\n" &
  250. "[GC] debuggc: " & $(mstats.debuggc) & "\n" &
  251. "[GC] total pause time [ms]: " & $(mstats.pause_total_ns div 1000_000)
  252. proc getOccupiedMem(): int =
  253. var mstats: goMStats
  254. goRuntime_ReadMemStats(addr mstats)
  255. result = int(mstats.alloc)
  256. proc getFreeMem(): int =
  257. var mstats: goMStats
  258. goRuntime_ReadMemStats(addr mstats)
  259. result = int(mstats.sys - mstats.alloc)
  260. proc getTotalMem(): int =
  261. var mstats: goMStats
  262. goRuntime_ReadMemStats(addr mstats)
  263. result = int(mstats.sys)
  264. proc setStackBottom(theStackBottom: pointer) = discard
  265. proc alloc(size: Natural): pointer =
  266. result = c_malloc(size)
  267. if result == nil: raiseOutOfMem()
  268. proc alloc0(size: Natural): pointer =
  269. result = alloc(size)
  270. zeroMem(result, size)
  271. proc realloc(p: pointer, newsize: Natural): pointer =
  272. result = c_realloc(p, newsize)
  273. if result == nil: raiseOutOfMem()
  274. proc dealloc(p: pointer) = c_free(p)
  275. proc allocShared(size: Natural): pointer =
  276. result = c_malloc(size)
  277. if result == nil: raiseOutOfMem()
  278. proc allocShared0(size: Natural): pointer =
  279. result = alloc(size)
  280. zeroMem(result, size)
  281. proc reallocShared(p: pointer, newsize: Natural): pointer =
  282. result = c_realloc(p, newsize)
  283. if result == nil: raiseOutOfMem()
  284. proc deallocShared(p: pointer) = c_free(p)
  285. when hasThreadSupport:
  286. proc getFreeSharedMem(): int = discard
  287. proc getTotalSharedMem(): int = discard
  288. proc getOccupiedSharedMem(): int = discard
  289. const goFlagNoZero: uint32 = 1 shl 3
  290. proc goRuntimeMallocGC(size: uint, typ: uint, flag: uint32): pointer {.importc: "runtime_mallocgc", dynlib: goLib.}
  291. proc goFree(v: pointer) {.importc: "__go_free", dynlib: goLib.}
  292. proc goSetFinalizer(obj: pointer, f: pointer) {.importc: "set_finalizer", codegenDecl:"$1 $2$3 __asm__ (\"main.Set_finalizer\");\n$1 $2$3", dynlib: goLib.}
  293. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  294. result = goRuntimeMallocGC(roundup(size, sizeof(pointer)).uint, 0.uint, 0.uint32)
  295. if typ.finalizer != nil:
  296. goSetFinalizer(result, typ.finalizer)
  297. proc newObjNoInit(typ: PNimType, size: int): pointer =
  298. result = goRuntimeMallocGC(roundup(size, sizeof(pointer)).uint, 0.uint, goFlagNoZero)
  299. if typ.finalizer != nil:
  300. goSetFinalizer(result, typ.finalizer)
  301. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  302. result = newObj(typ, len * typ.base.size + GenericSeqSize)
  303. cast[PGenericSeq](result).len = len
  304. cast[PGenericSeq](result).reserved = len
  305. cast[PGenericSeq](result).elemSize = typ.base.size
  306. proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
  307. result = newObj(typ, cap * typ.base.size + GenericSeqSize)
  308. cast[PGenericSeq](result).len = 0
  309. cast[PGenericSeq](result).reserved = cap
  310. cast[PGenericSeq](result).elemSize = typ.base.size
  311. proc growObj(old: pointer, newsize: int): pointer =
  312. # the Go GC doesn't have a realloc
  313. var
  314. oldsize = cast[PGenericSeq](old).len * cast[PGenericSeq](old).elemSize + GenericSeqSize
  315. result = goRuntimeMallocGC(roundup(newsize, sizeof(pointer)).uint, 0.uint, goFlagNoZero)
  316. copyMem(result, old, oldsize)
  317. zeroMem(cast[pointer](cast[ByteAddress](result) +% oldsize), newsize - oldsize)
  318. goFree(old)
  319. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  320. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  321. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  322. dest[] = src
  323. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  324. dest[] = src
  325. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  326. dest[] = src
  327. type
  328. MemRegion = object {.final, pure.}
  329. {.deprecated: [TMemRegion: MemRegion].}
  330. proc alloc(r: var MemRegion, size: int): pointer =
  331. result = alloc(size)
  332. proc alloc0(r: var MemRegion, size: int): pointer =
  333. result = alloc0(size)
  334. proc dealloc(r: var MemRegion, p: pointer) = dealloc(p)
  335. proc deallocOsPages(r: var MemRegion) {.inline.} = discard
  336. proc deallocOsPages() {.inline.} = discard
  337. elif defined(nogc) and defined(useMalloc):
  338. when not defined(useNimRtl):
  339. proc alloc(size: Natural): pointer =
  340. var x = c_malloc(size + sizeof(size))
  341. if x == nil: raiseOutOfMem()
  342. cast[ptr int](x)[] = size
  343. result = cast[pointer](cast[int](x) + sizeof(size))
  344. proc alloc0(size: Natural): pointer =
  345. result = alloc(size)
  346. zeroMem(result, size)
  347. proc realloc(p: pointer, newsize: Natural): pointer =
  348. var x = cast[pointer](cast[int](p) - sizeof(newsize))
  349. let oldsize = cast[ptr int](x)[]
  350. x = c_realloc(x, newsize + sizeof(newsize))
  351. if x == nil: raiseOutOfMem()
  352. cast[ptr int](x)[] = newsize
  353. result = cast[pointer](cast[int](x) + sizeof(newsize))
  354. if newsize > oldsize:
  355. zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
  356. proc dealloc(p: pointer) = c_free(cast[pointer](cast[int](p) - sizeof(int)))
  357. proc allocShared(size: Natural): pointer =
  358. result = c_malloc(size)
  359. if result == nil: raiseOutOfMem()
  360. proc allocShared0(size: Natural): pointer =
  361. result = alloc(size)
  362. zeroMem(result, size)
  363. proc reallocShared(p: pointer, newsize: Natural): pointer =
  364. result = c_realloc(p, newsize)
  365. if result == nil: raiseOutOfMem()
  366. proc deallocShared(p: pointer) = c_free(p)
  367. proc GC_disable() = discard
  368. proc GC_enable() = discard
  369. proc GC_fullCollect() = discard
  370. proc GC_setStrategy(strategy: GC_Strategy) = discard
  371. proc GC_enableMarkAndSweep() = discard
  372. proc GC_disableMarkAndSweep() = discard
  373. proc GC_getStatistics(): string = return ""
  374. proc getOccupiedMem(): int = discard
  375. proc getFreeMem(): int = discard
  376. proc getTotalMem(): int = discard
  377. proc setStackBottom(theStackBottom: pointer) = discard
  378. proc initGC() = discard
  379. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  380. result = alloc0(size)
  381. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  382. result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
  383. cast[PGenericSeq](result).len = len
  384. cast[PGenericSeq](result).reserved = len
  385. proc newObjNoInit(typ: PNimType, size: int): pointer =
  386. result = alloc(size)
  387. proc growObj(old: pointer, newsize: int): pointer =
  388. result = realloc(old, newsize)
  389. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  390. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  391. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  392. dest[] = src
  393. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  394. dest[] = src
  395. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  396. dest[] = src
  397. type
  398. MemRegion = object {.final, pure.}
  399. {.deprecated: [TMemRegion: MemRegion].}
  400. proc alloc(r: var MemRegion, size: int): pointer =
  401. result = alloc(size)
  402. proc alloc0(r: var MemRegion, size: int): pointer =
  403. result = alloc0(size)
  404. proc dealloc(r: var MemRegion, p: pointer) = dealloc(p)
  405. proc deallocOsPages(r: var MemRegion) {.inline.} = discard
  406. proc deallocOsPages() {.inline.} = discard
  407. elif defined(nogc):
  408. # Even though we don't want the GC, we cannot simply use C's memory manager
  409. # because Nim's runtime wants ``realloc`` to zero out the additional
  410. # space which C's ``realloc`` does not. And we cannot get the old size of an
  411. # object, because C does not support this operation... Even though every
  412. # possible implementation has to have a way to determine the object's size.
  413. # C just sucks.
  414. when appType == "lib":
  415. {.warning: "nogc in a library context may not work".}
  416. include "system/alloc"
  417. proc initGC() = discard
  418. proc GC_disable() = discard
  419. proc GC_enable() = discard
  420. proc GC_fullCollect() = discard
  421. proc GC_setStrategy(strategy: GC_Strategy) = discard
  422. proc GC_enableMarkAndSweep() = discard
  423. proc GC_disableMarkAndSweep() = discard
  424. proc GC_getStatistics(): string = return ""
  425. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  426. result = alloc0(size)
  427. proc newObjNoInit(typ: PNimType, size: int): pointer =
  428. result = alloc(size)
  429. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  430. result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
  431. cast[PGenericSeq](result).len = len
  432. cast[PGenericSeq](result).reserved = len
  433. proc growObj(old: pointer, newsize: int): pointer =
  434. result = realloc(old, newsize)
  435. proc setStackBottom(theStackBottom: pointer) = discard
  436. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  437. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  438. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  439. dest[] = src
  440. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  441. dest[] = src
  442. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  443. dest[] = src
  444. var allocator {.rtlThreadVar.}: MemRegion
  445. instantiateForRegion(allocator)
  446. include "system/cellsets"
  447. else:
  448. when not defined(gcRegions):
  449. include "system/alloc"
  450. include "system/cellsets"
  451. when not leakDetector and not useCellIds:
  452. sysAssert(sizeof(Cell) == sizeof(FreeCell), "sizeof FreeCell")
  453. when compileOption("gc", "v2"):
  454. include "system/gc2"
  455. elif defined(gcRegions):
  456. # XXX due to bootstrapping reasons, we cannot use compileOption("gc", "stack") here
  457. include "system/gc_regions"
  458. elif defined(gcMarkAndSweep):
  459. # XXX use 'compileOption' here
  460. include "system/gc_ms"
  461. elif defined(gcGenerational):
  462. include "system/gc"
  463. else:
  464. include "system/gc"
  465. when not declared(nimNewSeqOfCap):
  466. proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
  467. let s = addInt(mulInt(cap, typ.base.size), GenericSeqSize)
  468. when declared(newObjNoInit):
  469. result = if ntfNoRefs in typ.base.flags: newObjNoInit(typ, s) else: newObj(typ, s)
  470. else:
  471. result = newObj(typ, s)
  472. cast[PGenericSeq](result).len = 0
  473. cast[PGenericSeq](result).reserved = cap
  474. {.pop.}