mmdisp.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. # 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 improvments 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")
  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 boehmGC_set_all_interior_pointers(flag: cint) {.
  65. importc: "GC_set_all_interior_pointers", boehmGC.}
  66. proc boehmAlloc(size: int): pointer {.importc: "GC_malloc", boehmGC.}
  67. proc boehmAllocAtomic(size: int): pointer {.
  68. importc: "GC_malloc_atomic", boehmGC.}
  69. proc boehmRealloc(p: pointer, size: int): pointer {.
  70. importc: "GC_realloc", boehmGC.}
  71. proc boehmDealloc(p: pointer) {.importc: "GC_free", boehmGC.}
  72. when hasThreadSupport:
  73. proc boehmGC_allow_register_threads {.
  74. importc: "GC_allow_register_threads", boehmGC.}
  75. proc boehmGetHeapSize: int {.importc: "GC_get_heap_size", boehmGC.}
  76. ## Return the number of bytes in the heap. Excludes collector private
  77. ## data structures. Includes empty blocks and fragmentation loss.
  78. ## Includes some pages that were allocated but never written.
  79. proc boehmGetFreeBytes: int {.importc: "GC_get_free_bytes", boehmGC.}
  80. ## Return a lower bound on the number of free bytes in the heap.
  81. proc boehmGetBytesSinceGC: int {.importc: "GC_get_bytes_since_gc", boehmGC.}
  82. ## Return the number of bytes allocated since the last collection.
  83. proc boehmGetTotalBytes: int {.importc: "GC_get_total_bytes", boehmGC.}
  84. ## Return the total number of bytes allocated in this process.
  85. ## Never decreases.
  86. proc allocAtomic(size: int): pointer =
  87. result = boehmAllocAtomic(size)
  88. zeroMem(result, size)
  89. when not defined(useNimRtl):
  90. proc alloc(size: Natural): pointer =
  91. result = boehmAlloc(size)
  92. if result == nil: raiseOutOfMem()
  93. proc alloc0(size: Natural): pointer =
  94. result = alloc(size)
  95. proc realloc(p: pointer, newsize: Natural): pointer =
  96. result = boehmRealloc(p, newsize)
  97. if result == nil: raiseOutOfMem()
  98. proc dealloc(p: pointer) = boehmDealloc(p)
  99. proc allocShared(size: Natural): pointer =
  100. result = boehmAlloc(size)
  101. if result == nil: raiseOutOfMem()
  102. proc allocShared0(size: Natural): pointer =
  103. result = allocShared(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 nimGC_setStackBottom(theStackBottom: pointer) = discard
  127. proc initGC() =
  128. boehmGC_set_all_interior_pointers(0)
  129. boehmGCinit()
  130. when hasThreadSupport:
  131. boehmGC_allow_register_threads()
  132. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  133. if ntfNoRefs in typ.flags: result = allocAtomic(size)
  134. else: result = alloc(size)
  135. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  136. result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
  137. cast[PGenericSeq](result).len = len
  138. cast[PGenericSeq](result).reserved = len
  139. proc growObj(old: pointer, newsize: int): pointer =
  140. result = realloc(old, newsize)
  141. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  142. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  143. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  144. dest[] = src
  145. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  146. dest[] = src
  147. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  148. deprecated: "old compiler compat".} = asgnRef(dest, src)
  149. type
  150. MemRegion = object
  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 initGC() = discard
  169. proc GC_disable() = discard
  170. proc GC_enable() = discard
  171. proc go_gc() {.importc: "go_gc", dynlib: goLib.}
  172. proc GC_fullCollect() = go_gc()
  173. proc GC_setStrategy(strategy: GC_Strategy) = discard
  174. proc GC_enableMarkAndSweep() = discard
  175. proc GC_disableMarkAndSweep() = discard
  176. const
  177. goNumSizeClasses = 67
  178. type
  179. goMStats = object
  180. alloc: uint64 # bytes allocated and still in use
  181. total_alloc: uint64 # bytes allocated (even if freed)
  182. sys: uint64 # bytes obtained from system
  183. nlookup: uint64 # number of pointer lookups
  184. nmalloc: uint64 # number of mallocs
  185. nfree: uint64 # number of frees
  186. heap_objects: uint64 # total number of allocated objects
  187. pause_total_ns: uint64 # cumulative nanoseconds in GC stop-the-world pauses since the program started
  188. numgc: uint32 # number of completed GC cycles
  189. proc goMemStats(): goMStats {.importc: "go_mem_stats", dynlib: goLib.}
  190. proc goMalloc(size: uint): pointer {.importc: "go_malloc", dynlib: goLib.}
  191. proc goSetFinalizer(obj: pointer, f: pointer) {.importc: "set_finalizer", codegenDecl:"$1 $2$3 __asm__ (\"main.Set_finalizer\");\n$1 $2$3", dynlib: goLib.}
  192. proc writebarrierptr(dest: PPointer, src: pointer) {.importc: "writebarrierptr", codegenDecl:"$1 $2$3 __asm__ (\"main.Atomic_store_pointer\");\n$1 $2$3", dynlib: goLib.}
  193. proc `$`*(x: uint64): string {.noSideEffect, raises: [].}
  194. proc GC_getStatistics(): string =
  195. var mstats = goMemStats()
  196. result = "[GC] total allocated memory: " & $(mstats.total_alloc) & "\n" &
  197. "[GC] total memory obtained from system: " & $(mstats.sys) & "\n" &
  198. "[GC] occupied memory: " & $(mstats.alloc) & "\n" &
  199. "[GC] number of pointer lookups: " & $(mstats.nlookup) & "\n" &
  200. "[GC] number of mallocs: " & $(mstats.nmalloc) & "\n" &
  201. "[GC] number of frees: " & $(mstats.nfree) & "\n" &
  202. "[GC] heap objects: " & $(mstats.heap_objects) & "\n" &
  203. "[GC] number of completed GC cycles: " & $(mstats.numgc) & "\n" &
  204. "[GC] total GC pause time [ms]: " & $(mstats.pause_total_ns div 1000_000)
  205. proc getOccupiedMem(): int =
  206. var mstats = goMemStats()
  207. result = int(mstats.alloc)
  208. proc getFreeMem(): int =
  209. var mstats = goMemStats()
  210. result = int(mstats.sys - mstats.alloc)
  211. proc getTotalMem(): int =
  212. var mstats = goMemStats()
  213. result = int(mstats.sys)
  214. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  215. proc alloc(size: Natural): pointer =
  216. result = goMalloc(size.uint)
  217. proc alloc0(size: Natural): pointer =
  218. result = goMalloc(size.uint)
  219. proc realloc(p: pointer, newsize: Natural): pointer =
  220. doAssert false, "not implemented"
  221. proc dealloc(p: pointer) =
  222. discard
  223. proc allocShared(size: Natural): pointer =
  224. result = alloc(size)
  225. proc allocShared0(size: Natural): pointer =
  226. result = alloc0(size)
  227. proc reallocShared(p: pointer, newsize: Natural): pointer =
  228. result = realloc(p, newsize)
  229. proc deallocShared(p: pointer) = dealloc(p)
  230. when hasThreadSupport:
  231. proc getFreeSharedMem(): int = discard
  232. proc getTotalSharedMem(): int = discard
  233. proc getOccupiedSharedMem(): int = discard
  234. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  235. writebarrierptr(addr(result), goMalloc(size.uint))
  236. if typ.finalizer != nil:
  237. goSetFinalizer(result, typ.finalizer)
  238. proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
  239. writebarrierptr(addr(result), newObj(typ, size))
  240. proc newObjNoInit(typ: PNimType, size: int): pointer =
  241. writebarrierptr(addr(result), newObj(typ, size))
  242. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  243. writebarrierptr(addr(result), newObj(typ, len * typ.base.size + GenericSeqSize))
  244. cast[PGenericSeq](result).len = len
  245. cast[PGenericSeq](result).reserved = len
  246. cast[PGenericSeq](result).elemSize = typ.base.size
  247. proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
  248. writebarrierptr(addr(result), newSeq(typ, len))
  249. proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
  250. result = newObj(typ, cap * typ.base.size + GenericSeqSize)
  251. cast[PGenericSeq](result).len = 0
  252. cast[PGenericSeq](result).reserved = cap
  253. cast[PGenericSeq](result).elemSize = typ.base.size
  254. proc typedMemMove(dest: pointer, src: pointer, size: uint) {.importc: "typedmemmove", dynlib: goLib.}
  255. proc growObj(old: pointer, newsize: int): pointer =
  256. # the Go GC doesn't have a realloc
  257. var metadataOld = cast[PGenericSeq](old)
  258. if metadataOld.elemSize == 0:
  259. metadataOld.elemSize = 1
  260. let oldsize = cast[PGenericSeq](old).len * cast[PGenericSeq](old).elemSize + GenericSeqSize
  261. writebarrierptr(addr(result), goMalloc(newsize.uint))
  262. typedMemMove(result, old, oldsize.uint)
  263. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  264. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  265. proc nimGCunrefNoCycle(p: pointer) {.compilerProc, inline.} = discard
  266. proc nimGCunrefRC1(p: pointer) {.compilerProc, inline.} = discard
  267. proc nimGCvisit(d: pointer, op: int) {.compilerRtl.} = discard
  268. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  269. writebarrierptr(dest, src)
  270. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  271. writebarrierptr(dest, src)
  272. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  273. deprecated: "old compiler compat".} = asgnRef(dest, src)
  274. type
  275. MemRegion = object
  276. proc alloc(r: var MemRegion, size: int): pointer =
  277. result = alloc(size)
  278. proc alloc0(r: var MemRegion, size: int): pointer =
  279. result = alloc0(size)
  280. proc dealloc(r: var MemRegion, p: pointer) = dealloc(p)
  281. proc deallocOsPages(r: var MemRegion) {.inline.} = discard
  282. proc deallocOsPages() {.inline.} = discard
  283. elif defined(nogc) and defined(useMalloc):
  284. when not defined(useNimRtl):
  285. proc alloc(size: Natural): pointer =
  286. var x = c_malloc(size + sizeof(size))
  287. if x == nil: raiseOutOfMem()
  288. cast[ptr int](x)[] = size
  289. result = cast[pointer](cast[int](x) + sizeof(size))
  290. proc alloc0(size: Natural): pointer =
  291. result = alloc(size)
  292. zeroMem(result, size)
  293. proc realloc(p: pointer, newsize: Natural): pointer =
  294. var x = cast[pointer](cast[int](p) - sizeof(newsize))
  295. let oldsize = cast[ptr int](x)[]
  296. x = c_realloc(x, newsize + sizeof(newsize))
  297. if x == nil: raiseOutOfMem()
  298. cast[ptr int](x)[] = newsize
  299. result = cast[pointer](cast[int](x) + sizeof(newsize))
  300. if newsize > oldsize:
  301. zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
  302. proc dealloc(p: pointer) = c_free(cast[pointer](cast[int](p) - sizeof(int)))
  303. proc allocShared(size: Natural): pointer =
  304. result = c_malloc(size)
  305. if result == nil: raiseOutOfMem()
  306. proc allocShared0(size: Natural): pointer =
  307. result = alloc(size)
  308. zeroMem(result, size)
  309. proc reallocShared(p: pointer, newsize: Natural): pointer =
  310. result = c_realloc(p, newsize)
  311. if result == nil: raiseOutOfMem()
  312. proc deallocShared(p: pointer) = c_free(p)
  313. proc GC_disable() = discard
  314. proc GC_enable() = discard
  315. proc GC_fullCollect() = discard
  316. proc GC_setStrategy(strategy: GC_Strategy) = discard
  317. proc GC_enableMarkAndSweep() = discard
  318. proc GC_disableMarkAndSweep() = discard
  319. proc GC_getStatistics(): string = return ""
  320. proc getOccupiedMem(): int = discard
  321. proc getFreeMem(): int = discard
  322. proc getTotalMem(): int = discard
  323. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  324. proc initGC() = discard
  325. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  326. result = alloc0(size)
  327. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  328. result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
  329. cast[PGenericSeq](result).len = len
  330. cast[PGenericSeq](result).reserved = len
  331. proc newObjNoInit(typ: PNimType, size: int): pointer =
  332. result = alloc(size)
  333. proc growObj(old: pointer, newsize: int): pointer =
  334. result = realloc(old, newsize)
  335. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  336. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  337. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  338. dest[] = src
  339. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  340. dest[] = src
  341. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  342. deprecated: "old compiler compat".} = asgnRef(dest, src)
  343. type
  344. MemRegion = object
  345. proc alloc(r: var MemRegion, size: int): pointer =
  346. result = alloc(size)
  347. proc alloc0(r: var MemRegion, size: int): pointer =
  348. result = alloc0(size)
  349. proc dealloc(r: var MemRegion, p: pointer) = dealloc(p)
  350. proc deallocOsPages(r: var MemRegion) {.inline.} = discard
  351. proc deallocOsPages() {.inline.} = discard
  352. elif defined(nogc):
  353. # Even though we don't want the GC, we cannot simply use C's memory manager
  354. # because Nim's runtime wants ``realloc`` to zero out the additional
  355. # space which C's ``realloc`` does not. And we cannot get the old size of an
  356. # object, because C does not support this operation... Even though every
  357. # possible implementation has to have a way to determine the object's size.
  358. # C just sucks.
  359. when appType == "lib":
  360. {.warning: "nogc in a library context may not work".}
  361. include "system/alloc"
  362. proc initGC() = discard
  363. proc GC_disable() = discard
  364. proc GC_enable() = discard
  365. proc GC_fullCollect() = discard
  366. proc GC_setStrategy(strategy: GC_Strategy) = discard
  367. proc GC_enableMarkAndSweep() = discard
  368. proc GC_disableMarkAndSweep() = discard
  369. proc GC_getStatistics(): string = return ""
  370. proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} =
  371. result = alloc0(size)
  372. proc newObjNoInit(typ: PNimType, size: int): pointer =
  373. result = alloc(size)
  374. proc newSeq(typ: PNimType, len: int): pointer {.compilerproc.} =
  375. result = newObj(typ, addInt(mulInt(len, typ.base.size), GenericSeqSize))
  376. cast[PGenericSeq](result).len = len
  377. cast[PGenericSeq](result).reserved = len
  378. proc growObj(old: pointer, newsize: int): pointer =
  379. result = realloc(old, newsize)
  380. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  381. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  382. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  383. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  384. dest[] = src
  385. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  386. dest[] = src
  387. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  388. deprecated: "old compiler compat".} = asgnRef(dest, src)
  389. var allocator {.rtlThreadVar.}: MemRegion
  390. instantiateForRegion(allocator)
  391. include "system/cellsets"
  392. else:
  393. when not defined(gcRegions):
  394. include "system/alloc"
  395. include "system/cellsets"
  396. when not leakDetector and not useCellIds:
  397. sysAssert(sizeof(Cell) == sizeof(FreeCell), "sizeof FreeCell")
  398. when compileOption("gc", "v2"):
  399. include "system/gc2"
  400. elif defined(gcRegions):
  401. # XXX due to bootstrapping reasons, we cannot use compileOption("gc", "stack") here
  402. include "system/gc_regions"
  403. elif defined(nimV2):
  404. var allocator {.rtlThreadVar.}: MemRegion
  405. instantiateForRegion(allocator)
  406. elif defined(gcMarkAndSweep) or defined(gcDestructors):
  407. # XXX use 'compileOption' here
  408. include "system/gc_ms"
  409. else:
  410. include "system/gc"
  411. when not declared(nimNewSeqOfCap) and not defined(gcDestructors):
  412. proc nimNewSeqOfCap(typ: PNimType, cap: int): pointer {.compilerproc.} =
  413. when defined(gcRegions):
  414. let s = mulInt(cap, typ.base.size) # newStr already adds GenericSeqSize
  415. result = newStr(typ, s, ntfNoRefs notin typ.base.flags)
  416. else:
  417. let s = addInt(mulInt(cap, typ.base.size), GenericSeqSize)
  418. when declared(newObjNoInit):
  419. result = if ntfNoRefs in typ.base.flags: newObjNoInit(typ, s) else: newObj(typ, s)
  420. else:
  421. result = newObj(typ, s)
  422. cast[PGenericSeq](result).len = 0
  423. cast[PGenericSeq](result).reserved = cap
  424. {.pop.}
  425. when not declared(ForeignCell):
  426. type ForeignCell* = object
  427. data*: pointer
  428. proc protect*(x: pointer): ForeignCell = ForeignCell(data: x)
  429. proc dispose*(x: ForeignCell) = discard
  430. proc isNotForeign*(x: ForeignCell): bool = false