mmdisp.nim 19 KB

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