gc_regions.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #
  2. # Nim's Runtime Library
  3. # (c) Copyright 2016 Andreas Rumpf
  4. #
  5. # See the file "copying.txt", included in this
  6. # distribution, for details about the copyright.
  7. #
  8. # "Stack GC" for embedded devices or ultra performance requirements.
  9. when defined(nimphpext):
  10. proc roundup(x, v: int): int {.inline.} =
  11. result = (x + (v-1)) and not (v-1)
  12. proc emalloc(size: int): pointer {.importc: "_emalloc".}
  13. proc efree(mem: pointer) {.importc: "_efree".}
  14. proc osAllocPages(size: int): pointer {.inline.} =
  15. emalloc(size)
  16. proc osTryAllocPages(size: int): pointer {.inline.} =
  17. emalloc(size)
  18. proc osDeallocPages(p: pointer, size: int) {.inline.} =
  19. efree(p)
  20. else:
  21. include osalloc
  22. # We manage memory as a thread local stack. Since the allocation pointer
  23. # is detached from the control flow pointer, this model is vastly more
  24. # useful than the traditional programming model while almost as safe.
  25. # Individual objects can also be deleted but no coalescing is performed.
  26. # Stacks can also be moved from one thread to another.
  27. # We also support 'finalizers'.
  28. type
  29. Finalizer {.compilerproc.} = proc (self: pointer) {.nimcall, benign.}
  30. # A ref type can have a finalizer that is called before the object's
  31. # storage is freed.
  32. AlignType = BiggestFloat
  33. ObjHeader = object
  34. typ: PNimType
  35. nextFinal: ptr ObjHeader # next object with finalizer
  36. Chunk = ptr BaseChunk
  37. BaseChunk = object
  38. next: Chunk
  39. size: int
  40. head, tail: ptr ObjHeader # first and last object in chunk that
  41. # has a finalizer attached to it
  42. const
  43. MaxSmallObject = 128
  44. type
  45. FreeEntry = ptr object
  46. next: FreeEntry
  47. SizedFreeEntry = ptr object
  48. next: SizedFreeEntry
  49. size: int
  50. StackPtr = object
  51. bump: pointer
  52. remaining: int
  53. current: Chunk
  54. MemRegion* = object
  55. remaining: int
  56. bump: pointer
  57. head, tail: Chunk
  58. nextChunkSize, totalSize: int
  59. when false:
  60. freeLists: array[MaxSmallObject div MemAlign, FreeEntry]
  61. holes: SizedFreeEntry
  62. when hasThreadSupport:
  63. lock: SysLock
  64. SeqHeader = object # minor hack ahead: Since we know that seqs
  65. # and strings cannot have finalizers, we use the field
  66. # instead for a 'region' field so that they can grow
  67. # and shrink safely.
  68. typ: PNimType
  69. region: ptr MemRegion
  70. var
  71. tlRegion {.threadVar.}: MemRegion
  72. # tempStrRegion {.threadVar.}: MemRegion # not yet used
  73. template withRegion*(r: MemRegion; body: untyped) =
  74. let oldRegion = tlRegion
  75. tlRegion = r
  76. try:
  77. body
  78. finally:
  79. #r = tlRegion
  80. tlRegion = oldRegion
  81. template inc(p: pointer, s: int) =
  82. p = cast[pointer](cast[int](p) +% s)
  83. template dec(p: pointer, s: int) =
  84. p = cast[pointer](cast[int](p) -% s)
  85. template `+!`(p: pointer, s: int): pointer =
  86. cast[pointer](cast[int](p) +% s)
  87. template `-!`(p: pointer, s: int): pointer =
  88. cast[pointer](cast[int](p) -% s)
  89. proc allocSlowPath(r: var MemRegion; size: int) =
  90. # we need to ensure that the underlying linked list
  91. # stays small. Say we want to grab 16GB of RAM with some
  92. # exponential growth function. So we allocate 16KB, then
  93. # 32 KB, 64 KB, 128KB, 256KB, 512KB, 1MB, 2MB, 4MB,
  94. # 8MB, 16MB, 32MB, 64MB, 128MB, 512MB, 1GB, 2GB, 4GB, 8GB,
  95. # 16GB --> list contains only 20 elements! That's reasonable.
  96. if (r.totalSize and 1) == 0:
  97. r.nextChunkSize =
  98. if r.totalSize < 64 * 1024: PageSize*4
  99. else: r.nextChunkSize*2
  100. var s = roundup(size+sizeof(BaseChunk), PageSize)
  101. var fresh: Chunk
  102. if s > r.nextChunkSize:
  103. fresh = cast[Chunk](osAllocPages(s))
  104. else:
  105. fresh = cast[Chunk](osTryAllocPages(r.nextChunkSize))
  106. if fresh == nil:
  107. fresh = cast[Chunk](osAllocPages(s))
  108. # lowest bit in totalSize is the "don't increase nextChunkSize"
  109. inc r.totalSize
  110. else:
  111. s = r.nextChunkSize
  112. fresh.size = s
  113. fresh.head = nil
  114. fresh.tail = nil
  115. fresh.next = nil
  116. inc r.totalSize, s
  117. let old = r.tail
  118. if old == nil:
  119. r.head = fresh
  120. else:
  121. r.tail.next = fresh
  122. r.bump = fresh +! sizeof(BaseChunk)
  123. r.tail = fresh
  124. r.remaining = s - sizeof(BaseChunk)
  125. proc allocFast(r: var MemRegion; size: int): pointer =
  126. when false:
  127. if size <= MaxSmallObject:
  128. var it = r.freeLists[size div MemAlign]
  129. if it != nil:
  130. r.freeLists[size div MemAlign] = it.next
  131. return pointer(it)
  132. else:
  133. var it = r.holes
  134. var prev: SizedFreeEntry = nil
  135. while it != nil:
  136. if it.size >= size:
  137. if prev != nil: prev.next = it.next
  138. else: r.holes = it.next
  139. return pointer(it)
  140. prev = it
  141. it = it.next
  142. let size = roundup(size, MemAlign)
  143. if size > r.remaining:
  144. allocSlowPath(r, size)
  145. sysAssert(size <= r.remaining, "size <= r.remaining")
  146. dec(r.remaining, size)
  147. result = r.bump
  148. inc r.bump, size
  149. proc runFinalizers(c: Chunk) =
  150. var it = c.head
  151. while it != nil:
  152. # indivually freed objects with finalizer stay in the list, but
  153. # their typ is nil then:
  154. if it.typ != nil and it.typ.finalizer != nil:
  155. (cast[Finalizer](it.typ.finalizer))(it+!sizeof(ObjHeader))
  156. it = it.nextFinal
  157. proc dealloc(r: var MemRegion; p: pointer; size: int) =
  158. let it = cast[ptr ObjHeader](p-!sizeof(ObjHeader))
  159. if it.typ != nil and it.typ.finalizer != nil:
  160. (cast[Finalizer](it.typ.finalizer))(p)
  161. it.typ = nil
  162. # it is benefitial to not use the free lists here:
  163. if r.bump -! size == p:
  164. dec r.bump, size
  165. when false:
  166. if size <= MaxSmallObject:
  167. let it = cast[FreeEntry](p)
  168. it.next = r.freeLists[size div MemAlign]
  169. r.freeLists[size div MemAlign] = it
  170. else:
  171. let it = cast[SizedFreeEntry](p)
  172. it.size = size
  173. it.next = r.holes
  174. r.holes = it
  175. proc deallocAll(r: var MemRegion; head: Chunk) =
  176. var it = head
  177. while it != nil:
  178. let nxt = it.next
  179. runFinalizers(it)
  180. dec r.totalSize, it.size
  181. osDeallocPages(it, it.size)
  182. it = nxt
  183. proc deallocAll*(r: var MemRegion) =
  184. deallocAll(r, r.head)
  185. zeroMem(addr r, sizeof r)
  186. proc obstackPtr*(r: MemRegion): StackPtr =
  187. result.bump = r.bump
  188. result.remaining = r.remaining
  189. result.current = r.tail
  190. template computeRemaining(r): untyped =
  191. r.tail.size -% (cast[int](r.bump) -% cast[int](r.tail))
  192. proc setObstackPtr*(r: var MemRegion; sp: StackPtr) =
  193. # free everything after 'sp':
  194. if sp.current.next != nil:
  195. deallocAll(r, sp.current.next)
  196. sp.current.next = nil
  197. when false:
  198. # better leak this memory than be sorry:
  199. for i in 0..high(r.freeLists): r.freeLists[i] = nil
  200. r.holes = nil
  201. #else:
  202. # deallocAll(r, r.head)
  203. # r.head = nil
  204. r.bump = sp.bump
  205. r.tail = sp.current
  206. r.remaining = sp.remaining
  207. proc obstackPtr*(): StackPtr = tlRegion.obstackPtr()
  208. proc setObstackPtr*(sp: StackPtr) = tlRegion.setObstackPtr(sp)
  209. proc deallocAll*() = tlRegion.deallocAll()
  210. proc deallocOsPages(r: var MemRegion) = r.deallocAll()
  211. template withScratchRegion*(body: untyped) =
  212. var scratch: MemRegion
  213. let oldRegion = tlRegion
  214. tlRegion = scratch
  215. try:
  216. body
  217. finally:
  218. tlRegion = oldRegion
  219. deallocAll(scratch)
  220. when false:
  221. proc joinRegion*(dest: var MemRegion; src: MemRegion) =
  222. # merging is not hard.
  223. if dest.head.isNil:
  224. dest.head = src.head
  225. else:
  226. dest.tail.next = src.head
  227. dest.tail = src.tail
  228. dest.bump = src.bump
  229. dest.remaining = src.remaining
  230. dest.nextChunkSize = max(dest.nextChunkSize, src.nextChunkSize)
  231. inc dest.totalSize, src.totalSize
  232. proc isOnHeap*(r: MemRegion; p: pointer): bool =
  233. # the tail chunk is the largest, so check it first. It's also special
  234. # in that contains the current bump pointer:
  235. if r.tail >= p and p < r.bump:
  236. return true
  237. var it = r.head
  238. while it != r.tail:
  239. if it >= p and p <= it+!it.size: return true
  240. it = it.next
  241. proc rawNewObj(r: var MemRegion, typ: PNimType, size: int): pointer =
  242. var res = cast[ptr ObjHeader](allocFast(r, size + sizeof(ObjHeader)))
  243. res.typ = typ
  244. if typ.finalizer != nil:
  245. res.nextFinal = r.head.head
  246. r.head.head = res
  247. result = res +! sizeof(ObjHeader)
  248. proc rawNewSeq(r: var MemRegion, typ: PNimType, size: int): pointer =
  249. var res = cast[ptr SeqHeader](allocFast(r, size + sizeof(SeqHeader)))
  250. res.typ = typ
  251. res.region = addr(r)
  252. result = res +! sizeof(SeqHeader)
  253. proc newObj(typ: PNimType, size: int): pointer {.compilerRtl.} =
  254. sysAssert typ.kind notin {tySequence, tyString}, "newObj cannot be used to construct seqs"
  255. result = rawNewObj(tlRegion, typ, size)
  256. zeroMem(result, size)
  257. when defined(memProfiler): nimProfile(size)
  258. proc newObjNoInit(typ: PNimType, size: int): pointer {.compilerRtl.} =
  259. sysAssert typ.kind notin {tySequence, tyString}, "newObj cannot be used to construct seqs"
  260. result = rawNewObj(tlRegion, typ, size)
  261. when defined(memProfiler): nimProfile(size)
  262. proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} =
  263. let size = roundup(addInt(mulInt(len, typ.base.size), GenericSeqSize),
  264. MemAlign)
  265. result = rawNewSeq(tlRegion, typ, size)
  266. zeroMem(result, size)
  267. cast[PGenericSeq](result).len = len
  268. cast[PGenericSeq](result).reserved = len
  269. proc newStr(typ: PNimType, len: int; init: bool): pointer {.compilerRtl.} =
  270. let size = roundup(addInt(len, GenericSeqSize), MemAlign)
  271. result = rawNewSeq(tlRegion, typ, size)
  272. if init: zeroMem(result, size)
  273. cast[PGenericSeq](result).len = 0
  274. cast[PGenericSeq](result).reserved = len
  275. proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
  276. result = rawNewObj(tlRegion, typ, size)
  277. zeroMem(result, size)
  278. proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
  279. result = newSeq(typ, len)
  280. proc growObj(regionUnused: var MemRegion; old: pointer, newsize: int): pointer =
  281. let sh = cast[ptr SeqHeader](old -! sizeof(SeqHeader))
  282. let typ = sh.typ
  283. result = rawNewSeq(sh.region[], typ,
  284. roundup(newsize, MemAlign))
  285. let elemSize = if typ.kind == tyString: 1 else: typ.base.size
  286. let oldsize = cast[PGenericSeq](old).len*elemSize + GenericSeqSize
  287. zeroMem(result +! oldsize, newsize-oldsize)
  288. copyMem(result, old, oldsize)
  289. dealloc(sh.region[], old, roundup(oldsize, MemAlign))
  290. proc growObj(old: pointer, newsize: int): pointer {.rtl.} =
  291. result = growObj(tlRegion, old, newsize)
  292. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  293. dest[] = src
  294. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  295. dest[] = src
  296. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  297. deprecated: "old compiler compat".} = asgnRef(dest, src)
  298. proc alloc(size: Natural): pointer =
  299. result = c_malloc(size)
  300. if result == nil: raiseOutOfMem()
  301. proc alloc0(size: Natural): pointer =
  302. result = alloc(size)
  303. zeroMem(result, size)
  304. proc realloc(p: pointer, newsize: Natural): pointer =
  305. result = c_realloc(p, newsize)
  306. if result == nil: raiseOutOfMem()
  307. proc dealloc(p: pointer) = c_free(p)
  308. proc alloc0(r: var MemRegion; size: Natural): pointer =
  309. # ignore the region. That is correct for the channels module
  310. # but incorrect in general. XXX
  311. result = alloc0(size)
  312. proc alloc(r: var MemRegion; size: Natural): pointer =
  313. # ignore the region. That is correct for the channels module
  314. # but incorrect in general. XXX
  315. result = alloc(size)
  316. proc dealloc(r: var MemRegion; p: pointer) = dealloc(p)
  317. proc allocShared(size: Natural): pointer =
  318. result = c_malloc(size)
  319. if result == nil: raiseOutOfMem()
  320. proc allocShared0(size: Natural): pointer =
  321. result = alloc(size)
  322. zeroMem(result, size)
  323. proc reallocShared(p: pointer, newsize: Natural): pointer =
  324. result = c_realloc(p, newsize)
  325. if result == nil: raiseOutOfMem()
  326. proc deallocShared(p: pointer) = c_free(p)
  327. when hasThreadSupport:
  328. proc getFreeSharedMem(): int = 0
  329. proc getTotalSharedMem(): int = 0
  330. proc getOccupiedSharedMem(): int = 0
  331. proc GC_disable() = discard
  332. proc GC_enable() = discard
  333. proc GC_fullCollect() = discard
  334. proc GC_setStrategy(strategy: GC_Strategy) = discard
  335. proc GC_enableMarkAndSweep() = discard
  336. proc GC_disableMarkAndSweep() = discard
  337. proc GC_getStatistics(): string = return ""
  338. proc getOccupiedMem(): int =
  339. result = tlRegion.totalSize - tlRegion.remaining
  340. proc getFreeMem(): int = tlRegion.remaining
  341. proc getTotalMem(): int =
  342. result = tlRegion.totalSize
  343. proc getOccupiedMem*(r: MemRegion): int =
  344. result = r.totalSize - r.remaining
  345. proc getFreeMem*(r: MemRegion): int = r.remaining
  346. proc getTotalMem*(r: MemRegion): int =
  347. result = r.totalSize
  348. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  349. proc nimGCref(x: pointer) {.compilerProc.} = discard
  350. proc nimGCunref(x: pointer) {.compilerProc.} = discard