alloc.nim 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Low level allocator for Nim. Has been designed to support the GC.
  10. {.push profiler:off.}
  11. include osalloc
  12. template track(op, address, size) =
  13. when defined(memTracker):
  14. memTrackerOp(op, address, size)
  15. # We manage *chunks* of memory. Each chunk is a multiple of the page size.
  16. # Each chunk starts at an address that is divisible by the page size.
  17. const
  18. nimMinHeapPages {.intdefine.} = 128 # 0.5 MB
  19. SmallChunkSize = PageSize
  20. MaxFli = 30
  21. MaxLog2Sli = 5 # 32, this cannot be increased without changing 'uint32'
  22. # everywhere!
  23. MaxSli = 1 shl MaxLog2Sli
  24. FliOffset = 6
  25. RealFli = MaxFli - FliOffset
  26. # size of chunks in last matrix bin
  27. MaxBigChunkSize = 1 shl MaxFli - 1 shl (MaxFli-MaxLog2Sli-1)
  28. HugeChunkSize = MaxBigChunkSize + 1
  29. type
  30. PTrunk = ptr Trunk
  31. Trunk = object
  32. next: PTrunk # all nodes are connected with this pointer
  33. key: int # start address at bit 0
  34. bits: array[0..IntsPerTrunk-1, uint] # a bit vector
  35. TrunkBuckets = array[0..255, PTrunk]
  36. IntSet = object
  37. data: TrunkBuckets
  38. type
  39. FreeCell {.final, pure.} = object
  40. next: ptr FreeCell # next free cell in chunk (overlaid with refcount)
  41. when not defined(gcDestructors):
  42. zeroField: int # 0 means cell is not used (overlaid with typ field)
  43. # 1 means cell is manually managed pointer
  44. # otherwise a PNimType is stored in there
  45. else:
  46. alignment: int
  47. PChunk = ptr BaseChunk
  48. PBigChunk = ptr BigChunk
  49. PSmallChunk = ptr SmallChunk
  50. BaseChunk {.pure, inheritable.} = object
  51. prevSize: int # size of previous chunk; for coalescing
  52. # 0th bit == 1 if 'used
  53. size: int # if < PageSize it is a small chunk
  54. SmallChunk = object of BaseChunk
  55. next, prev: PSmallChunk # chunks of the same size
  56. freeList: ptr FreeCell
  57. free: int # how many bytes remain
  58. acc: int # accumulator for small object allocation
  59. when defined(nimAlignPragma):
  60. data {.align: MemAlign.}: UncheckedArray[byte] # start of usable memory
  61. else:
  62. data: UncheckedArray[byte]
  63. BigChunk = object of BaseChunk # not necessarily > PageSize!
  64. next, prev: PBigChunk # chunks of the same (or bigger) size
  65. when defined(nimAlignPragma):
  66. data {.align: MemAlign.}: UncheckedArray[byte] # start of usable memory
  67. else:
  68. data: UncheckedArray[byte]
  69. template smallChunkOverhead(): untyped = sizeof(SmallChunk)
  70. template bigChunkOverhead(): untyped = sizeof(BigChunk)
  71. # ------------- chunk table ---------------------------------------------------
  72. # We use a PtrSet of chunk starts and a table[Page, chunksize] for chunk
  73. # endings of big chunks. This is needed by the merging operation. The only
  74. # remaining operation is best-fit for big chunks. Since there is a size-limit
  75. # for big chunks (because greater than the limit means they are returned back
  76. # to the OS), a fixed size array can be used.
  77. type
  78. PLLChunk = ptr LLChunk
  79. LLChunk = object ## *low-level* chunk
  80. size: int # remaining size
  81. acc: int # accumulator
  82. next: PLLChunk # next low-level chunk; only needed for dealloc
  83. PAvlNode = ptr AvlNode
  84. AvlNode = object
  85. link: array[0..1, PAvlNode] # Left (0) and right (1) links
  86. key, upperBound: int
  87. level: int
  88. HeapLinks = object
  89. len: int
  90. chunks: array[30, (PBigChunk, int)]
  91. next: ptr HeapLinks
  92. MemRegion = object
  93. when not defined(gcDestructors):
  94. minLargeObj, maxLargeObj: int
  95. freeSmallChunks: array[0..SmallChunkSize div MemAlign-1, PSmallChunk]
  96. flBitmap: uint32
  97. slBitmap: array[RealFli, uint32]
  98. matrix: array[RealFli, array[MaxSli, PBigChunk]]
  99. llmem: PLLChunk
  100. currMem, maxMem, freeMem, occ: int # memory sizes (allocated from OS)
  101. lastSize: int # needed for the case that OS gives us pages linearly
  102. chunkStarts: IntSet
  103. when not defined(gcDestructors):
  104. root, deleted, last, freeAvlNodes: PAvlNode
  105. locked, blockChunkSizeIncrease: bool # if locked, we cannot free pages.
  106. nextChunkSize: int
  107. when not defined(gcDestructors):
  108. bottomData: AvlNode
  109. heapLinks: HeapLinks
  110. when defined(nimTypeNames):
  111. allocCounter, deallocCounter: int
  112. const
  113. fsLookupTable: array[byte, int8] = [
  114. -1'i8, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  115. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  116. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  117. 5, 5, 5, 5, 5, 5, 5, 5,
  118. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  119. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  120. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  121. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  122. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  123. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  124. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  125. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  126. 7, 7, 7, 7, 7, 7, 7, 7
  127. ]
  128. proc msbit(x: uint32): int {.inline.} =
  129. let a = if x <= 0xff_ff'u32:
  130. (if x <= 0xff: 0 else: 8)
  131. else:
  132. (if x <= 0xff_ff_ff'u32: 16 else: 24)
  133. result = int(fsLookupTable[byte(x shr a)]) + a
  134. proc lsbit(x: uint32): int {.inline.} =
  135. msbit(x and ((not x) + 1))
  136. proc setBit(nr: int; dest: var uint32) {.inline.} =
  137. dest = dest or (1u32 shl (nr and 0x1f))
  138. proc clearBit(nr: int; dest: var uint32) {.inline.} =
  139. dest = dest and not (1u32 shl (nr and 0x1f))
  140. proc mappingSearch(r, fl, sl: var int) {.inline.} =
  141. #let t = (1 shl (msbit(uint32 r) - MaxLog2Sli)) - 1
  142. # This diverges from the standard TLSF algorithm because we need to ensure
  143. # PageSize alignment:
  144. let t = roundup((1 shl (msbit(uint32 r) - MaxLog2Sli)), PageSize) - 1
  145. r = r + t
  146. r = r and not t
  147. r = min(r, MaxBigChunkSize)
  148. fl = msbit(uint32 r)
  149. sl = (r shr (fl - MaxLog2Sli)) - MaxSli
  150. dec fl, FliOffset
  151. sysAssert((r and PageMask) == 0, "mappingSearch: still not aligned")
  152. # See http://www.gii.upv.es/tlsf/files/papers/tlsf_desc.pdf for details of
  153. # this algorithm.
  154. proc mappingInsert(r: int): tuple[fl, sl: int] {.inline.} =
  155. sysAssert((r and PageMask) == 0, "mappingInsert: still not aligned")
  156. result.fl = msbit(uint32 r)
  157. result.sl = (r shr (result.fl - MaxLog2Sli)) - MaxSli
  158. dec result.fl, FliOffset
  159. template mat(): untyped = a.matrix[fl][sl]
  160. proc findSuitableBlock(a: MemRegion; fl, sl: var int): PBigChunk {.inline.} =
  161. let tmp = a.slBitmap[fl] and (not 0u32 shl sl)
  162. result = nil
  163. if tmp != 0:
  164. sl = lsbit(tmp)
  165. result = mat()
  166. else:
  167. fl = lsbit(a.flBitmap and (not 0u32 shl (fl + 1)))
  168. if fl > 0:
  169. sl = lsbit(a.slBitmap[fl])
  170. result = mat()
  171. template clearBits(sl, fl) =
  172. clearBit(sl, a.slBitmap[fl])
  173. if a.slBitmap[fl] == 0u32:
  174. # do not forget to cascade:
  175. clearBit(fl, a.flBitmap)
  176. proc removeChunkFromMatrix(a: var MemRegion; b: PBigChunk) =
  177. let (fl, sl) = mappingInsert(b.size)
  178. if b.next != nil: b.next.prev = b.prev
  179. if b.prev != nil: b.prev.next = b.next
  180. if mat() == b:
  181. mat() = b.next
  182. if mat() == nil:
  183. clearBits(sl, fl)
  184. b.prev = nil
  185. b.next = nil
  186. proc removeChunkFromMatrix2(a: var MemRegion; b: PBigChunk; fl, sl: int) =
  187. mat() = b.next
  188. if mat() != nil:
  189. mat().prev = nil
  190. else:
  191. clearBits(sl, fl)
  192. b.prev = nil
  193. b.next = nil
  194. proc addChunkToMatrix(a: var MemRegion; b: PBigChunk) =
  195. let (fl, sl) = mappingInsert(b.size)
  196. b.prev = nil
  197. b.next = mat()
  198. if mat() != nil:
  199. mat().prev = b
  200. mat() = b
  201. setBit(sl, a.slBitmap[fl])
  202. setBit(fl, a.flBitmap)
  203. proc incCurrMem(a: var MemRegion, bytes: int) {.inline.} =
  204. inc(a.currMem, bytes)
  205. proc decCurrMem(a: var MemRegion, bytes: int) {.inline.} =
  206. a.maxMem = max(a.maxMem, a.currMem)
  207. dec(a.currMem, bytes)
  208. proc getMaxMem(a: var MemRegion): int =
  209. # Since we update maxPagesCount only when freeing pages,
  210. # maxPagesCount may not be up to date. Thus we use the
  211. # maximum of these both values here:
  212. result = max(a.currMem, a.maxMem)
  213. proc llAlloc(a: var MemRegion, size: int): pointer =
  214. # *low-level* alloc for the memory managers data structures. Deallocation
  215. # is done at the end of the allocator's life time.
  216. if a.llmem == nil or size > a.llmem.size:
  217. # the requested size is ``roundup(size+sizeof(LLChunk), PageSize)``, but
  218. # since we know ``size`` is a (small) constant, we know the requested size
  219. # is one page:
  220. sysAssert roundup(size+sizeof(LLChunk), PageSize) == PageSize, "roundup 6"
  221. var old = a.llmem # can be nil and is correct with nil
  222. a.llmem = cast[PLLChunk](osAllocPages(PageSize))
  223. when defined(nimAvlcorruption):
  224. trackLocation(a.llmem, PageSize)
  225. incCurrMem(a, PageSize)
  226. a.llmem.size = PageSize - sizeof(LLChunk)
  227. a.llmem.acc = sizeof(LLChunk)
  228. a.llmem.next = old
  229. result = cast[pointer](cast[ByteAddress](a.llmem) + a.llmem.acc)
  230. dec(a.llmem.size, size)
  231. inc(a.llmem.acc, size)
  232. zeroMem(result, size)
  233. when not defined(gcDestructors):
  234. proc getBottom(a: var MemRegion): PAvlNode =
  235. result = addr(a.bottomData)
  236. if result.link[0] == nil:
  237. result.link[0] = result
  238. result.link[1] = result
  239. proc allocAvlNode(a: var MemRegion, key, upperBound: int): PAvlNode =
  240. if a.freeAvlNodes != nil:
  241. result = a.freeAvlNodes
  242. a.freeAvlNodes = a.freeAvlNodes.link[0]
  243. else:
  244. result = cast[PAvlNode](llAlloc(a, sizeof(AvlNode)))
  245. when defined(nimAvlcorruption):
  246. cprintf("tracking location: %p\n", result)
  247. result.key = key
  248. result.upperBound = upperBound
  249. let bottom = getBottom(a)
  250. result.link[0] = bottom
  251. result.link[1] = bottom
  252. result.level = 1
  253. #when defined(nimAvlcorruption):
  254. # track("allocAvlNode", result, sizeof(AvlNode))
  255. sysAssert(bottom == addr(a.bottomData), "bottom data")
  256. sysAssert(bottom.link[0] == bottom, "bottom link[0]")
  257. sysAssert(bottom.link[1] == bottom, "bottom link[1]")
  258. proc deallocAvlNode(a: var MemRegion, n: PAvlNode) {.inline.} =
  259. n.link[0] = a.freeAvlNodes
  260. a.freeAvlNodes = n
  261. proc addHeapLink(a: var MemRegion; p: PBigChunk, size: int) =
  262. var it = addr(a.heapLinks)
  263. while it != nil and it.len >= it.chunks.len: it = it.next
  264. if it == nil:
  265. var n = cast[ptr HeapLinks](llAlloc(a, sizeof(HeapLinks)))
  266. n.next = a.heapLinks.next
  267. a.heapLinks.next = n
  268. n.chunks[0] = (p, size)
  269. n.len = 1
  270. else:
  271. let L = it.len
  272. it.chunks[L] = (p, size)
  273. inc it.len
  274. when not defined(gcDestructors):
  275. include "system/avltree"
  276. proc llDeallocAll(a: var MemRegion) =
  277. var it = a.llmem
  278. while it != nil:
  279. # we know each block in the list has the size of 1 page:
  280. var next = it.next
  281. osDeallocPages(it, PageSize)
  282. it = next
  283. a.llmem = nil
  284. proc intSetGet(t: IntSet, key: int): PTrunk =
  285. var it = t.data[key and high(t.data)]
  286. while it != nil:
  287. if it.key == key: return it
  288. it = it.next
  289. result = nil
  290. proc intSetPut(a: var MemRegion, t: var IntSet, key: int): PTrunk =
  291. result = intSetGet(t, key)
  292. if result == nil:
  293. result = cast[PTrunk](llAlloc(a, sizeof(result[])))
  294. result.next = t.data[key and high(t.data)]
  295. t.data[key and high(t.data)] = result
  296. result.key = key
  297. proc contains(s: IntSet, key: int): bool =
  298. var t = intSetGet(s, key shr TrunkShift)
  299. if t != nil:
  300. var u = key and TrunkMask
  301. result = (t.bits[u shr IntShift] and (uint(1) shl (u and IntMask))) != 0
  302. else:
  303. result = false
  304. proc incl(a: var MemRegion, s: var IntSet, key: int) =
  305. var t = intSetPut(a, s, key shr TrunkShift)
  306. var u = key and TrunkMask
  307. t.bits[u shr IntShift] = t.bits[u shr IntShift] or (uint(1) shl (u and IntMask))
  308. proc excl(s: var IntSet, key: int) =
  309. var t = intSetGet(s, key shr TrunkShift)
  310. if t != nil:
  311. var u = key and TrunkMask
  312. t.bits[u shr IntShift] = t.bits[u shr IntShift] and not
  313. (uint(1) shl (u and IntMask))
  314. iterator elements(t: IntSet): int {.inline.} =
  315. # while traversing it is forbidden to change the set!
  316. for h in 0..high(t.data):
  317. var r = t.data[h]
  318. while r != nil:
  319. var i = 0
  320. while i <= high(r.bits):
  321. var w = r.bits[i] # taking a copy of r.bits[i] here is correct, because
  322. # modifying operations are not allowed during traversation
  323. var j = 0
  324. while w != 0: # test all remaining bits for zero
  325. if (w and 1) != 0: # the bit is set!
  326. yield (r.key shl TrunkShift) or (i shl IntShift +% j)
  327. inc(j)
  328. w = w shr 1
  329. inc(i)
  330. r = r.next
  331. proc isSmallChunk(c: PChunk): bool {.inline.} =
  332. result = c.size <= SmallChunkSize-smallChunkOverhead()
  333. proc chunkUnused(c: PChunk): bool {.inline.} =
  334. result = (c.prevSize and 1) == 0
  335. iterator allObjects(m: var MemRegion): pointer {.inline.} =
  336. m.locked = true
  337. for s in elements(m.chunkStarts):
  338. # we need to check here again as it could have been modified:
  339. if s in m.chunkStarts:
  340. let c = cast[PChunk](s shl PageShift)
  341. if not chunkUnused(c):
  342. if isSmallChunk(c):
  343. var c = cast[PSmallChunk](c)
  344. let size = c.size
  345. var a = cast[ByteAddress](addr(c.data))
  346. let limit = a + c.acc
  347. while a <% limit:
  348. yield cast[pointer](a)
  349. a = a +% size
  350. else:
  351. let c = cast[PBigChunk](c)
  352. yield addr(c.data)
  353. m.locked = false
  354. proc iterToProc*(iter: typed, envType: typedesc; procName: untyped) {.
  355. magic: "Plugin", compileTime.}
  356. when not defined(gcDestructors):
  357. proc isCell(p: pointer): bool {.inline.} =
  358. result = cast[ptr FreeCell](p).zeroField >% 1
  359. # ------------- chunk management ----------------------------------------------
  360. proc pageIndex(c: PChunk): int {.inline.} =
  361. result = cast[ByteAddress](c) shr PageShift
  362. proc pageIndex(p: pointer): int {.inline.} =
  363. result = cast[ByteAddress](p) shr PageShift
  364. proc pageAddr(p: pointer): PChunk {.inline.} =
  365. result = cast[PChunk](cast[ByteAddress](p) and not PageMask)
  366. #sysAssert(Contains(allocator.chunkStarts, pageIndex(result)))
  367. when false:
  368. proc writeFreeList(a: MemRegion) =
  369. var it = a.freeChunksList
  370. c_fprintf(stdout, "freeChunksList: %p\n", it)
  371. while it != nil:
  372. c_fprintf(stdout, "it: %p, next: %p, prev: %p, size: %ld\n",
  373. it, it.next, it.prev, it.size)
  374. it = it.next
  375. const nimMaxHeap {.intdefine.} = 0
  376. proc requestOsChunks(a: var MemRegion, size: int): PBigChunk =
  377. when not defined(emscripten):
  378. if not a.blockChunkSizeIncrease:
  379. let usedMem = a.occ #a.currMem # - a.freeMem
  380. when nimMaxHeap != 0:
  381. if usedMem > nimMaxHeap * 1024 * 1024:
  382. raiseOutOfMem()
  383. if usedMem < 64 * 1024:
  384. a.nextChunkSize = PageSize*4
  385. else:
  386. a.nextChunkSize = min(roundup(usedMem shr 2, PageSize), a.nextChunkSize * 2)
  387. a.nextChunkSize = min(a.nextChunkSize, MaxBigChunkSize)
  388. var size = size
  389. if size > a.nextChunkSize:
  390. result = cast[PBigChunk](osAllocPages(size))
  391. else:
  392. result = cast[PBigChunk](osTryAllocPages(a.nextChunkSize))
  393. if result == nil:
  394. result = cast[PBigChunk](osAllocPages(size))
  395. a.blockChunkSizeIncrease = true
  396. else:
  397. size = a.nextChunkSize
  398. incCurrMem(a, size)
  399. inc(a.freeMem, size)
  400. a.addHeapLink(result, size)
  401. when defined(debugHeapLinks):
  402. cprintf("owner: %p; result: %p; next pointer %p; size: %ld\n", addr(a),
  403. result, result.heapLink, result.size)
  404. when defined(memtracker):
  405. trackLocation(addr result.size, sizeof(int))
  406. sysAssert((cast[ByteAddress](result) and PageMask) == 0, "requestOsChunks 1")
  407. #zeroMem(result, size)
  408. result.next = nil
  409. result.prev = nil
  410. result.size = size
  411. # update next.prevSize:
  412. var nxt = cast[ByteAddress](result) +% size
  413. sysAssert((nxt and PageMask) == 0, "requestOsChunks 2")
  414. var next = cast[PChunk](nxt)
  415. if pageIndex(next) in a.chunkStarts:
  416. #echo("Next already allocated!")
  417. next.prevSize = size or (next.prevSize and 1)
  418. # set result.prevSize:
  419. var lastSize = if a.lastSize != 0: a.lastSize else: PageSize
  420. var prv = cast[ByteAddress](result) -% lastSize
  421. sysAssert((nxt and PageMask) == 0, "requestOsChunks 3")
  422. var prev = cast[PChunk](prv)
  423. if pageIndex(prev) in a.chunkStarts and prev.size == lastSize:
  424. #echo("Prev already allocated!")
  425. result.prevSize = lastSize or (result.prevSize and 1)
  426. else:
  427. result.prevSize = 0 or (result.prevSize and 1) # unknown
  428. # but do not overwrite 'used' field
  429. a.lastSize = size # for next request
  430. sysAssert((cast[int](result) and PageMask) == 0, "requestOschunks: unaligned chunk")
  431. proc isAccessible(a: MemRegion, p: pointer): bool {.inline.} =
  432. result = contains(a.chunkStarts, pageIndex(p))
  433. proc contains[T](list, x: T): bool =
  434. var it = list
  435. while it != nil:
  436. if it == x: return true
  437. it = it.next
  438. proc listAdd[T](head: var T, c: T) {.inline.} =
  439. sysAssert(c notin head, "listAdd 1")
  440. sysAssert c.prev == nil, "listAdd 2"
  441. sysAssert c.next == nil, "listAdd 3"
  442. c.next = head
  443. if head != nil:
  444. sysAssert head.prev == nil, "listAdd 4"
  445. head.prev = c
  446. head = c
  447. proc listRemove[T](head: var T, c: T) {.inline.} =
  448. sysAssert(c in head, "listRemove")
  449. if c == head:
  450. head = c.next
  451. sysAssert c.prev == nil, "listRemove 2"
  452. if head != nil: head.prev = nil
  453. else:
  454. sysAssert c.prev != nil, "listRemove 3"
  455. c.prev.next = c.next
  456. if c.next != nil: c.next.prev = c.prev
  457. c.next = nil
  458. c.prev = nil
  459. proc updatePrevSize(a: var MemRegion, c: PBigChunk,
  460. prevSize: int) {.inline.} =
  461. var ri = cast[PChunk](cast[ByteAddress](c) +% c.size)
  462. sysAssert((cast[ByteAddress](ri) and PageMask) == 0, "updatePrevSize")
  463. if isAccessible(a, ri):
  464. ri.prevSize = prevSize or (ri.prevSize and 1)
  465. proc splitChunk2(a: var MemRegion, c: PBigChunk, size: int): PBigChunk =
  466. result = cast[PBigChunk](cast[ByteAddress](c) +% size)
  467. result.size = c.size - size
  468. track("result.size", addr result.size, sizeof(int))
  469. # XXX check if these two nil assignments are dead code given
  470. # addChunkToMatrix's implementation:
  471. result.next = nil
  472. result.prev = nil
  473. # size and not used:
  474. result.prevSize = size
  475. sysAssert((size and 1) == 0, "splitChunk 2")
  476. sysAssert((size and PageMask) == 0,
  477. "splitChunk: size is not a multiple of the PageSize")
  478. updatePrevSize(a, c, result.size)
  479. c.size = size
  480. incl(a, a.chunkStarts, pageIndex(result))
  481. proc splitChunk(a: var MemRegion, c: PBigChunk, size: int) =
  482. let rest = splitChunk2(a, c, size)
  483. addChunkToMatrix(a, rest)
  484. proc freeBigChunk(a: var MemRegion, c: PBigChunk) =
  485. var c = c
  486. sysAssert(c.size >= PageSize, "freeBigChunk")
  487. inc(a.freeMem, c.size)
  488. c.prevSize = c.prevSize and not 1 # set 'used' to false
  489. when coalescLeft:
  490. let prevSize = c.prevSize
  491. if prevSize != 0:
  492. var le = cast[PChunk](cast[ByteAddress](c) -% prevSize)
  493. sysAssert((cast[ByteAddress](le) and PageMask) == 0, "freeBigChunk 4")
  494. if isAccessible(a, le) and chunkUnused(le):
  495. sysAssert(not isSmallChunk(le), "freeBigChunk 5")
  496. if not isSmallChunk(le) and le.size < MaxBigChunkSize:
  497. removeChunkFromMatrix(a, cast[PBigChunk](le))
  498. inc(le.size, c.size)
  499. excl(a.chunkStarts, pageIndex(c))
  500. c = cast[PBigChunk](le)
  501. if c.size > MaxBigChunkSize:
  502. let rest = splitChunk2(a, c, MaxBigChunkSize)
  503. addChunkToMatrix(a, c)
  504. c = rest
  505. when coalescRight:
  506. var ri = cast[PChunk](cast[ByteAddress](c) +% c.size)
  507. sysAssert((cast[ByteAddress](ri) and PageMask) == 0, "freeBigChunk 2")
  508. if isAccessible(a, ri) and chunkUnused(ri):
  509. sysAssert(not isSmallChunk(ri), "freeBigChunk 3")
  510. if not isSmallChunk(ri) and c.size < MaxBigChunkSize:
  511. removeChunkFromMatrix(a, cast[PBigChunk](ri))
  512. inc(c.size, ri.size)
  513. excl(a.chunkStarts, pageIndex(ri))
  514. if c.size > MaxBigChunkSize:
  515. let rest = splitChunk2(a, c, MaxBigChunkSize)
  516. addChunkToMatrix(a, rest)
  517. addChunkToMatrix(a, c)
  518. proc getBigChunk(a: var MemRegion, size: int): PBigChunk =
  519. sysAssert(size > 0, "getBigChunk 2")
  520. var size = size # roundup(size, PageSize)
  521. var fl = 0
  522. var sl = 0
  523. mappingSearch(size, fl, sl)
  524. sysAssert((size and PageMask) == 0, "getBigChunk: unaligned chunk")
  525. result = findSuitableBlock(a, fl, sl)
  526. if result == nil:
  527. if size < nimMinHeapPages * PageSize:
  528. result = requestOsChunks(a, nimMinHeapPages * PageSize)
  529. splitChunk(a, result, size)
  530. else:
  531. result = requestOsChunks(a, size)
  532. # if we over allocated split the chunk:
  533. if result.size > size:
  534. splitChunk(a, result, size)
  535. else:
  536. removeChunkFromMatrix2(a, result, fl, sl)
  537. if result.size >= size + PageSize:
  538. splitChunk(a, result, size)
  539. # set 'used' to to true:
  540. result.prevSize = 1
  541. track("setUsedToFalse", addr result.size, sizeof(int))
  542. incl(a, a.chunkStarts, pageIndex(result))
  543. dec(a.freeMem, size)
  544. proc getHugeChunk(a: var MemRegion; size: int): PBigChunk =
  545. result = cast[PBigChunk](osAllocPages(size))
  546. incCurrMem(a, size)
  547. # XXX add this to the heap links. But also remove it from it later.
  548. when false: a.addHeapLink(result, size)
  549. sysAssert((cast[ByteAddress](result) and PageMask) == 0, "getHugeChunk")
  550. result.next = nil
  551. result.prev = nil
  552. result.size = size
  553. # set 'used' to to true:
  554. result.prevSize = 1
  555. incl(a, a.chunkStarts, pageIndex(result))
  556. proc freeHugeChunk(a: var MemRegion; c: PBigChunk) =
  557. let size = c.size
  558. sysAssert(size >= HugeChunkSize, "freeHugeChunk: invalid size")
  559. excl(a.chunkStarts, pageIndex(c))
  560. decCurrMem(a, size)
  561. osDeallocPages(c, size)
  562. proc getSmallChunk(a: var MemRegion): PSmallChunk =
  563. var res = getBigChunk(a, PageSize)
  564. sysAssert res.prev == nil, "getSmallChunk 1"
  565. sysAssert res.next == nil, "getSmallChunk 2"
  566. result = cast[PSmallChunk](res)
  567. # -----------------------------------------------------------------------------
  568. when not defined(gcDestructors):
  569. proc isAllocatedPtr(a: MemRegion, p: pointer): bool {.benign.}
  570. when true:
  571. template allocInv(a: MemRegion): bool = true
  572. else:
  573. proc allocInv(a: MemRegion): bool =
  574. ## checks some (not all yet) invariants of the allocator's data structures.
  575. for s in low(a.freeSmallChunks)..high(a.freeSmallChunks):
  576. var c = a.freeSmallChunks[s]
  577. while not (c == nil):
  578. if c.next == c:
  579. echo "[SYSASSERT] c.next == c"
  580. return false
  581. if not (c.size == s * MemAlign):
  582. echo "[SYSASSERT] c.size != s * MemAlign"
  583. return false
  584. var it = c.freeList
  585. while not (it == nil):
  586. if not (it.zeroField == 0):
  587. echo "[SYSASSERT] it.zeroField != 0"
  588. c_printf("%ld %p\n", it.zeroField, it)
  589. return false
  590. it = it.next
  591. c = c.next
  592. result = true
  593. when false:
  594. var
  595. rsizes: array[50_000, int]
  596. rsizesLen: int
  597. proc trackSize(size: int) =
  598. rsizes[rsizesLen] = size
  599. inc rsizesLen
  600. proc untrackSize(size: int) =
  601. for i in 0 .. rsizesLen-1:
  602. if rsizes[i] == size:
  603. rsizes[i] = rsizes[rsizesLen-1]
  604. dec rsizesLen
  605. return
  606. c_fprintf(stdout, "%ld\n", size)
  607. sysAssert(false, "untracked size!")
  608. else:
  609. template trackSize(x) = discard
  610. template untrackSize(x) = discard
  611. proc rawAlloc(a: var MemRegion, requestedSize: int): pointer =
  612. when defined(nimTypeNames):
  613. inc(a.allocCounter)
  614. sysAssert(allocInv(a), "rawAlloc: begin")
  615. sysAssert(roundup(65, 8) == 72, "rawAlloc: roundup broken")
  616. var size = roundup(requestedSize, MemAlign)
  617. sysAssert(size >= sizeof(FreeCell), "rawAlloc: requested size too small")
  618. sysAssert(size >= requestedSize, "insufficient allocated size!")
  619. #c_fprintf(stdout, "alloc; size: %ld; %ld\n", requestedSize, size)
  620. if size <= SmallChunkSize-smallChunkOverhead():
  621. # allocate a small block: for small chunks, we use only its next pointer
  622. var s = size div MemAlign
  623. var c = a.freeSmallChunks[s]
  624. if c == nil:
  625. c = getSmallChunk(a)
  626. c.freeList = nil
  627. sysAssert c.size == PageSize, "rawAlloc 3"
  628. c.size = size
  629. c.acc = size
  630. c.free = SmallChunkSize - smallChunkOverhead() - size
  631. c.next = nil
  632. c.prev = nil
  633. listAdd(a.freeSmallChunks[s], c)
  634. result = addr(c.data)
  635. sysAssert((cast[ByteAddress](result) and (MemAlign-1)) == 0, "rawAlloc 4")
  636. else:
  637. sysAssert(allocInv(a), "rawAlloc: begin c != nil")
  638. sysAssert c.next != c, "rawAlloc 5"
  639. #if c.size != size:
  640. # c_fprintf(stdout, "csize: %lld; size %lld\n", c.size, size)
  641. sysAssert c.size == size, "rawAlloc 6"
  642. if c.freeList == nil:
  643. sysAssert(c.acc + smallChunkOverhead() + size <= SmallChunkSize,
  644. "rawAlloc 7")
  645. result = cast[pointer](cast[ByteAddress](addr(c.data)) +% c.acc)
  646. inc(c.acc, size)
  647. else:
  648. result = c.freeList
  649. when not defined(gcDestructors):
  650. sysAssert(c.freeList.zeroField == 0, "rawAlloc 8")
  651. c.freeList = c.freeList.next
  652. dec(c.free, size)
  653. sysAssert((cast[ByteAddress](result) and (MemAlign-1)) == 0, "rawAlloc 9")
  654. sysAssert(allocInv(a), "rawAlloc: end c != nil")
  655. sysAssert(allocInv(a), "rawAlloc: before c.free < size")
  656. if c.free < size:
  657. sysAssert(allocInv(a), "rawAlloc: before listRemove test")
  658. listRemove(a.freeSmallChunks[s], c)
  659. sysAssert(allocInv(a), "rawAlloc: end listRemove test")
  660. sysAssert(((cast[ByteAddress](result) and PageMask) - smallChunkOverhead()) %%
  661. size == 0, "rawAlloc 21")
  662. sysAssert(allocInv(a), "rawAlloc: end small size")
  663. inc a.occ, size
  664. trackSize(c.size)
  665. else:
  666. size = requestedSize + bigChunkOverhead() # roundup(requestedSize+bigChunkOverhead(), PageSize)
  667. # allocate a large block
  668. var c = if size >= HugeChunkSize: getHugeChunk(a, size)
  669. else: getBigChunk(a, size)
  670. sysAssert c.prev == nil, "rawAlloc 10"
  671. sysAssert c.next == nil, "rawAlloc 11"
  672. result = addr(c.data)
  673. sysAssert((cast[ByteAddress](c) and (MemAlign-1)) == 0, "rawAlloc 13")
  674. sysAssert((cast[ByteAddress](c) and PageMask) == 0, "rawAlloc: Not aligned on a page boundary")
  675. when not defined(gcDestructors):
  676. if a.root == nil: a.root = getBottom(a)
  677. add(a, a.root, cast[ByteAddress](result), cast[ByteAddress](result)+%size)
  678. inc a.occ, c.size
  679. trackSize(c.size)
  680. sysAssert(isAccessible(a, result), "rawAlloc 14")
  681. sysAssert(allocInv(a), "rawAlloc: end")
  682. when logAlloc: cprintf("var pointer_%p = alloc(%ld)\n", result, requestedSize)
  683. proc rawAlloc0(a: var MemRegion, requestedSize: int): pointer =
  684. result = rawAlloc(a, requestedSize)
  685. zeroMem(result, requestedSize)
  686. proc rawDealloc(a: var MemRegion, p: pointer) =
  687. when defined(nimTypeNames):
  688. inc(a.deallocCounter)
  689. #sysAssert(isAllocatedPtr(a, p), "rawDealloc: no allocated pointer")
  690. sysAssert(allocInv(a), "rawDealloc: begin")
  691. var c = pageAddr(p)
  692. if isSmallChunk(c):
  693. # `p` is within a small chunk:
  694. var c = cast[PSmallChunk](c)
  695. var s = c.size
  696. dec a.occ, s
  697. untrackSize(s)
  698. sysAssert a.occ >= 0, "rawDealloc: negative occupied memory (case A)"
  699. sysAssert(((cast[ByteAddress](p) and PageMask) - smallChunkOverhead()) %%
  700. s == 0, "rawDealloc 3")
  701. var f = cast[ptr FreeCell](p)
  702. when not defined(gcDestructors):
  703. #echo("setting to nil: ", $cast[ByteAddress](addr(f.zeroField)))
  704. sysAssert(f.zeroField != 0, "rawDealloc 1")
  705. f.zeroField = 0
  706. f.next = c.freeList
  707. c.freeList = f
  708. when overwriteFree:
  709. # set to 0xff to check for usage after free bugs:
  710. nimSetMem(cast[pointer](cast[int](p) +% sizeof(FreeCell)), -1'i32,
  711. s -% sizeof(FreeCell))
  712. # check if it is not in the freeSmallChunks[s] list:
  713. if c.free < s:
  714. # add it to the freeSmallChunks[s] array:
  715. listAdd(a.freeSmallChunks[s div MemAlign], c)
  716. inc(c.free, s)
  717. else:
  718. inc(c.free, s)
  719. if c.free == SmallChunkSize-smallChunkOverhead():
  720. listRemove(a.freeSmallChunks[s div MemAlign], c)
  721. c.size = SmallChunkSize
  722. freeBigChunk(a, cast[PBigChunk](c))
  723. sysAssert(((cast[ByteAddress](p) and PageMask) - smallChunkOverhead()) %%
  724. s == 0, "rawDealloc 2")
  725. else:
  726. # set to 0xff to check for usage after free bugs:
  727. when overwriteFree: nimSetMem(p, -1'i32, c.size -% bigChunkOverhead())
  728. # free big chunk
  729. var c = cast[PBigChunk](c)
  730. dec a.occ, c.size
  731. untrackSize(c.size)
  732. sysAssert a.occ >= 0, "rawDealloc: negative occupied memory (case B)"
  733. when not defined(gcDestructors):
  734. a.deleted = getBottom(a)
  735. del(a, a.root, cast[int](addr(c.data)))
  736. if c.size >= HugeChunkSize: freeHugeChunk(a, c)
  737. else: freeBigChunk(a, c)
  738. sysAssert(allocInv(a), "rawDealloc: end")
  739. when logAlloc: cprintf("dealloc(pointer_%p)\n", p)
  740. when not defined(gcDestructors):
  741. proc isAllocatedPtr(a: MemRegion, p: pointer): bool =
  742. if isAccessible(a, p):
  743. var c = pageAddr(p)
  744. if not chunkUnused(c):
  745. if isSmallChunk(c):
  746. var c = cast[PSmallChunk](c)
  747. var offset = (cast[ByteAddress](p) and (PageSize-1)) -%
  748. smallChunkOverhead()
  749. result = (c.acc >% offset) and (offset %% c.size == 0) and
  750. (cast[ptr FreeCell](p).zeroField >% 1)
  751. else:
  752. var c = cast[PBigChunk](c)
  753. result = p == addr(c.data) and cast[ptr FreeCell](p).zeroField >% 1
  754. proc prepareForInteriorPointerChecking(a: var MemRegion) {.inline.} =
  755. a.minLargeObj = lowGauge(a.root)
  756. a.maxLargeObj = highGauge(a.root)
  757. proc interiorAllocatedPtr(a: MemRegion, p: pointer): pointer =
  758. if isAccessible(a, p):
  759. var c = pageAddr(p)
  760. if not chunkUnused(c):
  761. if isSmallChunk(c):
  762. var c = cast[PSmallChunk](c)
  763. var offset = (cast[ByteAddress](p) and (PageSize-1)) -%
  764. smallChunkOverhead()
  765. if c.acc >% offset:
  766. sysAssert(cast[ByteAddress](addr(c.data)) +% offset ==
  767. cast[ByteAddress](p), "offset is not what you think it is")
  768. var d = cast[ptr FreeCell](cast[ByteAddress](addr(c.data)) +%
  769. offset -% (offset %% c.size))
  770. if d.zeroField >% 1:
  771. result = d
  772. sysAssert isAllocatedPtr(a, result), " result wrong pointer!"
  773. else:
  774. var c = cast[PBigChunk](c)
  775. var d = addr(c.data)
  776. if p >= d and cast[ptr FreeCell](d).zeroField >% 1:
  777. result = d
  778. sysAssert isAllocatedPtr(a, result), " result wrong pointer!"
  779. else:
  780. var q = cast[int](p)
  781. if q >=% a.minLargeObj and q <=% a.maxLargeObj:
  782. # this check is highly effective! Test fails for 99,96% of all checks on
  783. # an x86-64.
  784. var avlNode = inRange(a.root, q)
  785. if avlNode != nil:
  786. var k = cast[pointer](avlNode.key)
  787. var c = cast[PBigChunk](pageAddr(k))
  788. sysAssert(addr(c.data) == k, " k is not the same as addr(c.data)!")
  789. if cast[ptr FreeCell](k).zeroField >% 1:
  790. result = k
  791. sysAssert isAllocatedPtr(a, result), " result wrong pointer!"
  792. proc ptrSize(p: pointer): int =
  793. when not defined(gcDestructors):
  794. var x = cast[pointer](cast[ByteAddress](p) -% sizeof(FreeCell))
  795. var c = pageAddr(p)
  796. sysAssert(not chunkUnused(c), "ptrSize")
  797. result = c.size -% sizeof(FreeCell)
  798. if not isSmallChunk(c):
  799. dec result, bigChunkOverhead()
  800. else:
  801. var c = pageAddr(p)
  802. sysAssert(not chunkUnused(c), "ptrSize")
  803. result = c.size
  804. if not isSmallChunk(c):
  805. dec result, bigChunkOverhead()
  806. proc alloc(allocator: var MemRegion, size: Natural): pointer {.gcsafe.} =
  807. when not defined(gcDestructors):
  808. result = rawAlloc(allocator, size+sizeof(FreeCell))
  809. cast[ptr FreeCell](result).zeroField = 1 # mark it as used
  810. sysAssert(not isAllocatedPtr(allocator, result), "alloc")
  811. result = cast[pointer](cast[ByteAddress](result) +% sizeof(FreeCell))
  812. track("alloc", result, size)
  813. else:
  814. result = rawAlloc(allocator, size)
  815. proc alloc0(allocator: var MemRegion, size: Natural): pointer =
  816. result = alloc(allocator, size)
  817. zeroMem(result, size)
  818. proc dealloc(allocator: var MemRegion, p: pointer) =
  819. when not defined(gcDestructors):
  820. sysAssert(p != nil, "dealloc: p is nil")
  821. var x = cast[pointer](cast[ByteAddress](p) -% sizeof(FreeCell))
  822. sysAssert(x != nil, "dealloc: x is nil")
  823. sysAssert(isAccessible(allocator, x), "is not accessible")
  824. sysAssert(cast[ptr FreeCell](x).zeroField == 1, "dealloc: object header corrupted")
  825. rawDealloc(allocator, x)
  826. sysAssert(not isAllocatedPtr(allocator, x), "dealloc: object still accessible")
  827. track("dealloc", p, 0)
  828. else:
  829. rawDealloc(allocator, p)
  830. proc realloc(allocator: var MemRegion, p: pointer, newsize: Natural): pointer =
  831. if newsize > 0:
  832. result = alloc(allocator, newsize)
  833. if p != nil:
  834. copyMem(result, p, min(ptrSize(p), newsize))
  835. dealloc(allocator, p)
  836. elif p != nil:
  837. dealloc(allocator, p)
  838. proc realloc0(allocator: var MemRegion, p: pointer, oldsize, newsize: Natural): pointer =
  839. result = realloc(allocator, p, newsize)
  840. if newsize > oldsize:
  841. zeroMem(cast[pointer](cast[uint](result) + uint(oldsize)), newsize - oldsize)
  842. proc deallocOsPages(a: var MemRegion) =
  843. # we free every 'ordinarily' allocated page by iterating over the page bits:
  844. var it = addr(a.heapLinks)
  845. while true:
  846. let next = it.next
  847. for i in 0..it.len-1:
  848. let (p, size) = it.chunks[i]
  849. when defined(debugHeapLinks):
  850. cprintf("owner %p; dealloc A: %p size: %ld; next: %p\n", addr(a),
  851. it, it.size, next)
  852. sysAssert size >= PageSize, "origSize too small"
  853. osDeallocPages(p, size)
  854. it = next
  855. if it == nil: break
  856. # And then we free the pages that are in use for the page bits:
  857. llDeallocAll(a)
  858. proc getFreeMem(a: MemRegion): int {.inline.} = result = a.freeMem
  859. proc getTotalMem(a: MemRegion): int {.inline.} = result = a.currMem
  860. proc getOccupiedMem(a: MemRegion): int {.inline.} =
  861. result = a.occ
  862. # a.currMem - a.freeMem
  863. when defined(nimTypeNames):
  864. proc getMemCounters(a: MemRegion): (int, int) {.inline.} =
  865. (a.allocCounter, a.deallocCounter)
  866. # ---------------------- thread memory region -------------------------------
  867. template instantiateForRegion(allocator: untyped) {.dirty.} =
  868. {.push stackTrace: off.}
  869. when defined(nimFulldebug):
  870. proc interiorAllocatedPtr*(p: pointer): pointer =
  871. result = interiorAllocatedPtr(allocator, p)
  872. proc isAllocatedPtr*(p: pointer): bool =
  873. let p = cast[pointer](cast[ByteAddress](p)-%ByteAddress(sizeof(Cell)))
  874. result = isAllocatedPtr(allocator, p)
  875. proc deallocOsPages = deallocOsPages(allocator)
  876. proc allocImpl(size: Natural): pointer =
  877. result = alloc(allocator, size)
  878. proc alloc0Impl(size: Natural): pointer =
  879. result = alloc0(allocator, size)
  880. proc deallocImpl(p: pointer) =
  881. dealloc(allocator, p)
  882. proc reallocImpl(p: pointer, newSize: Natural): pointer =
  883. result = realloc(allocator, p, newSize)
  884. proc realloc0Impl(p: pointer, oldSize, newSize: Natural): pointer =
  885. result = realloc(allocator, p, newSize)
  886. if newSize > oldSize:
  887. zeroMem(cast[pointer](cast[uint](result) + uint(oldSize)), newSize - oldSize)
  888. when false:
  889. proc countFreeMem(): int =
  890. # only used for assertions
  891. var it = allocator.freeChunksList
  892. while it != nil:
  893. inc(result, it.size)
  894. it = it.next
  895. when hasThreadSupport:
  896. var sharedHeap: MemRegion
  897. var heapLock: SysLock
  898. initSysLock(heapLock)
  899. proc getFreeMem(): int =
  900. #sysAssert(result == countFreeMem())
  901. when hasThreadSupport and defined(gcDestructors):
  902. acquireSys(heapLock)
  903. result = sharedHeap.freeMem
  904. releaseSys(heapLock)
  905. else:
  906. result = allocator.freeMem
  907. proc getTotalMem(): int =
  908. when hasThreadSupport and defined(gcDestructors):
  909. acquireSys(heapLock)
  910. result = sharedHeap.currMem
  911. releaseSys(heapLock)
  912. else:
  913. result = allocator.currMem
  914. proc getOccupiedMem(): int =
  915. when hasThreadSupport and defined(gcDestructors):
  916. acquireSys(heapLock)
  917. result = sharedHeap.occ
  918. releaseSys(heapLock)
  919. else:
  920. result = allocator.occ #getTotalMem() - getFreeMem()
  921. proc getMaxMem*(): int =
  922. when hasThreadSupport and defined(gcDestructors):
  923. acquireSys(heapLock)
  924. result = getMaxMem(sharedHeap)
  925. releaseSys(heapLock)
  926. else:
  927. result = getMaxMem(allocator)
  928. when defined(nimTypeNames):
  929. proc getMemCounters*(): (int, int) = getMemCounters(allocator)
  930. # -------------------- shared heap region ----------------------------------
  931. proc allocSharedImpl(size: Natural): pointer =
  932. when hasThreadSupport:
  933. acquireSys(heapLock)
  934. result = alloc(sharedHeap, size)
  935. releaseSys(heapLock)
  936. else:
  937. result = allocImpl(size)
  938. proc allocShared0Impl(size: Natural): pointer =
  939. result = allocSharedImpl(size)
  940. zeroMem(result, size)
  941. proc deallocSharedImpl(p: pointer) =
  942. when hasThreadSupport:
  943. acquireSys(heapLock)
  944. dealloc(sharedHeap, p)
  945. releaseSys(heapLock)
  946. else:
  947. deallocImpl(p)
  948. proc reallocSharedImpl(p: pointer, newSize: Natural): pointer =
  949. when hasThreadSupport:
  950. acquireSys(heapLock)
  951. result = realloc(sharedHeap, p, newSize)
  952. releaseSys(heapLock)
  953. else:
  954. result = reallocImpl(p, newSize)
  955. proc reallocShared0Impl(p: pointer, oldSize, newSize: Natural): pointer =
  956. when hasThreadSupport:
  957. acquireSys(heapLock)
  958. result = realloc0(sharedHeap, p, oldSize, newSize)
  959. releaseSys(heapLock)
  960. else:
  961. result = realloc0Impl(p, oldSize, newSize)
  962. when hasThreadSupport:
  963. template sharedMemStatsShared(v: int) =
  964. acquireSys(heapLock)
  965. result = v
  966. releaseSys(heapLock)
  967. proc getFreeSharedMem(): int =
  968. sharedMemStatsShared(sharedHeap.freeMem)
  969. proc getTotalSharedMem(): int =
  970. sharedMemStatsShared(sharedHeap.currMem)
  971. proc getOccupiedSharedMem(): int =
  972. sharedMemStatsShared(sharedHeap.occ)
  973. #sharedMemStatsShared(sharedHeap.currMem - sharedHeap.freeMem)
  974. {.pop.}
  975. {.pop.}