gc_regions.nim 13 KB

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