gc_regions.nim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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(useMalloc):
  10. proc roundup(x, v: int): int {.inline.} =
  11. result = (x + (v-1)) and not (v-1)
  12. proc emalloc(size: int): pointer {.importc: "malloc", header: "<stdlib.h>".}
  13. proc efree(mem: pointer) {.importc: "free", header: "<stdlib.h>".}
  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: var 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. const nimMinHeapPages {.intdefine.} = 4
  90. proc allocSlowPath(r: var MemRegion; size: int) =
  91. # we need to ensure that the underlying linked list
  92. # stays small. Say we want to grab 16GB of RAM with some
  93. # exponential growth function. So we allocate 16KB, then
  94. # 32 KB, 64 KB, 128KB, 256KB, 512KB, 1MB, 2MB, 4MB,
  95. # 8MB, 16MB, 32MB, 64MB, 128MB, 512MB, 1GB, 2GB, 4GB, 8GB,
  96. # 16GB --> list contains only 20 elements! That's reasonable.
  97. if (r.totalSize and 1) == 0:
  98. r.nextChunkSize = if r.totalSize < 64 * 1024: PageSize*nimMinHeapPages
  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 runFinalizers(c: Chunk; newbump: pointer) =
  158. var it = c.head
  159. var prev: ptr ObjHeader = nil
  160. while it != nil:
  161. let nxt = it.nextFinal
  162. if it >= newbump:
  163. if it.typ != nil and it.typ.finalizer != nil:
  164. (cast[Finalizer](it.typ.finalizer))(it+!sizeof(ObjHeader))
  165. elif prev != nil:
  166. prev.nextFinal = nil
  167. prev = it
  168. it = nxt
  169. proc dealloc(r: var MemRegion; p: pointer; size: int) =
  170. let it = cast[ptr ObjHeader](p-!sizeof(ObjHeader))
  171. if it.typ != nil and it.typ.finalizer != nil:
  172. (cast[Finalizer](it.typ.finalizer))(p)
  173. it.typ = nil
  174. # it is beneficial to not use the free lists here:
  175. if r.bump -! size == p:
  176. dec r.bump, size
  177. when false:
  178. if size <= MaxSmallObject:
  179. let it = cast[FreeEntry](p)
  180. it.next = r.freeLists[size div MemAlign]
  181. r.freeLists[size div MemAlign] = it
  182. else:
  183. let it = cast[SizedFreeEntry](p)
  184. it.size = size
  185. it.next = r.holes
  186. r.holes = it
  187. proc deallocAll(r: var MemRegion; head: Chunk) =
  188. var it = head
  189. while it != nil:
  190. let nxt = it.next
  191. runFinalizers(it)
  192. dec r.totalSize, it.size
  193. osDeallocPages(it, it.size)
  194. it = nxt
  195. proc deallocAll*(r: var MemRegion) =
  196. deallocAll(r, r.head)
  197. zeroMem(addr r, sizeof r)
  198. proc obstackPtr*(r: MemRegion): StackPtr =
  199. result.bump = r.bump
  200. result.remaining = r.remaining
  201. result.current = r.tail
  202. template computeRemaining(r): untyped =
  203. r.tail.size -% (cast[int](r.bump) -% cast[int](r.tail))
  204. proc setObstackPtr*(r: var MemRegion; sp: StackPtr) =
  205. # free everything after 'sp':
  206. if sp.current != nil and sp.current.next != nil:
  207. deallocAll(r, sp.current.next)
  208. sp.current.next = nil
  209. when false:
  210. # better leak this memory than be sorry:
  211. for i in 0..high(r.freeLists): r.freeLists[i] = nil
  212. r.holes = nil
  213. if r.tail != nil: runFinalizers(r.tail, sp.bump)
  214. r.bump = sp.bump
  215. r.tail = sp.current
  216. r.remaining = sp.remaining
  217. proc obstackPtr*(): StackPtr = tlRegion.obstackPtr()
  218. proc setObstackPtr*(sp: StackPtr) = tlRegion.setObstackPtr(sp)
  219. proc deallocAll*() = tlRegion.deallocAll()
  220. proc deallocOsPages(r: var MemRegion) = r.deallocAll()
  221. when false:
  222. let obs = obstackPtr()
  223. try:
  224. body
  225. finally:
  226. setObstackPtr(obs)
  227. template withScratchRegion*(body: untyped) =
  228. let oldRegion = tlRegion
  229. tlRegion = MemRegion()
  230. try:
  231. body
  232. finally:
  233. deallocAll()
  234. tlRegion = oldRegion
  235. when false:
  236. proc joinRegion*(dest: var MemRegion; src: MemRegion) =
  237. # merging is not hard.
  238. if dest.head.isNil:
  239. dest.head = src.head
  240. else:
  241. dest.tail.next = src.head
  242. dest.tail = src.tail
  243. dest.bump = src.bump
  244. dest.remaining = src.remaining
  245. dest.nextChunkSize = max(dest.nextChunkSize, src.nextChunkSize)
  246. inc dest.totalSize, src.totalSize
  247. proc isOnHeap*(r: MemRegion; p: pointer): bool =
  248. # the tail chunk is the largest, so check it first. It's also special
  249. # in that contains the current bump pointer:
  250. if r.tail >= p and p < r.bump:
  251. return true
  252. var it = r.head
  253. while it != r.tail:
  254. if it >= p and p <= it+!it.size: return true
  255. it = it.next
  256. proc rawNewObj(r: var MemRegion, typ: PNimType, size: int): pointer =
  257. var res = cast[ptr ObjHeader](allocFast(r, size + sizeof(ObjHeader)))
  258. res.typ = typ
  259. if typ.finalizer != nil:
  260. res.nextFinal = r.head.head
  261. r.head.head = res
  262. result = res +! sizeof(ObjHeader)
  263. proc rawNewSeq(r: var MemRegion, typ: PNimType, size: int): pointer =
  264. var res = cast[ptr SeqHeader](allocFast(r, size + sizeof(SeqHeader)))
  265. res.typ = typ
  266. res.region = addr(r)
  267. result = res +! sizeof(SeqHeader)
  268. proc newObj(typ: PNimType, size: int): pointer {.compilerRtl.} =
  269. sysAssert typ.kind notin {tySequence, tyString}, "newObj cannot be used to construct seqs"
  270. result = rawNewObj(tlRegion, typ, size)
  271. zeroMem(result, size)
  272. when defined(memProfiler): nimProfile(size)
  273. proc newObjNoInit(typ: PNimType, size: int): pointer {.compilerRtl.} =
  274. sysAssert typ.kind notin {tySequence, tyString}, "newObj cannot be used to construct seqs"
  275. result = rawNewObj(tlRegion, typ, size)
  276. when defined(memProfiler): nimProfile(size)
  277. {.push overflowChecks: on.}
  278. proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} =
  279. let size = roundup(align(GenericSeqSize, typ.base.align) + len * typ.base.size, MemAlign)
  280. result = rawNewSeq(tlRegion, typ, size)
  281. zeroMem(result, size)
  282. cast[PGenericSeq](result).len = len
  283. cast[PGenericSeq](result).reserved = len
  284. proc newStr(typ: PNimType, len: int; init: bool): pointer {.compilerRtl.} =
  285. let size = roundup(len + GenericSeqSize, MemAlign)
  286. result = rawNewSeq(tlRegion, typ, size)
  287. if init: zeroMem(result, size)
  288. cast[PGenericSeq](result).len = 0
  289. cast[PGenericSeq](result).reserved = len
  290. {.pop.}
  291. proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
  292. result = rawNewObj(tlRegion, typ, size)
  293. zeroMem(result, size)
  294. proc newSeqRC1(typ: PNimType, len: int): pointer {.compilerRtl.} =
  295. result = newSeq(typ, len)
  296. proc growObj(regionUnused: var MemRegion; old: pointer, newsize: int): pointer =
  297. let sh = cast[ptr SeqHeader](old -! sizeof(SeqHeader))
  298. let typ = sh.typ
  299. result = rawNewSeq(sh.region[], typ,
  300. roundup(newsize, MemAlign))
  301. let elemSize = if typ.kind == tyString: 1 else: typ.base.size
  302. let elemAlign = if typ.kind == tyString: 1 else: typ.base.align
  303. let oldsize = align(GenericSeqSize, elemAlign) + cast[PGenericSeq](old).len*elemSize
  304. zeroMem(result +! oldsize, newsize-oldsize)
  305. copyMem(result, old, oldsize)
  306. dealloc(sh.region[], old, roundup(oldsize, MemAlign))
  307. proc growObj(old: pointer, newsize: int): pointer {.rtl.} =
  308. result = growObj(tlRegion, old, newsize)
  309. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  310. dest[] = src
  311. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  312. dest[] = src
  313. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  314. deprecated: "old compiler compat".} = asgnRef(dest, src)
  315. proc allocImpl(size: Natural): pointer =
  316. result = c_malloc(cast[csize_t](size))
  317. if result == nil: raiseOutOfMem()
  318. proc alloc0Impl(size: Natural): pointer =
  319. result = alloc(size)
  320. zeroMem(result, size)
  321. proc reallocImpl(p: pointer, newsize: Natural): pointer =
  322. result = c_realloc(p, cast[csize_t](newsize))
  323. if result == nil: raiseOutOfMem()
  324. proc realloc0Impl(p: pointer, oldsize, newsize: Natural): pointer =
  325. result = c_realloc(p, cast[csize_t](newsize))
  326. if result == nil: raiseOutOfMem()
  327. if newsize > oldsize:
  328. zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
  329. proc deallocImpl(p: pointer) = c_free(p)
  330. proc alloc0(r: var MemRegion; size: Natural): pointer =
  331. # ignore the region. That is correct for the channels module
  332. # but incorrect in general. XXX
  333. result = alloc0(size)
  334. proc alloc(r: var MemRegion; size: Natural): pointer =
  335. # ignore the region. That is correct for the channels module
  336. # but incorrect in general. XXX
  337. result = alloc(size)
  338. proc dealloc(r: var MemRegion; p: pointer) = dealloc(p)
  339. proc allocSharedImpl(size: Natural): pointer =
  340. result = c_malloc(cast[csize_t](size))
  341. if result == nil: raiseOutOfMem()
  342. proc allocShared0Impl(size: Natural): pointer =
  343. result = alloc(size)
  344. zeroMem(result, size)
  345. proc reallocSharedImpl(p: pointer, newsize: Natural): pointer =
  346. result = c_realloc(p, cast[csize_t](newsize))
  347. if result == nil: raiseOutOfMem()
  348. proc reallocShared0Impl(p: pointer, oldsize, newsize: Natural): pointer =
  349. result = c_realloc(p, cast[csize_t](newsize))
  350. if result == nil: raiseOutOfMem()
  351. if newsize > oldsize:
  352. zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
  353. proc deallocSharedImpl(p: pointer) = c_free(p)
  354. when hasThreadSupport:
  355. proc getFreeSharedMem(): int = 0
  356. proc getTotalSharedMem(): int = 0
  357. proc getOccupiedSharedMem(): int = 0
  358. proc GC_disable() = discard
  359. proc GC_enable() = discard
  360. proc GC_fullCollect() = discard
  361. proc GC_setStrategy(strategy: GC_Strategy) = discard
  362. proc GC_enableMarkAndSweep() = discard
  363. proc GC_disableMarkAndSweep() = discard
  364. proc GC_getStatistics(): string = return ""
  365. proc getOccupiedMem(): int =
  366. result = tlRegion.totalSize - tlRegion.remaining
  367. proc getFreeMem(): int = tlRegion.remaining
  368. proc getTotalMem(): int =
  369. result = tlRegion.totalSize
  370. proc getOccupiedMem*(r: MemRegion): int =
  371. result = r.totalSize - r.remaining
  372. proc getFreeMem*(r: MemRegion): int = r.remaining
  373. proc getTotalMem*(r: MemRegion): int =
  374. result = r.totalSize
  375. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  376. proc nimGCref(x: pointer) {.compilerProc.} = discard
  377. proc nimGCunref(x: pointer) {.compilerProc.} = discard