heap.inc 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. functions for heap management in the data segment
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. { Do not use standard memory manager }
  13. { $define HAS_MEMORYMANAGER}
  14. { Memory manager }
  15. const
  16. MemoryManager: TMemoryManager = (
  17. NeedLock: false; // Obsolete
  18. GetMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysGetMem{$else}nil{$endif};
  19. FreeMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysFreeMem{$else}nil{$endif};
  20. FreeMemSize: {$ifndef FPC_NO_DEFAULT_HEAP}@SysFreeMemSize{$else}nil{$endif};
  21. AllocMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysAllocMem{$else}nil{$endif};
  22. ReAllocMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysReAllocMem{$else}nil{$endif};
  23. MemSize: {$ifndef FPC_NO_DEFAULT_HEAP}@SysMemSize{$else}nil{$endif};
  24. InitThread: nil;
  25. DoneThread: nil;
  26. RelocateHeap: nil;
  27. GetHeapStatus: {$ifndef FPC_NO_DEFAULT_HEAP}@SysGetHeapStatus{$else}nil{$endif};
  28. GetFPCHeapStatus: {$ifndef FPC_NO_DEFAULT_HEAP}@SysGetFPCHeapStatus{$else}nil{$endif};
  29. ); {$ifdef FPC_NO_DEFAULT_HEAP} public name 'FPC_SYSTEM_MEMORYMANAGER'; {$endif}
  30. {*****************************************************************************
  31. Memory Manager
  32. *****************************************************************************}
  33. procedure GetMemoryManager(var MemMgr:TMemoryManager);
  34. begin
  35. MemMgr := MemoryManager;
  36. end;
  37. procedure SetMemoryManager(const MemMgr:TMemoryManager);
  38. begin
  39. MemoryManager := MemMgr;
  40. end;
  41. function IsMemoryManagerSet:Boolean;
  42. begin
  43. {$if defined(HAS_MEMORYMANAGER) or defined(FPC_NO_DEFAULT_HEAP)}
  44. Result:=false;
  45. {$else not FPC_NO_DEFAULT_HEAP}
  46. IsMemoryManagerSet := (MemoryManager.GetMem<>@SysGetMem)
  47. or (MemoryManager.FreeMem<>@SysFreeMem);
  48. {$endif HAS_MEMORYMANAGER or FPC_NO_DEFAULT_HEAP}
  49. end;
  50. {$ifdef FPC_HAS_FEATURE_HEAP}
  51. procedure GetMem(Out p:pointer;Size:ptruint);
  52. begin
  53. p := MemoryManager.GetMem(Size);
  54. end;
  55. procedure GetMemory(Out p:pointer;Size:ptruint);
  56. begin
  57. GetMem(p,size);
  58. end;
  59. procedure FreeMem(p:pointer;Size:ptruint);
  60. begin
  61. MemoryManager.FreeMemSize(p,Size);
  62. end;
  63. procedure FreeMemory(p:pointer;Size:ptruint);
  64. begin
  65. FreeMem(p,size);
  66. end;
  67. function GetHeapStatus:THeapStatus;
  68. begin
  69. Result:=MemoryManager.GetHeapStatus();
  70. end;
  71. function GetFPCHeapStatus:TFPCHeapStatus;
  72. begin
  73. Result:=MemoryManager.GetFPCHeapStatus();
  74. end;
  75. function MemSize(p:pointer):ptruint;
  76. begin
  77. MemSize := MemoryManager.MemSize(p);
  78. end;
  79. { Delphi style }
  80. function FreeMem(p:pointer):ptruint;
  81. begin
  82. FreeMem := MemoryManager.FreeMem(p);
  83. end;
  84. function FreeMemory(p:pointer):ptruint; cdecl;
  85. begin
  86. FreeMemory := FreeMem(p);
  87. end;
  88. function GetMem(size:ptruint):pointer;
  89. begin
  90. GetMem := MemoryManager.GetMem(Size);
  91. end;
  92. function GetMemory(size:ptruint):pointer; cdecl;
  93. begin
  94. GetMemory := GetMem(size);
  95. end;
  96. function AllocMem(Size:ptruint):pointer;
  97. begin
  98. AllocMem := MemoryManager.AllocMem(size);
  99. end;
  100. function ReAllocMem(var p:pointer;Size:ptruint):pointer;
  101. begin
  102. ReAllocMem := MemoryManager.ReAllocMem(p,size);
  103. end;
  104. function ReAllocMemory(p:pointer;Size:ptruint):pointer; cdecl;
  105. begin
  106. ReAllocMemory := ReAllocMem(p,size);
  107. end;
  108. { Needed for calls from Assembler }
  109. function fpc_getmem(size:ptruint):pointer;compilerproc;[public,alias:'FPC_GETMEM'];
  110. begin
  111. fpc_GetMem := MemoryManager.GetMem(size);
  112. end;
  113. procedure fpc_freemem(p:pointer);compilerproc;[public,alias:'FPC_FREEMEM'];
  114. begin
  115. MemoryManager.FreeMem(p);
  116. end;
  117. {$ifndef HAS_MEMORYMANAGER}
  118. type
  119. {
  120. We use 'fixed' size chunks for small allocations,
  121. os chunks with variable sized blocks for bigger allocations,
  122. and (almost) directly use os chunks for huge allocations.
  123. * a block is an area allocated by user
  124. * a chunk is a block plus our bookkeeping
  125. * an os chunk is a collection of chunks
  126. Memory layout:
  127. fixed: < CommonHeader > [ ... user data ... ]
  128. variable: [ VarHeader < CommonHeader > ] [ ... user data ... ]
  129. huge: HugeChunk < CommonHeader > [ ... user data ... ]
  130. When all chunks in an os chunk are free, we keep a few around
  131. but otherwise it will be freed to the OS.
  132. }
  133. {$ifdef ENDIAN_LITTLE}
  134. {$define HEAP_INC_USE_SETS} { Potentially better codegen than “or 1 shl” etc. (at least on x86). Can be adapted for big endian, too, but I have no such platform to test. }
  135. {$endif ENDIAN_LITTLE}
  136. HeapInc = object
  137. const
  138. { Alignment requirement for blocks. All fixed sizes (among other things) are assumed to be divisible. }
  139. Alignment = 2 * sizeof(pointer);
  140. { Fixed chunk sizes are:
  141. ┌──── step = 16 ────┐┌─── step = 32 ────┐┌──── step = 48 ───┐┌ step 64 ┐
  142. 16, 32, 48, 64, 80, 96, 128, 160, 192, 224, 272, 320, 368, 416, 480, 544
  143. #0 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 }
  144. MinFixedHeaderAndPayload = 16;
  145. MaxFixedHeaderAndPayload = 544;
  146. FixedSizesCount = 16;
  147. FixedSizes: array[0 .. FixedSizesCount - 1] of uint16 = (16, 32, 48, 64, 80, 96, 128, 160, 192, 224, 272, 320, 368, 416, 480, 544);
  148. SizeMinus1Div16ToIndex: array[0 .. (MaxFixedHeaderAndPayload - 1) div 16] of uint8 =
  149. { 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400, 416, 432, 448, 464, 480, 496, 512, 528, 544 }
  150. ( 0, 1, 2, 3, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15);
  151. class function SizeMinus1ToIndex(sizeMinus1: SizeUint): SizeUint; static; inline; { sizeMinus1 + 1 ≤ MaxFixedHeaderAndPayload }
  152. class function IndexToSize(sizeIndex: SizeUint): SizeUint; static; inline;
  153. const
  154. OSChunkVarSizeQuant = 64 * 1024;
  155. FixedArenaSizeQuant = 4 * 1024;
  156. MinFixedArenaSize = 8 * 1024;
  157. MaxFixedArenaSize = 64 * 1024;
  158. MaxKeptFixedArenas = 4;
  159. { Bin index that corresponds to the minimum size ChooseFixedArenaSize can return.
  160. = VarSizeToBinIndex(MinFixedArenaSize - MinFixedArenaSize shr (FirstVarRangeP2 - FirstVarStepP2), roundUp := true) or so.
  161. Pure functions could be useful to calculate these with less hardcoding... }
  162. MinArenaBinIndex = 96;
  163. { Maximum size that can be added by AllocVar(isArena := true) to the original request.
  164. BinIndexToVarSize(MinArenaBinIndex) - 1 is possibly the (minimal) ideal value because it guarantees that
  165. there will be no tail left unusable for futher arenas, but can be arbitrary. }
  166. MaxArenaOverallocation = 7968 - 1;
  167. { Adjustable part ends here~ }
  168. const
  169. SizeIndexBits = 1 + trunc(ln(FixedSizesCount - 1) / ln(2));
  170. SizeIndexMask = 1 shl SizeIndexBits - 1;
  171. FixedBitPos = {$if SizeIndexBits >= 4} SizeIndexBits {$else} 4 {$endif}; { Variable chunks use 4 low bits for used / last / prev. free / fixed arena. }
  172. FixedFlag = 1 shl FixedBitPos;
  173. FixedArenaOffsetShift = {$if FixedBitPos + 1 >= 5} FixedBitPos + 1 {$else} 5 {$endif}; { VarSizeQuant is expected to be 2^5. }
  174. UsedFlag = 1 shl 0;
  175. LastFlag = 1 shl 1;
  176. PrevIsFreeFlag = 1 shl 2;
  177. FixedArenaFlag = 1 shl 3;
  178. VarSizeQuant = 1 shl FixedArenaOffsetShift; {$if VarSizeQuant <> 32} {$error Should in principle work but explanations below assume exactly 32. :)} {$endif}
  179. VarSizeMask = uint32(-VarSizeQuant);
  180. HugeHeader = 0; { Special header value for huge chunks. FixedFlag must be 0, and the value must be impossible for a variable chunk. 0 turns out to be suitable. :) }
  181. { Variable chunk sizes, not counting extra MaxFixedHeaderAndPayload added to each of these:
  182. 32 sizes in the range +1 .. 1 024 (2^10) rounded up to the multiple of 32 = 2^ 5, + 0, max = 1 024 = %100 0000 0000
  183. 32 sizes in the range +1 .. 2 048 (2^11) rounded up to the multiple of 64 = 2^ 6, + 1024, max = 3 072 = %1100 0000 0000
  184. 32 sizes in the range +1 .. 4 096 (2^12) rounded up to the multiple of 128 = 2^ 7, + 1024 + 2048, max = 7 168 = %1 1100 0000 0000
  185. 32 sizes in the range +1 .. 8 192 (2^13) rounded up to the multiple of 256 = 2^ 8, + 2^10 + .. + 2^12, max = 15 360 = %11 1100 0000 0000
  186. 32 sizes in the range +1 .. 16 384 (2^14) rounded up to the multiple of 512 = 2^ 9, + 2^10 + .. + 2^13, max = 31 744 = %111 1100 0000 0000
  187. 32 sizes in the range +1 .. 32 768 (2^15) rounded up to the multiple of 1 024 = 2^10, + 2^10 + .. + 2^14, max = 64 512 = %1111 1100 0000 0000
  188. 32 sizes in the range +1 .. 65 536 (2^16) rounded up to the multiple of 2 048 = 2^11, + 2^10 + .. + 2^15, max = 130 048 = %1 1111 1100 0000 0000
  189. 32 sizes in the range +1 .. 131 072 (2^17) rounded up to the multiple of 4 096 = 2^12, + 2^10 + .. + 2^16, max = 261 120 = %11 1111 1100 0000 0000
  190. 32 sizes in the range +1 .. 262 144 (2^18) rounded up to the multiple of 8 192 = 2^13, + 2^10 + .. + 2^17, max = 523 264 = %111 1111 1100 0000 0000
  191. 32 sizes in the range +1 .. 524 288 (2^19) rounded up to the multiple of 16 384 = 2^14, + 2^10 + .. + 2^18, max = 1 047 552 = %1111 1111 1100 0000 0000 }
  192. FirstVarRangeP2 = 10;
  193. FirstVarStepP2 = FixedArenaOffsetShift; {$if FirstVarStepP2 <> 5} {$error :|} {$endif}
  194. VarSizeClassesCount = 10;
  195. VarSizesPerClass = 32;
  196. VarSizesCount = VarSizeClassesCount * VarSizesPerClass;
  197. L0BinSize = 32;
  198. { Minimum size of the chunk that can be added to varFree.
  199. Medium chunks can be smaller than this, all the way down to MinAnyVarHeaderAndPayload defined later in terms of things it must fit;
  200. they aren’t visible for varFree searches but are visible for merging with freed neighbors. }
  201. MinSearchableVarHeaderAndPayload = (MaxFixedHeaderAndPayload + 1 shl FirstVarStepP2 + VarSizeQuant - 1) and -VarSizeQuant;
  202. MaxVarHeaderAndPayload = (MaxFixedHeaderAndPayload + (1 shl VarSizeClassesCount - 1) shl FirstVarRangeP2) and -VarSizeQuant; {$if MaxVarHeaderAndPayload <> MaxFixedHeaderAndPayload + 1047552} {$error does not match the explanation above :D} {$endif}
  203. class function VarSizeToBinIndex(size: SizeUint; roundUp: boolean): SizeUint; static; inline; { roundUp is constant. }
  204. class function BinIndexToVarSize(binIndex: SizeUint): SizeUint; static; inline;
  205. type
  206. { Common header of any memory chunk, residing immediately to the left of the ~payload~ (block).
  207. Fixed chunk header, assuming SizeIndexBits = 4:
  208. h[0:3] = size index (= h and SizeIndexMask)
  209. h[4] = 1 (h and FixedFlag <> 0)
  210. h[5:31] — offset in the FixedArena (= h shr FixedArenaOffsetShift)
  211. Variable chunk header, assuming SizeIndexBits = 4:
  212. h[0] = used flag (h and UsedFlag <> 0)
  213. h[1] = last flag (h and LastFlag <> 0)
  214. h[2] = previous is free flag (h and PrevIsFreeFlag <> 0)
  215. h[3] = fixed arena flag (h and FixedArenaFlag <> 0)
  216. h[4] = 0 (h and FixedFlag = 0)
  217. h[5:31] = size, rounded up to 32 (VarSizeQuant), shr 5; in other words, size = h and VarSizeMask.
  218. Huge chunk header:
  219. h[4] = 0 (h and FixedFlag = 0)
  220. h[0:31] = HugeHeader }
  221. pCommonHeader = ^CommonHeader;
  222. CommonHeader = record
  223. h: uint32;
  224. end;
  225. pThreadState = ^ThreadState;
  226. { Chunk that has been freed. Reuses the now-uninteresting payload, so payload must always fit its size.
  227. Used for fixed freelists and cross-thread to-free queue. }
  228. pFreeChunk = ^FreeChunk;
  229. FreeChunk = record
  230. next: pFreeChunk;
  231. end;
  232. OSChunkBase = object { Shared between OSChunk and HugeChunk. }
  233. size: SizeUint; { Full size asked from SysOSAlloc. }
  234. end;
  235. pOSChunk = ^OSChunk;
  236. OSChunk = object(OSChunkBase) { Common header for all OS chunks. }
  237. prev, next: pointer; { pOSChunk, but used for different subtypes. }
  238. end;
  239. pFreeOSChunk = ^FreeOSChunk;
  240. FreeOSChunk = object(OSChunk)
  241. end;
  242. {$ifndef HAS_SYSOSFREE}
  243. FreeOSChunkList = object
  244. first, last: pFreeOSChunk;
  245. function Get(minSize, maxSize: SizeUint): pOSChunk;
  246. end;
  247. {$endif not HAS_SYSOSFREE}
  248. pFixedArena = ^FixedArena;
  249. FixedArena = record
  250. { Allocated with AllocVar(isArena := true), so has VarHeader to the left.
  251. Data starts at FixedArenaDataOffset and spans for “maxSize” (virtual value, does not exist directly) bytes, of which:
  252. — first “formattedSize” are either allocated (“used”; counted in usedSizeMinus1) or in the freelist (firstFreeChunk; size = “formattedSize” - (usedSizeMinus1 + 1)),
  253. — the rest “maxSize” - “formattedSize” are yet unallocated space.
  254. This design, together with tracking free chunks per FixedArena rather than per fixed size, trivializes reusing the fixed arenas.
  255. Chopping all available space at once would get rid of the “unallocated space” entity, but is a lot of potentially wasted work:
  256. https://gitlab.com/freepascal.org/fpc/source/-/issues/40447.
  257. Values are multiples of the chunk size instead of counts (could be chunksUsed, chunksFormatted, chunksMax) to save on multiplications.
  258. Moreover, instead of “maxSize” from the explanation above, almostFullThreshold is used, which is such a value that the chunk is full if usedSizeMinus1 - chunk size >= almostFullThreshold.
  259. maxSize = RoundUp(almostFullThreshold + chunk size + 1, chunk size).
  260. Reasons are, calculating almostFullThreshold does not require division, and it is more convenient (in terms of code generation) for AllocFixed / FreeFixed.
  261. “formattedSize” is a virtual value, too; it equals to usedSizeMinus1 + 1 + <total size of the freelist> and is used only when said freelist is empty, so is in practice int32(usedSizeMinus1) + 1 (see AllocFixed). }
  262. firstFreeChunk: pFreeChunk;
  263. usedSizeMinus1, almostFullThreshold: uint32;
  264. prev, next: pFixedArena;
  265. end;
  266. pVarOSChunk = ^VarOSChunk;
  267. VarOSChunk = object(OSChunk)
  268. {$ifdef FPC_HAS_FEATURE_THREADING}
  269. threadState: pThreadState; { Protected with gs.lock. Nil if orphaned. }
  270. {$endif}
  271. end;
  272. pVarHeader = ^VarHeader;
  273. VarHeader = record
  274. { Negative offset from the end of this VarHeader to owning VarOSChunk, friendlier to x86 LEA instruction than the more obvious positive variant.
  275. Truly required only under FPC_HAS_FEATURE_THREADING and could be removed otherwise, bringing the variable header size to the same 4 bytes as fixed headers,
  276. but this would require some redesign (reintroducing FirstFlag removed in https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/1027
  277. or some other way to detect the first chunk) and does not matter enough to bother.
  278. Moreover, accessing VarOSChunk could have been useful beyond multithreading, it just so happens it isn’t. }
  279. ofsToOs: int32;
  280. { Assumed to indeed match chunk’s CommonHeader, i.e. that there is no padding after this field.
  281. Otherwise must be accessed as pCommonHeader(pointer(varHdr) + (VarHeaderSize - CommonHeaderSize))^ :D. }
  282. ch: CommonHeader;
  283. end;
  284. { Reuses the payload of variable chunks whose ch.h and UsedFlag = 0, so variable chunk payload must always fit its size. }
  285. pFreeVarChunk = ^FreeVarChunk;
  286. FreeVarChunk = record
  287. prev, next: pFreeVarChunk;
  288. binIndex: uint32;
  289. end;
  290. { Placed at the end of the free variable chunks that have occupied chunks to the right, thus immediately to the left of such an occupied chunk. }
  291. pFreeVarTail = ^FreeVarTail;
  292. FreeVarTail = record
  293. size: uint32;
  294. end;
  295. pHugeChunk = ^HugeChunk;
  296. HugeChunk = object(OSChunkBase)
  297. end;
  298. {$ifdef HEAP_INC_USE_SETS}
  299. Set32 = set of 0 .. 31;
  300. {$endif HEAP_INC_USE_SETS}
  301. VarFreeMap = object
  302. { Two-level bitfield that allows to search for minimal-size fits (up to the quantization) using up to two “Bsf”s.
  303. Bit 1 in L1 means that the corresponding cell of L0 is non-0.
  304. Bit 1 in L0 means that the corresponding cell of bins is non-nil. }
  305. L1: uint32;
  306. L0: array[0 .. (VarSizesCount + L0BinSize - 1) div L0BinSize - 1] of uint32;
  307. bins: array[0 .. VarSizesCount - 1] of pFreeVarChunk;
  308. { As an optimization, Add.binIndex can also be a size (will be rounded down), assuming VarSizesCount <= MinSearchableVarHeaderAndPayload. }
  309. {$if VarSizesCount > MinSearchableVarHeaderAndPayload} {$error assumption above does not hold} {$endif}
  310. procedure Add(c: pFreeVarChunk; binIndex: SizeUint);
  311. procedure Remove(c: pFreeVarChunk);
  312. function Find(binIndex: SizeUint): pFreeVarChunk;
  313. function FindSmaller(binIndex: SizeUint): pFreeVarChunk;
  314. end;
  315. ThreadState = object
  316. emptyArenas: pFixedArena; { Empty fixed arenas to be reused instead of slower AllocVar. Singly linked list, “prev”s are garbage. }
  317. nEmptyArenas: SizeUint; { # of items in emptyArenas. }
  318. {$ifdef HAS_SYSOSFREE}
  319. freeOS1: pFreeOSChunk; { Just one cached empty OS chunk so that borderline (free + alloc) × N scenarios don’t lead to N OS allocations. }
  320. {$else HAS_SYSOSFREE}
  321. freeOS: FreeOSChunkList; { Completely empty OS chunks. }
  322. {$endif HAS_SYSOSFREE}
  323. {$ifdef FPC_HAS_FEATURE_THREADING}
  324. toFree: pFreeChunk; { Free requests from other threads, atomic. }
  325. {$endif}
  326. used, maxUsed, allocated, maxAllocated: SizeUint; { “maxUsed” and “maxAllocated” include gs.hugeUsed; “used” and “allocated” don’t. }
  327. varOS: pVarOSChunk;
  328. { Fixed arenas with at least 1 free chunk (including unformatted space), but not completely empty.
  329. Fixed arenas that become completely empty are moved to emptyArenas, completely full are... not present in any list. }
  330. partialArenas: array[0 .. FixedSizesCount - 1] of pFixedArena;
  331. { Only to calculate preferable new fixed arena sizes...
  332. (Updated infrequently, as opposed to possible “usedPerArena”. When a new arena is required, all existing arenas of its size are full.) }
  333. allocatedByFullArenas: array[0 .. FixedSizesCount - 1] of uint32; { SizeUint is not obligatory, overflow is tolerable. }
  334. varFree: VarFreeMap;
  335. {$if defined(SUPPORT_INIT_HEAP_PROCESS_WIDE) and defined(HAS_SYSOSFREE)}
  336. prev, next: pThreadState; { For gs.threads. }
  337. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE and HAS_SYSOSFREE}
  338. {$ifdef DEBUG_HEAP_INC}
  339. procedure Dump(var f: text);
  340. {$endif}
  341. function ChooseFixedArenaSize(sizeIndex: SizeUint): SizeUint;
  342. function AllocFixed(size: SizeUint): pointer; {$ifndef DEBUG_HEAP_INC} inline; {$endif}
  343. function FreeFixed(p: pointer): SizeUint; {$ifndef DEBUG_HEAP_INC} inline; {$endif}
  344. procedure FreeEmptyArenas;
  345. {$ifdef HAS_SYSOSFREE}
  346. procedure ReplaceFreeOS1(&with: pFreeOSChunk); inline;
  347. {$endif HAS_SYSOSFREE}
  348. function GetOSChunk(minSize, maxSize: SizeUint): pOSChunk; {$if defined(HAS_SYSOSFREE) or not defined(FPC_HAS_FEATURE_THREADING)} inline; {$endif}
  349. function AllocateOSChunk(minSize, maxSize: SizeUint): pOSChunk;
  350. function AllocVar(size: SizeUint; isArena: boolean): pointer;
  351. function FreeVar(p: pointer): SizeUint;
  352. function TryResizeVar(p: pointer; size: SizeUint): pointer;
  353. class function AddToHugeUsed(delta: SizeInt): SizeUint; static;
  354. function AllocHuge(size: SizeUint): pointer;
  355. function FreeHuge(p: pointer): SizeUint;
  356. function TryResizeHuge(p: pointer; size: SizeUint): pointer;
  357. procedure UpdateMaxStats(hugeUsed: SizeUint);
  358. {$ifdef FPC_HAS_FEATURE_THREADING}
  359. procedure PushToFree(p: pFreeChunk);
  360. procedure FlushToFree;
  361. class procedure FreeToFreeList(tf: pFreeChunk); static;
  362. procedure Orphan;
  363. procedure AdoptArena(arena: pFixedArena);
  364. procedure AdoptVarOwner(p: pointer); { Adopts the OS chunk that contains p. Must be performed under gs.lock. }
  365. {$ifndef FPC_SECTION_THREADVARS}
  366. procedure FixupSelfPtr;
  367. {$endif ndef FPC_SECTION_THREADVARS}
  368. {$endif FPC_HAS_FEATURE_THREADING}
  369. end;
  370. GlobalState = object
  371. const
  372. LockInitializedProcessWide = -1; { Special lockUse value to support DLL_PROCESS_ATTACH / DLL_PROCESS_DETACH-like mechanism. }
  373. var
  374. hugeUsed: SizeUint; { Same as non-existing “hugeAllocated” as huge chunks don’t have free space.
  375. Atomic, but can be read unprotected if unreliability is tolerable.
  376. Huge chunks don’t have thread affinity, so are tracked here. Presently, this value is added to all memory statistics.
  377. Not a good idea and makes multithreaded statistics a strange and unreliable mix, but alternatives are even worse. }
  378. {$ifdef FPC_HAS_FEATURE_THREADING}
  379. lock: TRTLCriticalSection;
  380. lockUse: int32;
  381. {$ifdef SUPPORT_INIT_HEAP_PROCESS_WIDE}
  382. askedForProcessWideLockInitialization: boolean;
  383. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE}
  384. { Like ThreadState.varFree but over orphaned OS chunks. Protected by gs.lock. }
  385. varFree: VarFreeMap;
  386. {$if not defined(HAS_SYSOSFREE)}
  387. freeOS: FreeOSChunkList;
  388. {$elseif defined(SUPPORT_INIT_HEAP_PROCESS_WIDE)}
  389. threads: pThreadState;
  390. {$endif not HAS_SYSOSFREE | SUPPORT_INIT_HEAP_PROCESS_WIDE}
  391. {$endif FPC_HAS_FEATURE_THREADING}
  392. end;
  393. class function AllocFailed: pointer; static;
  394. class var
  395. gs: GlobalState;
  396. {$ifdef FPC_HAS_FEATURE_THREADING}
  397. class threadvar
  398. {$endif FPC_HAS_FEATURE_THREADING}
  399. thisTs: ThreadState;
  400. const
  401. CommonHeaderSize = sizeof(CommonHeader);
  402. {$if MinFixedHeaderAndPayload < CommonHeaderSize + sizeof(FreeChunk)} {$error MinFixedHeaderAndPayload does not fit CommonHeader + FreeChunk.} {$endif}
  403. FixedArenaDataOffset = (sizeof(FixedArena) + CommonHeaderSize + Alignment - 1) and -Alignment - CommonHeaderSize;
  404. VarHeaderSize = sizeof(VarHeader);
  405. FreeVarTailSize = sizeof(FreeVarTail);
  406. VarOSChunkDataOffset = (sizeof(VarOSChunk) + VarHeaderSize + Alignment - 1) and -Alignment - VarHeaderSize;
  407. HugeChunkDataOffset = (sizeof(HugeChunk) + CommonHeaderSize + Alignment - 1) and -Alignment - CommonHeaderSize;
  408. MinAnyVarHeaderAndPayload = (sizeof(VarHeader) + sizeof(FreeVarChunk) + sizeof(FreeVarTail) + VarSizeQuant - 1) and -VarSizeQuant;
  409. end;
  410. class function HeapInc.SizeMinus1ToIndex(sizeMinus1: SizeUint): SizeUint;
  411. begin
  412. result := SizeMinus1Div16ToIndex[sizeMinus1 div 16];
  413. end;
  414. class function HeapInc.IndexToSize(sizeIndex: SizeUint): SizeUint;
  415. begin
  416. result := FixedSizes[sizeIndex];
  417. end;
  418. class function HeapInc.VarSizeToBinIndex(size: SizeUint; roundUp: boolean): SizeUint;
  419. var
  420. binClassIndex: SizeUint;
  421. begin
  422. { Honestly can’t explain the code, it’s made from and equivalent to https://gitlab.com/freepascal.org/fpc/source/-/blob/6ed0a74f54327e07f33e66ddcc4fb5eb8d808dd8/rtl/inc/heap.inc#L503.
  423. Basic idea is that size class breakpoints of the form %11..1100_0000_0000 are converted to %11..11 for simple BSR by adding %11_1111_1111 = 1 shl FirstVarRangeP2 - 1,
  424. but the rest, in particular applying roundUp, is magic that is somehow equivalent to the original but uses 1/3 of its instructions. }
  425. inc(size, 1 shl FirstVarRangeP2 - MaxFixedHeaderAndPayload - ord(roundUp));
  426. binClassIndex := BsrDWord(uint32(size) or 1); { Off by +FirstVarRangeP2. “or 1” is not required logically, just triggers node_not_zero optimization. }
  427. result := binClassIndex * VarSizesPerClass + size shr (binClassIndex - (FirstVarRangeP2 - FirstVarStepP2)) - (FirstVarRangeP2 * VarSizesPerClass + 1 shl (FirstVarRangeP2 - FirstVarStepP2) + ord(not roundUp));
  428. end;
  429. class function HeapInc.BinIndexToVarSize(binIndex: SizeUint): SizeUint;
  430. begin
  431. { Same. }
  432. result := (1 shl (FirstVarRangeP2 - FirstVarStepP2) + 1 + binIndex mod VarSizesPerClass) shl (binIndex div VarSizesPerClass + FirstVarStepP2) - (1 shl FirstVarRangeP2 - MaxFixedHeaderAndPayload);
  433. end;
  434. {$ifndef HAS_SYSOSFREE}
  435. function HeapInc.FreeOSChunkList.Get(minSize, maxSize: SizeUint): pOSChunk;
  436. var
  437. prev, next: pFreeOSChunk;
  438. begin
  439. result := first;
  440. while Assigned(result) and not ((result^.size >= minSize) and (result^.size <= maxSize)) do
  441. result := result^.next;
  442. if not Assigned(result) then
  443. exit;
  444. prev := result^.prev;
  445. next := result^.next;
  446. if Assigned(prev) then
  447. prev^.next := next
  448. else
  449. first := next;
  450. if Assigned(next) then
  451. next^.prev := prev
  452. else
  453. last := prev;
  454. end;
  455. {$endif not HAS_SYSOSFREE}
  456. procedure HeapInc.VarFreeMap.Add(c: pFreeVarChunk; binIndex: SizeUint);
  457. var
  458. next: pFreeVarChunk;
  459. iL0: SizeUint;
  460. vL0 {$ifdef HEAP_INC_USE_SETS}, vL1 {$endif}: uint32;
  461. begin
  462. if binIndex >= VarSizesCount then
  463. if binIndex >= MaxVarHeaderAndPayload then { Large sizes go to the last bin, assuming searches never search for more than MaxVarHeaderAndPayload. }
  464. binIndex := VarSizesCount - 1
  465. else
  466. binIndex := VarSizeToBinIndex(binIndex, false);
  467. next := bins[binIndex];
  468. c^.prev := nil;
  469. c^.next := next;
  470. c^.binIndex := binIndex;
  471. bins[binIndex] := c;
  472. if Assigned(next) then
  473. next^.prev := c
  474. else
  475. begin
  476. iL0 := binIndex div L0BinSize;
  477. vL0 := L0[iL0];
  478. {$ifdef HEAP_INC_USE_SETS}
  479. if vL0 = 0 then
  480. begin
  481. vL1 := L1;
  482. Include(Set32(vL1), iL0);
  483. L1 := vL1;
  484. end;
  485. Include(Set32(vL0), binIndex mod L0BinSize);
  486. L0[iL0] := vL0;
  487. {$else}
  488. if vL0 = 0 then
  489. L1 := L1 or uint32(1) shl iL0;
  490. L0[iL0] := vL0 or uint32(1) shl (binIndex mod L0BinSize);
  491. {$endif}
  492. end;
  493. end;
  494. procedure HeapInc.VarFreeMap.Remove(c: pFreeVarChunk);
  495. var
  496. prev, next: pFreeVarChunk;
  497. binIndex, iL0: SizeUint;
  498. v: uint32;
  499. begin
  500. prev := c^.prev;
  501. next := c^.next;
  502. if Assigned(next) then
  503. next^.prev := prev;
  504. if Assigned(prev) then
  505. prev^.next := next
  506. else
  507. begin
  508. binIndex := c^.binIndex;
  509. bins[binIndex] := next;
  510. if not Assigned(next) then
  511. begin
  512. iL0 := binIndex div L0BinSize;
  513. {$ifdef HEAP_INC_USE_SETS}
  514. v := L0[iL0];
  515. Exclude(Set32(v), binIndex mod L0BinSize);
  516. L0[iL0] := v;
  517. if v = 0 then
  518. begin
  519. v := L1;
  520. Exclude(Set32(v), iL0);
  521. L1 := v;
  522. end;
  523. {$else}
  524. v := L0[iL0] xor (uint32(1) shl (binIndex mod L0BinSize));
  525. L0[iL0] := v;
  526. if v = 0 then
  527. L1 := L1 xor (uint32(1) shl iL0);
  528. {$endif}
  529. end;
  530. end;
  531. end;
  532. function HeapInc.VarFreeMap.Find(binIndex: SizeUint): pFreeVarChunk;
  533. var
  534. mask: uint32;
  535. begin
  536. result := bins[binIndex];
  537. if Assigned(result) then
  538. exit;
  539. mask := L0[binIndex div L0BinSize] shr (binIndex mod L0BinSize); { Logically should be “1 + binIndex mod L0BinSize” but the bit that represents the binIndex-th bin is 0 anyway. }
  540. if mask <> 0 then
  541. exit(bins[binIndex + BsfDWord(mask or 1 shl (L0BinSize - 1))]); { In these two BsfDWords, ensuring the highest bit to be set is just an optimization, as long as the supposed alternative from https://gitlab.com/freepascal.org/fpc/source/-/issues/41179 does not work. }
  542. mask := L1 and (SizeUint(-2) shl (binIndex div L0BinSize));
  543. if mask <> 0 then
  544. begin
  545. binIndex := BsfDWord(mask or 1 shl (L0BinSize - 1)); { Index at L0. }
  546. result := bins[binIndex * L0BinSize + BsfDWord(L0[binIndex] or 1 shl (L0BinSize - 1))]; { Careful, this time “or 1 shl (L0BinSize - 1)” is NOT an optimization: unsynchronized gs.varFree.Find can read zero from L0[binIndex]. }
  547. end;
  548. end;
  549. function HeapInc.VarFreeMap.FindSmaller(binIndex: SizeUint): pFreeVarChunk;
  550. var
  551. mask: uint32;
  552. begin
  553. mask := L0[binIndex div L0BinSize] and (uint32(1) shl (binIndex mod L0BinSize) - 1);
  554. if mask <> 0 then
  555. exit(bins[binIndex and SizeUint(-L0BinSize) + BsrDWord(mask or 1)]);
  556. result := nil;
  557. mask := L1 and (uint32(1) shl (binIndex div L0BinSize) - 1);
  558. if mask <> 0 then
  559. begin
  560. binIndex := BsrDWord(mask or 1);
  561. result := bins[binIndex * L0BinSize + BsrDWord(L0[binIndex] or 1)];
  562. end;
  563. end;
  564. {$ifdef DEBUG_HEAP_INC}
  565. procedure HeapInc.ThreadState.Dump(var f: text);
  566. var
  567. i: SizeInt;
  568. fix: pFixedArena;
  569. fr: pFreeOSChunk;
  570. {$ifdef FPC_HAS_FEATURE_THREADING}
  571. tf: pFreeChunk;
  572. {$endif}
  573. vf: pFreeVarChunk;
  574. vOs: pVarOSChunk;
  575. p: pointer;
  576. needLE, anything: boolean;
  577. procedure MaybeLE;
  578. begin
  579. if needLE then
  580. writeln(f);
  581. needLE := false;
  582. end;
  583. procedure DumpVarFree(const varFree: VarFreeMap; const name: shortstring);
  584. var
  585. i: SizeInt;
  586. begin
  587. if varFree.L1 = 0 then
  588. exit;
  589. MaybeLE;
  590. write(f, name, LineEnding, 'L1:');
  591. for i := 0 to VarSizesCount div L0BinSize - 1 do
  592. if varFree.L1 shr i and 1 <> 0 then
  593. begin
  594. write(f, ' #', i, ' ', BinIndexToVarSize(i * L0BinSize), '-');
  595. if i = VarSizesCount div L0BinSize - 1 then
  596. write(f, 'inf')
  597. else
  598. write(f, BinIndexToVarSize((i + 1) * L0BinSize) - 1);
  599. end;
  600. writeln(f);
  601. write(f, 'L0 (bins):');
  602. for i := 0 to VarSizesCount - 1 do
  603. begin
  604. if varFree.L0[SizeUint(i) div L0BinSize] shr (SizeUint(i) mod L0BinSize) and 1 <> 0 then
  605. begin
  606. write(f, ' #', i, ' ', BinIndexToVarSize(i), '-');
  607. if i = VarSizesCount - 1 then
  608. write(f, 'inf')
  609. else
  610. write(f, BinIndexToVarSize(i + 1) - 1);
  611. end;
  612. if Assigned(varFree.bins[i]) then
  613. begin
  614. write(f, ' (');
  615. vf := varFree.bins[i];
  616. repeat
  617. if Assigned(vf^.prev) then write(f, ' ');
  618. write(f, HexStr(PtrUint(vf), 1 + BsrQWord(PtrUint(vf)) div 4), ':', pVarHeader(vf)[-1].ch.h and VarSizeMask);
  619. vf := vf^.next;
  620. until not Assigned(vf);
  621. write(f, ')');
  622. end;
  623. end;
  624. writeln(f);
  625. needLE := true;
  626. end;
  627. begin
  628. writeln(f, 'used = ', used, ', allocated = ', allocated, ', hugeUsed = ', gs.hugeUsed, ', maxUsed = ', maxUsed, ', maxAllocated = ', maxAllocated);
  629. needLE := true;
  630. anything := false;
  631. for i := 0 to FixedSizesCount - 1 do
  632. begin
  633. if not Assigned(partialArenas[i]) and (allocatedByFullArenas[i] = 0) then
  634. continue;
  635. MaybeLE;
  636. anything := true;
  637. write(f, 'Size #', i, ' (', IndexToSize(i), '):');
  638. if allocatedByFullArenas[i] <> 0 then
  639. write(f, ' allocatedByFullArenas = ', allocatedByFullArenas[i]);
  640. if Assigned(partialArenas[i]) then
  641. begin
  642. writeln(f);
  643. fix := partialArenas[i];
  644. repeat
  645. writeln(f, 'arena size = ', pVarHeader(fix)[-1].ch.h and VarSizeMask - VarHeaderSize - FixedArenaDataOffset, ', usedSizeMinus1 = ', fix^.usedSizeMinus1, ', almostFullThreshold = ', fix^.almostFullThreshold);
  646. fix := fix^.next;
  647. until not Assigned(fix);
  648. end
  649. else if allocatedByFullArenas[i] <> 0 then
  650. writeln(f);
  651. end;
  652. needLE := needLE or anything;
  653. if nEmptyArenas <> 0 then
  654. begin
  655. MaybeLE;
  656. writeln(f, 'nEmptyArenas = ', nEmptyArenas);
  657. needLE := true;
  658. end;
  659. vOs := varOS;
  660. while Assigned(vOs) do
  661. begin
  662. MaybeLE;
  663. writeln(f, 'Var OS chunk, size ', vOs^.size);
  664. p := pointer(vOs) + (VarOSChunkDataOffset + VarHeaderSize);
  665. repeat
  666. write(f, HexStr(p), ': size = ', pVarHeader(p - VarHeaderSize)^.ch.h and VarSizeMask, ', ofsToOs = ', pVarHeader(p - VarHeaderSize)^.ofsToOs);
  667. if pVarHeader(p - VarHeaderSize)^.ch.h and UsedFlag <> 0 then
  668. write(f, ', used')
  669. else
  670. begin
  671. write(f, ', f r e e');
  672. if pVarHeader(p - VarHeaderSize)^.ch.h and LastFlag = 0 then
  673. write(f, ' (tail ', pFreeVarTail(p + pVarHeader(p - VarHeaderSize)^.ch.h - VarHeaderSize - FreeVarTailSize)^.size, ')');
  674. end;
  675. if pVarHeader(p - VarHeaderSize)^.ch.h and LastFlag <> 0 then
  676. write(f, ', last');
  677. if pVarHeader(p - VarHeaderSize)^.ch.h and PrevIsFreeFlag <> 0 then
  678. write(f, ', prev. is free');
  679. if pVarHeader(p - VarHeaderSize)^.ch.h and FixedArenaFlag <> 0 then
  680. write(f, ', fixed arena');
  681. writeln(f);
  682. if pVarHeader(p - VarHeaderSize)^.ch.h and LastFlag <> 0 then
  683. break;
  684. p := p + pVarHeader(p - VarHeaderSize)^.ch.h and VarSizeMask;
  685. until false;
  686. needLE := true;
  687. vOs := vOs^.next;
  688. end;
  689. fr := {$ifdef HAS_SYSOSFREE} freeOS1 {$else} freeOS.first {$endif};
  690. if Assigned(fr) then
  691. begin
  692. MaybeLE;
  693. repeat
  694. writeln(f, 'Free OS: ', HexStr(fr), ', size = ', fr^.size);
  695. {$ifndef HAS_SYSOSFREE} fr := fr^.next; {$endif}
  696. until {$ifdef HAS_SYSOSFREE} true {$else} not Assigned(fr) {$endif};
  697. needLE := true;
  698. end;
  699. DumpVarFree(varFree, 'varFree');
  700. {$ifdef FPC_HAS_FEATURE_THREADING}
  701. DumpVarFree(gs.varFree, 'Orphaned varFree');
  702. tf := toFree;
  703. if Assigned(tf) then
  704. begin
  705. MaybeLE;
  706. write(f, 'To-free:');
  707. repeat
  708. if pCommonHeader(pointer(tf) - CommonHeaderSize)^.h and FixedFlag <> 0 then
  709. write(f, ' f ', CommonHeaderSize + SysMemSize(tf))
  710. else
  711. write(f, ' v ', VarHeaderSize + SysMemSize(tf));
  712. tf := tf^.next;
  713. until not Assigned(tf);
  714. writeln(f);
  715. end;
  716. {$endif FPC_HAS_FEATURE_THREADING}
  717. end;
  718. {$endif DEBUG_HEAP_INC}
  719. function HeapInc.ThreadState.ChooseFixedArenaSize(sizeIndex: SizeUint): SizeUint;
  720. begin
  721. result := (allocatedByFullArenas[sizeIndex] div 8 + (FixedArenaSizeQuant - 1)) and SizeUint(-FixedArenaSizeQuant); { 12.5% of memory allocated by the size. }
  722. if result < MinFixedArenaSize then
  723. result := MinFixedArenaSize;
  724. if result > MaxFixedArenaSize then
  725. result := MaxFixedArenaSize;
  726. dec(result, result shr (FirstVarRangeP2 - FirstVarStepP2)); { Prettier fit into OS chunks. }
  727. end;
  728. function HeapInc.ThreadState.AllocFixed(size: SizeUint): pointer;
  729. var
  730. sizeIndex, sizeUp, statv: SizeUint;
  731. usedSizeMinus1: int32;
  732. arena, nextArena: pFixedArena;
  733. begin
  734. sizeIndex := SizeMinus1ToIndex(size + (CommonHeaderSize - 1));
  735. arena := partialArenas[sizeIndex];
  736. if not Assigned(arena) then
  737. begin
  738. {$ifdef FPC_HAS_FEATURE_THREADING}
  739. if Assigned(toFree) then
  740. begin
  741. FlushToFree;
  742. arena := partialArenas[sizeIndex];
  743. end;
  744. if not Assigned(arena) then
  745. {$endif FPC_HAS_FEATURE_THREADING}
  746. begin
  747. arena := emptyArenas;
  748. if Assigned(arena) then
  749. begin
  750. emptyArenas := arena^.next;
  751. dec(nEmptyArenas);
  752. end else
  753. begin
  754. arena := AllocVar(ChooseFixedArenaSize(sizeIndex), true);
  755. if not Assigned(arena) then
  756. exit(nil);
  757. { Size index of the first chunk in the arena is used to determine if it can be reused. Set a purposely mismatching value for freshly allocated arena. }
  758. pCommonHeader(pointer(arena) + FixedArenaDataOffset)^.h := uint32(not sizeIndex);
  759. end;
  760. if pCommonHeader(pointer(arena) + FixedArenaDataOffset)^.h and SizeIndexMask = uint32(sizeIndex) then
  761. { Lucky! Just don’t reset the chunk and use its old freelist. }
  762. else
  763. begin
  764. arena^.firstFreeChunk := nil;
  765. arena^.usedSizeMinus1 := uint32(-1);
  766. arena^.almostFullThreshold := pVarHeader(arena)[-1].ch.h and VarSizeMask - 2 * IndexToSize(sizeIndex) - (VarHeaderSize + FixedArenaDataOffset); { available space - 2 * chunk size. }
  767. end;
  768. { Add arena to partialArenas[sizeIndex], which is nil. Careful: AllocVar above should not call FlushToFree, or this assumption might be violated. }
  769. arena^.prev := nil;
  770. arena^.next := nil;
  771. partialArenas[sizeIndex] := arena;
  772. end;
  773. end;
  774. sizeUp := IndexToSize(sizeIndex); { Not reusing the “size” variable saved a register at the time of writing this comment. }
  775. inc(used, sizeUp);
  776. { arena from partialArenas has either free chunk or free unformatted space for a new chunk. }
  777. usedSizeMinus1 := int32(arena^.usedSizeMinus1);
  778. arena^.usedSizeMinus1 := uint32(usedSizeMinus1 + int32(sizeUp));
  779. result := arena^.firstFreeChunk;
  780. if Assigned(result) then
  781. begin
  782. { This branch is much more likely (when compiling FPC: 9×), so comes first. }
  783. arena^.firstFreeChunk := pFreeChunk(result)^.next;
  784. if usedSizeMinus1 < int32(arena^.almostFullThreshold) then { Arena is still not full? Uses usedSizeMinus1 value before adding sizeUp, as assumed by almostFullThreshold. }
  785. exit;
  786. end else
  787. begin
  788. { Freelist is empty, so “formattedSize” = usedSizeMinus1 + 1. This “+ 1” is folded into constants. }
  789. result := pointer(arena) + (FixedArenaDataOffset + CommonHeaderSize + 1) + usedSizeMinus1;
  790. pCommonHeader(result - CommonHeadersize)^.h := uint32(int32(sizeIndex) + int32(usedSizeMinus1 shl FixedArenaOffsetShift) +
  791. (FixedFlag + (FixedArenaDataOffset + CommonHeaderSize + 1) shl FixedArenaOffsetShift) { ← const });
  792. if usedSizeMinus1 < int32(arena^.almostFullThreshold) then { Arena is still not full? }
  793. exit;
  794. end;
  795. { Arena became full. This is unlikely, so instead of the “if”, the check is duplicated in both branches above. (Saves a jump from the “then” branch above.) }
  796. inc(allocatedByFullArenas[sizeIndex], pVarHeader(arena)[-1].ch.h and VarSizeMask);
  797. { Remove arena from partialArenas[sizeIndex]. (It was first.) }
  798. nextArena := arena^.next;
  799. partialArenas[sizeIndex] := nextArena;
  800. if Assigned(nextArena) then
  801. nextArena^.prev := nil;
  802. { And since this is unlikely, it won’t hurt to update maxUsed (unlike doing it in the common path). }
  803. statv := used + gs.hugeUsed;
  804. if statv > maxUsed then
  805. maxUsed := statv;
  806. end;
  807. function HeapInc.ThreadState.FreeFixed(p: pointer): SizeUint;
  808. var
  809. sizeIndex: SizeUint;
  810. usedSizeMinus1: int32;
  811. arena, prevArena, nextArena: pFixedArena;
  812. {$ifdef FPC_HAS_FEATURE_THREADING}
  813. ts: pThreadState;
  814. {$endif FPC_HAS_FEATURE_THREADING}
  815. begin
  816. arena := p - pCommonHeader(p - CommonHeaderSize)^.h shr FixedArenaOffsetShift;
  817. {$ifdef FPC_HAS_FEATURE_THREADING}
  818. { This can be checked without blocking; <arena>.threadState can only change from one value not equal to @self to another value not equal to @self. }
  819. if pVarOSChunk(pointer(arena) + pVarHeader(arena)[-1].ofsToOs)^.threadState <> @self then
  820. begin
  821. EnterCriticalSection(gs.lock);
  822. ts := pVarOSChunk(pointer(arena) + pVarHeader(arena)[-1].ofsToOs)^.threadState;
  823. if Assigned(ts) then
  824. begin
  825. { Despite atomic Push lock must be held as otherwise target thread might end and destroy its threadState.
  826. However, target thread won’t block to free p, so PushToFree instantly invalidates p. }
  827. result := IndexToSize(pCommonHeader(p - CommonHeaderSize)^.h and SizeIndexMask) - CommonHeaderSize;
  828. ts^.PushToFree(p);
  829. LeaveCriticalSection(gs.lock);
  830. exit;
  831. end;
  832. AdoptVarOwner(arena); { ...And continue! }
  833. LeaveCriticalSection(gs.lock);
  834. end;
  835. {$endif FPC_HAS_FEATURE_THREADING}
  836. pFreeChunk(p)^.next := arena^.firstFreeChunk;
  837. arena^.firstFreeChunk := p;
  838. sizeIndex := pCommonHeader(p - CommonHeaderSize)^.h and SizeIndexMask;
  839. result := IndexToSize(sizeIndex);
  840. dec(used, result);
  841. usedSizeMinus1 := int32(arena^.usedSizeMinus1) - int32(result);
  842. arena^.usedSizeMinus1 := uint32(usedSizeMinus1);
  843. dec(result, CommonHeaderSize);
  844. { “(usedSizeMinus1 = -1) or (usedSizeMinus1 >= arena^.almostFullThreshold)” as 1 comparison. }
  845. if uint32(usedSizeMinus1) >= arena^.almostFullThreshold then
  846. if usedSizeMinus1 <> -1 then
  847. begin
  848. dec(allocatedByFullArenas[sizeIndex], pVarHeader(arena)[-1].ch.h and VarSizeMask);
  849. { Add arena to partialArenas[sizeIndex]. }
  850. nextArena := partialArenas[sizeIndex];
  851. arena^.prev := nil;
  852. arena^.next := nextArena;
  853. if Assigned(nextArena) then
  854. nextArena^.prev := arena;
  855. partialArenas[sizeIndex] := arena;
  856. end else
  857. begin
  858. { Remove arena from partialArenas[sizeIndex], add to emptyArenas (maybe). }
  859. prevArena := arena^.prev;
  860. nextArena := arena^.next;
  861. if Assigned(prevArena) then
  862. prevArena^.next := nextArena
  863. else
  864. partialArenas[sizeIndex] := nextArena;
  865. if Assigned(nextArena) then
  866. nextArena^.prev := prevArena;
  867. if nEmptyArenas < MaxKeptFixedArenas then
  868. begin
  869. arena^.next := emptyArenas;
  870. emptyArenas := arena;
  871. inc(nEmptyArenas);
  872. end else
  873. FreeVar(arena);
  874. end;
  875. end;
  876. procedure HeapInc.ThreadState.FreeEmptyArenas;
  877. var
  878. arena: pFixedArena;
  879. begin
  880. while nEmptyArenas > 0 do
  881. begin
  882. arena := emptyArenas;
  883. emptyArenas := arena^.next;
  884. dec(nEmptyArenas);
  885. FreeVar(arena);
  886. end;
  887. end;
  888. {$ifdef HAS_SYSOSFREE}
  889. procedure HeapInc.ThreadState.ReplaceFreeOS1(&with: pFreeOSChunk);
  890. begin
  891. if Assigned(freeOS1) then
  892. begin
  893. dec(allocated, freeOS1^.size);
  894. SysOSFree(freeOS1, freeOS1^.size);
  895. end;
  896. freeOS1 := &with;
  897. end;
  898. {$endif HAS_SYSOSFREE}
  899. function HeapInc.ThreadState.GetOSChunk(minSize, maxSize: SizeUint): pOSChunk;
  900. {$if defined(FPC_HAS_FEATURE_THREADING) and not defined(HAS_SYSOSFREE)}
  901. var
  902. statv: SizeUint;
  903. {$endif FPC_HAS_FEATURE_THREADING and not HAS_SYSOSFREE}
  904. begin
  905. {$ifdef HAS_SYSOSFREE}
  906. result := freeOS1;
  907. if Assigned(result) then
  908. if (result^.size >= minSize) and (result^.size <= maxSize) then
  909. freeOS1 := nil
  910. else
  911. result := nil;
  912. {$else HAS_SYSOSFREE}
  913. result := freeOS.Get(minSize, maxSize);
  914. {$ifdef FPC_HAS_FEATURE_THREADING}
  915. if not Assigned(result) and Assigned(gs.freeOS.first) then { Racing precheck. }
  916. begin
  917. EnterCriticalSection(gs.lock);
  918. result := gs.freeOS.Get(minSize, maxSize);
  919. LeaveCriticalSection(gs.lock);
  920. if Assigned(result) then
  921. begin
  922. statv := allocated + result^.size;
  923. allocated := statv;
  924. inc(statv, gs.hugeUsed);
  925. if statv > maxAllocated then
  926. maxAllocated := statv;
  927. end;
  928. end;
  929. {$endif FPC_HAS_FEATURE_THREADING}
  930. {$endif HAS_SYSOSFREE}
  931. end;
  932. function HeapInc.ThreadState.AllocateOSChunk(minSize, maxSize: SizeUint): pOSChunk;
  933. var
  934. query, statv: SizeUint;
  935. begin
  936. query := used div 16 + minSize div 2; { Base: 6.25% of the memory used, so if GrowHeapSize2 = 1 Mb, 1 Mb OS allocations start at 16 Mb used. }
  937. if query > maxSize then { Limit by maxSize (usually GrowHeapSize2). }
  938. query := maxSize;
  939. if query < minSize then { But of course allocate at least the amount requested. Also triggers if maxSize was wrong (smaller than minSize). }
  940. query := minSize;
  941. query := (query + (OSChunkVarSizeQuant - 1)) and SizeUint(-OSChunkVarSizeQuant); { Quantize. }
  942. result := SysOSAlloc(query);
  943. if not Assigned(result) and (query > minSize) then
  944. begin
  945. query := minSize;
  946. result := SysOSAlloc(query);
  947. end;
  948. if not Assigned(result) then
  949. exit(AllocFailed);
  950. result^.size := query;
  951. statv := allocated + query;
  952. allocated := statv;
  953. inc(statv, gs.hugeUsed);
  954. if statv > maxAllocated then
  955. maxAllocated := statv;
  956. end;
  957. function HeapInc.ThreadState.AllocVar(size: SizeUint; isArena: boolean): pointer;
  958. var
  959. fv: pFreeVarChunk absolute result;
  960. tailFv: pFreeVarChunk;
  961. osChunk, osNext: pVarOSChunk;
  962. binIndex, vSizeFlags, statv: SizeUint;
  963. begin
  964. { Search varFree for (roughly) smallest chunk ≥ size. }
  965. binIndex := VarSizeToBinIndex(size + VarHeaderSize, true);
  966. { Round the size up to the bin size.
  967. Can do without that, but not doing that will often mean the inability to reuse the hole for the same size because varFree rounds up for searches and down for additions. }
  968. size := BinIndexToVarSize(binIndex);
  969. repeat { break = found fv or osChunk. }
  970. fv := varFree.Find(binIndex);
  971. if Assigned(fv) then
  972. break;
  973. { If allocating arena, try to allocate less than requested, within arena size limitations. }
  974. if isArena and (binIndex > MinArenaBinIndex) then
  975. begin
  976. fv := varFree.FindSmaller(binIndex);
  977. if Assigned(fv) and (fv^.binIndex >= MinArenaBinIndex) then
  978. begin
  979. size := pVarHeader(fv)[-1].ch.h and VarSizeMask; { Use the entire chunk. }
  980. break;
  981. end;
  982. fv := nil;
  983. end;
  984. { Try reusing empty OS chunk. }
  985. osChunk := pVarOSChunk(GetOSChunk(VarOSChunkDataOffset + size, GrowHeapSize2));
  986. if Assigned(osChunk) then
  987. break;
  988. { If there are empty arenas, free them and retry. }
  989. if nEmptyArenas > 0 then
  990. begin
  991. FreeEmptyArenas;
  992. continue;
  993. end;
  994. {$ifdef FPC_HAS_FEATURE_THREADING}
  995. { Try reusing an orphaned chunk. }
  996. fv := gs.varFree.Find(binIndex); { Preliminary search without blocking, assuming varFree.Find doesn’t do anything that can go wrong. }
  997. if Assigned(fv) then
  998. begin
  999. EnterCriticalSection(gs.lock);
  1000. fv := gs.varFree.Find(binIndex); { True search. }
  1001. if Assigned(fv) then
  1002. AdoptVarOwner(fv); { Moves fv to own varFree. }
  1003. LeaveCriticalSection(gs.lock);
  1004. if Assigned(fv) then
  1005. break;
  1006. end;
  1007. {$endif FPC_HAS_FEATURE_THREADING}
  1008. osChunk := pVarOSChunk(AllocateOSChunk(VarOSChunkDataOffset + size, GrowHeapSize2));
  1009. if Assigned(osChunk) then
  1010. break;
  1011. exit; { (nil) as fv is nil and mapped to result. }
  1012. until false;
  1013. if not Assigned(fv) then
  1014. begin
  1015. {$ifdef FPC_HAS_FEATURE_THREADING}
  1016. osChunk^.threadState := @self;
  1017. {$endif}
  1018. { Add osChunk to varOS. }
  1019. osNext := varOS;
  1020. osChunk^.prev := nil;
  1021. osChunk^.next := osNext;
  1022. if Assigned(osNext) then
  1023. osNext^.prev := osChunk;
  1024. varOS := osChunk;
  1025. { Format new free var chunk spanning the entire osChunk. FreeVarTail is not required. }
  1026. fv := pointer(osChunk) + (VarOSChunkDataOffset + VarHeaderSize);
  1027. pVarHeader(result - VarHeaderSize)^.ofsToOs := -(VarOSChunkDataOffset + VarHeaderSize);
  1028. pVarHeader(result - VarHeaderSize)^.ch.h := (uint32(osChunk^.size) - VarOSChunkDataOffset) and VarSizeMask + LastFlag;
  1029. end else
  1030. varFree.Remove(fv);
  1031. { Result will be allocated at the beginning of fv; maybe format the remainder and add it back to varFree. }
  1032. vSizeFlags := pVarHeader(fv)[-1].ch.h - size; { Inherits LastFlag. }
  1033. if
  1034. { Allow over-allocating arenas by up to MaxArenaOverallocation.
  1035. Harmless check if not an arena, assuming (MaxArenaOverallocation and -VarSizeQuant + VarSizeQuant) >= MinSearchableVarHeaderAndPayload. }
  1036. (vSizeFlags >= MaxArenaOverallocation and -VarSizeQuant + VarSizeQuant) or
  1037. { Allow leaving a non-searchable tail if non-last.
  1038. “vSizeFlags >= MinAnyVarHeaderAndPayload” if non-last, “vSizeFlags >= MinSearchableVarHeaderAndPayload” if last. }
  1039. not isArena and (vSizeFlags >= MinAnyVarHeaderAndPayload + (MinSearchableVarHeaderAndPayload - MinAnyVarHeaderAndPayload) div LastFlag * (vSizeFlags and LastFlag)) then
  1040. begin
  1041. pVarHeader(result - VarHeaderSize)^.ch.h := uint32(size) + UsedFlag;
  1042. tailFv := result + size; { fv (result) = allocated block, tailFv = remainder. }
  1043. pVarHeader(pointer(tailFv) - VarHeaderSize)^.ofsToOs := pVarHeader(result - VarHeaderSize)^.ofsToOs - int32(size);
  1044. pVarHeader(pointer(tailFv) - VarHeaderSize)^.ch.h := vSizeFlags;
  1045. { Chunk to the right retains its PrevFreeFlag. }
  1046. if vSizeFlags and LastFlag = 0 then
  1047. pFreeVarTail(pointer(tailFv) + vSizeFlags - (VarHeaderSize + FreeVarTailSize))^.size := vSizeFlags;
  1048. if vSizeFlags >= MinSearchableVarHeaderAndPayload then
  1049. varFree.Add(tailFv, vSizeFlags); { Rounding down, so not masking is ok. }
  1050. end else
  1051. begin
  1052. { Use the entire chunk. }
  1053. inc(vSizeFlags, size);
  1054. pVarHeader(result - VarHeaderSize)^.ch.h := uint32(vSizeFlags) + UsedFlag;
  1055. if vSizeFlags and LastFlag = 0 then
  1056. dec(pVarHeader(result + vSizeFlags - VarHeaderSize)^.ch.h, PrevIsFreeFlag);
  1057. size := vSizeFlags and VarSizeMask;
  1058. end;
  1059. if isArena then
  1060. inc(pVarHeader(result)[-1].ch.h, FixedArenaFlag) { Arenas aren’t counted in “used” directly. }
  1061. else
  1062. inc(used, size);
  1063. { Update maxUsed regardless. }
  1064. statv := used + gs.hugeUsed;
  1065. if statv > maxUsed then
  1066. maxUsed := statv;
  1067. end;
  1068. function HeapInc.ThreadState.FreeVar(p: pointer): SizeUint;
  1069. var
  1070. fSizeFlags, hNext, prevSize: SizeUint;
  1071. osChunk, osPrev, osNext: pVarOSChunk;
  1072. {$ifdef FPC_HAS_FEATURE_THREADING}
  1073. pts: ^pThreadState;
  1074. {$endif FPC_HAS_FEATURE_THREADING}
  1075. {$ifndef HAS_SYSOSFREE}
  1076. freeOsNext: pFreeOSChunk;
  1077. fOs: ^FreeOSChunkList;
  1078. {$endif not HAS_SYSOSFREE}
  1079. begin
  1080. {$ifdef FPC_HAS_FEATURE_THREADING}
  1081. pts := @pVarOSChunk(p + pVarHeader(p - VarHeaderSize)^.ofsToOs)^.threadState;
  1082. if pts^ <> @self then
  1083. begin
  1084. EnterCriticalSection(gs.lock);
  1085. if Assigned(pts^) then
  1086. begin
  1087. { Despite atomic Push lock must be held as otherwise target thread might end and destroy its threadState.
  1088. However, target thread won’t block to free p, so PushToFree instantly invalidates p. }
  1089. result := pVarHeader(p - VarHeaderSize)^.ch.h and VarSizeMask - VarHeaderSize;
  1090. pts^^.PushToFree(p);
  1091. LeaveCriticalSection(gs.lock);
  1092. exit;
  1093. end;
  1094. AdoptVarOwner(p); { ...And continue! }
  1095. LeaveCriticalSection(gs.lock);
  1096. end;
  1097. {$endif FPC_HAS_FEATURE_THREADING}
  1098. fSizeFlags := pVarHeader(p - VarHeaderSize)^.ch.h;
  1099. result := fSizeFlags and VarSizeMask;
  1100. if fSizeFlags and FixedArenaFlag = 0 then
  1101. dec(used, result)
  1102. else
  1103. dec(fSizeFlags, FixedArenaFlag);
  1104. { If next/prev are free, remove them from varFree and merge with f — (f)uture (f)ree chunk that starts at p and has fSizeFlags. }
  1105. if fSizeFlags and LastFlag = 0 then
  1106. begin
  1107. hNext := pVarHeader(p + result - VarHeaderSize)^.ch.h;
  1108. if uint32(hNext) and UsedFlag = 0 then
  1109. begin
  1110. inc(fSizeFlags, hNext); { Inherit LastFlag, other p2 flags must be 0. }
  1111. if hNext >= MinSearchableVarHeaderAndPayload then { Logically “hNext and VarSizeMask”. }
  1112. varFree.Remove(p + result);
  1113. { Chunk to the right retains its PrevFreeFlag. }
  1114. end;
  1115. end;
  1116. if fSizeFlags and PrevIsFreeFlag <> 0 then
  1117. begin
  1118. prevSize := pFreeVarTail(p - (VarHeaderSize + FreeVarTailSize))^.size;
  1119. dec(p, prevSize);
  1120. fSizeFlags := fSizeFlags - PrevIsFreeFlag + prevSize;
  1121. if uint32(prevSize) >= MinSearchableVarHeaderAndPayload then
  1122. varFree.Remove(p);
  1123. end;
  1124. { Turn p into a free chunk and add it back to varFree...
  1125. unless it spans the entire OS chunk, in which case instead move the chunk from varOS to freeOS1 / freeOS. }
  1126. if (fSizeFlags and LastFlag = 0) or (pVarHeader(p - VarHeaderSize)^.ofsToOs <> -(VarOSChunkDataOffset + VarHeaderSize)) then
  1127. begin
  1128. dec(fSizeFlags, UsedFlag);
  1129. pVarHeader(p - VarHeaderSize)^.ch.h := fSizeFlags;
  1130. varFree.Add(p, fSizeFlags);
  1131. if fSizeFlags and LastFlag = 0 then
  1132. begin
  1133. pVarHeader(p + fSizeFlags - VarHeaderSize)^.ch.h := pVarHeader(p + fSizeFlags - VarHeaderSize)^.ch.h or PrevIsFreeFlag; { Could have it already. }
  1134. pFreeVarTail(p + fSizeFlags - (VarHeaderSize + FreeVarTailSize))^.size := fSizeFlags;
  1135. end;
  1136. end else
  1137. begin
  1138. osChunk := p - (VarOSChunkDataOffset + VarHeaderSize);
  1139. { Remove osChunk from varOS. }
  1140. osPrev := osChunk^.prev;
  1141. osNext := osChunk^.next;
  1142. if Assigned(osPrev) then
  1143. osPrev^.next := osNext
  1144. else
  1145. varOS := osNext;
  1146. if Assigned(osNext) then
  1147. osNext^.prev := osPrev;
  1148. {$ifdef HAS_SYSOSFREE}
  1149. ReplaceFreeOS1(pFreeOSChunk(osChunk)); { Move to freeOS1, discarding old freeOS1. }
  1150. {$else HAS_SYSOSFREE}
  1151. fOs := @freeOS;
  1152. { Share if huge. }
  1153. {$ifdef FPC_HAS_FEATURE_THREADING}
  1154. if osChunk^.size > GrowHeapSize2 then
  1155. begin
  1156. fOs := @gs.freeOS;
  1157. EnterCriticalSection(gs.lock);
  1158. end;
  1159. {$endif FPC_HAS_FEATURE_THREADING}
  1160. { Add to fOs. }
  1161. freeOsNext := fOs^.first;
  1162. osChunk^.prev := nil;
  1163. osChunk^.next := freeOsNext;
  1164. if Assigned(freeOsNext) then
  1165. freeOsNext^.prev := osChunk
  1166. else
  1167. fOs^.last := pFreeOSChunk(osChunk);
  1168. fOs^.first := pFreeOSChunk(osChunk);
  1169. {$ifdef FPC_HAS_FEATURE_THREADING}
  1170. if fOs <> @freeOS then
  1171. begin
  1172. dec(allocated, osChunk^.size); { gs.freeOS aren’t counted anywhere, for now. }
  1173. LeaveCriticalSection(gs.lock);
  1174. end;
  1175. {$endif FPC_HAS_FEATURE_THREADING}
  1176. {$endif HAS_SYSOSFREE}
  1177. end;
  1178. dec(result, VarHeaderSize);
  1179. end;
  1180. function HeapInc.ThreadState.TryResizeVar(p: pointer; size: SizeUint): pointer;
  1181. var
  1182. ar: pointer absolute result;
  1183. fv, fp: pointer;
  1184. arSizeFlags, prevSize2, maxFv, minFragment, fSizeFlags, hNext, hNext2, oldph: uint32;
  1185. prevSize, binIndex, oldpsize, statv, arSize: SizeUint;
  1186. {$ifdef FPC_HAS_FEATURE_THREADING}
  1187. pts: ^pThreadState;
  1188. {$endif FPC_HAS_FEATURE_THREADING}
  1189. begin
  1190. result := nil;
  1191. if (size > GrowHeapSize2) { Assuming GrowHeapSize2 is never larger than 3.999 Gb, this prevents overflow on adding headers and allows uint32(size) to tune for x64. }
  1192. or (uint32(size) <= MaxFixedHeaderAndPayload - CommonHeaderSize)
  1193. then
  1194. exit;
  1195. {$ifdef FPC_HAS_FEATURE_THREADING}
  1196. pts := @pVarOSChunk(p + pVarHeader(p - VarHeaderSize)^.ofsToOs)^.threadState;
  1197. if pts^ <> @self then
  1198. begin
  1199. if Assigned(pts^) then { Pretest to avoid acquiring the lock. }
  1200. exit;
  1201. EnterCriticalSection(gs.lock);
  1202. if Assigned(pts^) then
  1203. begin
  1204. LeaveCriticalSection(gs.lock);
  1205. exit;
  1206. end;
  1207. AdoptVarOwner(p); { ...And continue! }
  1208. LeaveCriticalSection(gs.lock);
  1209. end;
  1210. {$endif FPC_HAS_FEATURE_THREADING}
  1211. { Round the size up, but only if supported by VarSizeToBinIndex: chunks can be reallocated to the sizes larger than MaxVarHeaderAndPayload. }
  1212. if uint32(size) <= MaxVarHeaderAndPayload - VarHeaderSize then
  1213. begin
  1214. binIndex := VarSizeToBinIndex(size + VarHeaderSize, true);
  1215. size := BinIndexToVarSize(binIndex);
  1216. end else
  1217. size := uint32(uint32(size) + (VarHeaderSize + VarSizeQuant - 1)) and uint32(-VarSizeQuant); { Just do the strictly necessary quantization... }
  1218. { ar + arSizeFlags (from “around”) is the chunk made from p and its adjacent free chunks. }
  1219. ar := p;
  1220. arSizeFlags := pVarHeader(ar - VarHeaderSize)^.ch.h;
  1221. if arSizeFlags and LastFlag = 0 then
  1222. begin
  1223. hNext := pVarHeader(ar + arSizeFlags and VarSizeMask - VarHeaderSize)^.ch.h;
  1224. if hNext and UsedFlag = 0 then
  1225. inc(arSizeFlags, hNext); { Inherit LastFlag, other flags must be 0. }
  1226. end;
  1227. if arSizeFlags and PrevIsFreeFlag <> 0 then
  1228. begin
  1229. prevSize := pFreeVarTail(ar - (VarHeaderSize + FreeVarTailSize))^.size;
  1230. dec(ar, prevSize);
  1231. inc(arSizeFlags, prevSize);
  1232. end;
  1233. if uint32(size) > arSizeFlags then { “ar” has no way to fit the new chunk. }
  1234. exit(nil);
  1235. { Check if there is a better place... }
  1236. maxFv := arSizeFlags div 4 * 3;
  1237. if (uint32(size) <= MaxVarHeaderAndPayload) and (uint32(size) < maxFv) then { Pretest the condition on a “CONSIDERABLY” better fv below, maybe it’s not going to happen no matter what. }
  1238. begin
  1239. fv := varFree.Find(binIndex);
  1240. if Assigned(fv)
  1241. { fv may be one of the chunks around; in this case, ignore it. Checked as unsigned(fv - ar) < arSize. }
  1242. and (PtrUint(PtrInt(PtrUint(fv)) - PtrInt(PtrUint(ar))) >= arSizeFlags) { Logically “arSizeFlags and VarSizeMask”. }
  1243. { To justify moving FAR, better place should be CONSIDERABLY better: say, <75% of the ar. }
  1244. and (pVarHeader(fv)[-1].ch.h < maxFv) { Ignore masking, this is a rough check anyway. }
  1245. then
  1246. exit(nil);
  1247. end;
  1248. { So p will be placed inside “ar” after all. It is either moved to the beginning of “ar” or stays in place.
  1249. There might be no choice but to move: reallocating A in
  1250. [free 1,000][A 1,000][free 1,000]
  1251. to 2,500 bytes has to move, resulting in
  1252. [A 2,500][free 500].
  1253. But if there is a choice, moving might be or not be worth it. If we have
  1254. [free 5,000][A 1,000][free 5,000]
  1255. then moving will give
  1256. [A 1,000][free 10,000]
  1257. and that’s the point — [free 10,000] is better than 2 × [free 5,000]. But if we have
  1258. [free 64][A 1,000][free 9,936]
  1259. then moving for the sake of defragmenting these 64 bytes is definitely a waste of time.
  1260. So if there is a choice, moving is performed if fragments on BOTH sides are larger than 1/8 (12.5%) of the (new) size. }
  1261. if arSizeFlags and PrevIsFreeFlag <> 0 then
  1262. begin
  1263. prevSize2 := pFreeVarTail(p - (VarHeaderSize + FreeVarTailSize))^.size;
  1264. { Consider (not) moving... }
  1265. dec(arSizeFlags, prevSize2); { Temporarily (or not) remove prevSize from arSizeFlags. This corresponds to the size available without moving. }
  1266. minFragment := uint32(size) div 8;
  1267. if (arSizeFlags < uint32(size)) { Size does not fit without moving? }
  1268. or (prevSize2 >= minFragment) and (uint32(arSizeFlags - uint32(size)) >= minFragment) { There are large enough fragments on both sides? }
  1269. then
  1270. begin
  1271. if prevSize2 >= MinSearchableVarHeaderAndPayload then
  1272. varFree.Remove(ar);
  1273. inc(arSizeFlags, prevSize2 - PrevIsFreeFlag); { Add prevSize back, and remove PrevIsFreeFlag. }
  1274. { Move(p^, ar^, ...) is postponed, see below. }
  1275. end else
  1276. { Not moving; finish the removal of the previous chunk from “ar”. arSizeFlags is already decreased by prevSize, and keeps PrevIsFreeFlag. }
  1277. ar := p; { Same as inc(ar, prevSize2). }
  1278. end;
  1279. { Remove the free chunk after p. Note that:
  1280. — Under some circumstances, it can be overwritten with Move, so Move must be postponed.
  1281. — This section might decide that TryResizeVar is a complete no-op and exit “early”, and this decision depends on the decision to move,
  1282. so the decision to move must be made first.
  1283. Though a nontrivial amount of work has been done by this point, some more remains and can be skipped to speed up no-op ReallocMems (e.g. 26 → 16 ns).
  1284. Without shortcutting the no-op case, this entire section can be simply moved above the previous one and postponing Move would not be required. }
  1285. oldph := pVarHeader(p)[-1].ch.h;
  1286. oldpsize := oldph and VarSizeMask;
  1287. if (uint32(size) = uint32(oldpsize)) and (ar = p) then
  1288. { TryResizeVar was a no-op, and with some explicit efforts we managed to write nothing by this point,
  1289. so we use our last chance to get out. }
  1290. exit;
  1291. if oldph and LastFlag = 0 then
  1292. begin
  1293. hNext2 := pVarHeader(p + oldpsize - VarHeaderSize)^.ch.h;
  1294. if (hNext2 and UsedFlag = 0) and (hNext2 >= MinSearchableVarHeaderAndPayload) then
  1295. varFree.Remove(p + oldpsize);
  1296. end;
  1297. dec(used, oldpsize);
  1298. if ar <> p then
  1299. begin
  1300. if uint32(size) < uint32(oldpsize) then { oldpsize is reused as “moveSize”. }
  1301. oldpsize := uint32(size);
  1302. Move(p^, ar^, oldpsize - VarHeaderSize);
  1303. end;
  1304. { Format the free chunk after ar, or its absence. }
  1305. fSizeFlags := uint32(arSizeFlags - uint32(size)) and (VarSizeMask or LastFlag);
  1306. if fSizeFlags >= uint32(MinAnyVarHeaderAndPayload + (MinSearchableVarHeaderAndPayload - MinAnyVarHeaderAndPayload) div LastFlag * (fSizeFlags and LastFlag)) then
  1307. begin
  1308. dec(arSizeFlags, fSizeFlags);
  1309. arSize := arSizeFlags and VarSizeMask;
  1310. fp := ar + arSize;
  1311. pVarHeader(fp)[-1].ofsToOs := pVarHeader(ar)[-1].ofsToOs - int32(arSize);
  1312. pVarHeader(fp)[-1].ch.h := fSizeFlags;
  1313. if fSizeFlags and LastFlag = 0 then
  1314. begin
  1315. pFreeVarTail(fp + fSizeFlags - (VarHeaderSize + FreeVarTailSize))^.size := fSizeFlags;
  1316. pVarHeader(fp + fSizeFlags)[-1].ch.h := pVarHeader(fp + fSizeFlags)[-1].ch.h or PrevIsFreeFlag; { May have had it already. }
  1317. end;
  1318. if fSizeFlags >= MinSearchableVarHeaderAndPayload then
  1319. varFree.Add(fp, fSizeFlags);
  1320. end
  1321. else if arSizeFlags and LastFlag = 0 then
  1322. pVarHeader(ar + arSizeFlags and VarSizeMask)[-1].ch.h := pVarHeader(ar + arSizeFlags and VarSizeMask)[-1].ch.h and uint32(not PrevIsFreeFlag); { May not have had it already. }
  1323. pVarHeader(ar)[-1].ch.h := arSizeFlags;
  1324. statv := used + arSizeFlags and VarSizeMask;
  1325. used := statv;
  1326. inc(statv, gs.hugeUsed);
  1327. if statv > maxUsed then
  1328. maxUsed := statv;
  1329. end;
  1330. { If SysOSFree is available, huge chunks aren’t cached by any means.
  1331. If SysOSFree is not available, there’s no choice but to cache them.
  1332. Caching is done directly into gs.freeOS if FPC_HAS_FEATURE_THREADING, otherwise ThreadState.freeOS. }
  1333. class function HeapInc.ThreadState.AddToHugeUsed(delta: SizeInt): SizeUint;
  1334. begin
  1335. {$if not defined(FPC_HAS_FEATURE_THREADING)}
  1336. result := SizeUint(SizeInt(gs.hugeUsed) + delta);
  1337. gs.hugeUsed := result;
  1338. {$elseif not defined(VER3_2)}
  1339. result := AtomicIncrement(gs.hugeUsed, SizeUint(delta));
  1340. {$elseif sizeof(SizeInt) = sizeof(int64)}
  1341. result := SizeUint(delta + InterlockedExchangeAdd64(SizeInt(gs.hugeUsed), delta));
  1342. {$else}
  1343. result := SizeUint(delta + InterlockedExchangeAdd(SizeInt(gs.hugeUsed), delta));
  1344. {$endif}
  1345. end;
  1346. function HeapInc.ThreadState.AllocHuge(size: SizeUint): pointer;
  1347. var
  1348. userSize: SizeUint;
  1349. begin
  1350. userSize := size;
  1351. size := (size + (HugeChunkDataOffset + CommonHeaderSize + OSChunkVarSizeQuant - 1)) and SizeUint(-OSChunkVarSizeQuant);
  1352. if size < userSize then { Overflow. }
  1353. exit(AllocFailed);
  1354. {$ifdef HAS_SYSOSFREE}
  1355. result := SysOSAlloc(size);
  1356. if not Assigned(result) then
  1357. exit(AllocFailed);
  1358. pHugeChunk(result)^.size := size;
  1359. {$else HAS_SYSOSFREE}
  1360. result := GetOSChunk(size, High(SizeUint));
  1361. if not Assigned(result) then
  1362. begin
  1363. result := AllocateOSChunk(size, High(SizeUint));
  1364. if not Assigned(result) then
  1365. exit; { AllocateOSChunk throws an error if required. }
  1366. end;
  1367. size := pOSChunk(result)^.size;
  1368. dec(allocated, size); { After GetOSChunk* chunk size is counted in “allocated”; don’t count. }
  1369. {$endif HAS_SYSOSFREE}
  1370. pCommonHeader(result + HugeChunkDataOffset)^.h := HugeHeader;
  1371. inc(result, HugeChunkDataOffset + CommonHeaderSize);
  1372. UpdateMaxStats(AddToHugeUsed(size));
  1373. end;
  1374. function HeapInc.ThreadState.FreeHuge(p: pointer): SizeUint;
  1375. {$ifndef HAS_SYSOSFREE}
  1376. var
  1377. fOs: ^FreeOSChunkList;
  1378. osPrev: pOSChunk;
  1379. {$endif ndef HAS_SYSOSFREE}
  1380. begin
  1381. dec(p, HugeChunkDataOffset + CommonHeaderSize);
  1382. result := pHugeChunk(p)^.size;
  1383. AddToHugeUsed(-SizeInt(result));
  1384. {$ifndef HAS_SYSOSFREE} { But you’d better have SysOSFree... }
  1385. {$ifdef FPC_HAS_FEATURE_THREADING}
  1386. fOs := @gs.freeOS; { gs.freeOS aren’t counted anywhere (for now). }
  1387. EnterCriticalSection(gs.lock);
  1388. {$else FPC_HAS_FEATURE_THREADING}
  1389. fOs := @freeOS;
  1390. inc(allocated, result); { ThreadState.freeOS are counted in ThreadState.allocated. But since “size” (= result) is just moved from “hugeUsed” to “allocated”, it won’t affect maximums. }
  1391. {$endif FPC_HAS_FEATURE_THREADING}
  1392. { Turn p into FreeOSChunk and add to fOs; add to the end to reduce the chance for this chunk to be reused
  1393. (other OS chunks are added to the beginning and searched from the beginning). }
  1394. osPrev := fOs^.last;
  1395. pFreeOSChunk(p)^.prev := osPrev;
  1396. pFreeOSChunk(p)^.next := nil;
  1397. if Assigned(osPrev) then
  1398. osPrev^.next := p
  1399. else
  1400. fOs^.first := p;
  1401. fOs^.last := p;
  1402. {$ifdef FPC_HAS_FEATURE_THREADING} LeaveCriticalSection(gs.lock); {$endif}
  1403. {$endif ndef HAS_SYSOSFREE}
  1404. {$ifdef HAS_SYSOSFREE} SysOSFree(p, result); {$endif}
  1405. dec(result, HugeChunkDataOffset + CommonHeaderSize);
  1406. end;
  1407. function HeapInc.ThreadState.TryResizeHuge(p: pointer; size: SizeUint): pointer;
  1408. var
  1409. userSize, oldSize: SizeUint;
  1410. begin
  1411. userSize := size;
  1412. size := (size + (HugeChunkDataOffset + CommonHeaderSize + OSChunkVarSizeQuant - 1)) and SizeUint(-OSChunkVarSizeQuant);
  1413. if (size < userSize) or { Overflow. }
  1414. (size < GrowHeapSize2 div 4) { Limit on shrinking huge chunks. }
  1415. then
  1416. exit(nil);
  1417. oldSize := pHugeChunk(p - (HugeChunkDataOffset + CommonHeaderSize))^.size;
  1418. if size = oldSize then
  1419. exit(p);
  1420. {$ifdef FPC_SYSTEM_HAS_SYSOSREALLOC}
  1421. result := SysOSRealloc(p - (HugeChunkDataOffset + CommonHeaderSize), oldSize, size);
  1422. if Assigned(result) then
  1423. begin
  1424. UpdateMaxStats(AddToHugeUsed(SizeInt(size) - SizeInt(oldSize)));
  1425. pHugeChunk(result)^.size := size;
  1426. inc(result, HugeChunkDataOffset + CommonHeaderSize);
  1427. end;
  1428. {$else FPC_SYSTEM_HAS_SYSOSREALLOC}
  1429. result := nil; { Just don’t. Note shrinking 20 Mb to 19 will require temporary 39 because of this. }
  1430. {$endif FPC_SYSTEM_HAS_SYSOSREALLOC}
  1431. end;
  1432. procedure HeapInc.ThreadState.UpdateMaxStats(hugeUsed: SizeUint);
  1433. var
  1434. statv: SizeUint;
  1435. begin
  1436. statv := used + hugeUsed;
  1437. if statv > maxUsed then
  1438. maxUsed := statv;
  1439. statv := allocated + hugeUsed;
  1440. if statv > maxAllocated then
  1441. maxAllocated := statv;
  1442. end;
  1443. {$ifdef FPC_HAS_FEATURE_THREADING}
  1444. procedure HeapInc.ThreadState.PushToFree(p: pFreeChunk);
  1445. var
  1446. nx: pFreeChunk;
  1447. begin
  1448. repeat
  1449. nx := toFree;
  1450. p^.next := nx;
  1451. WriteBarrier; { Write p after p^.next. }
  1452. until {$ifdef VER3_2} InterlockedCompareExchange {$else} AtomicCmpExchange {$endif} (toFree, p, nx) = nx;
  1453. end;
  1454. procedure HeapInc.ThreadState.FlushToFree;
  1455. begin
  1456. FreeToFreeList({$ifdef VER3_2} InterlockedExchange {$else} AtomicExchange {$endif} (toFree, nil));
  1457. end;
  1458. class procedure HeapInc.ThreadState.FreeToFreeList(tf: pFreeChunk);
  1459. var
  1460. nx: pFreeChunk;
  1461. begin
  1462. while Assigned(tf) do
  1463. begin
  1464. ReadDependencyBarrier; { Read tf^.next after tf. }
  1465. nx := tf^.next;
  1466. SysFreeMem(tf);
  1467. tf := nx;
  1468. end;
  1469. end;
  1470. procedure HeapInc.ThreadState.Orphan;
  1471. var
  1472. vOs: pVarOSChunk;
  1473. p: pointer;
  1474. h: uint32;
  1475. {$ifndef HAS_SYSOSFREE}
  1476. lastFree, nextFree: pFreeOSChunk;
  1477. {$endif not HAS_SYSOSFREE}
  1478. begin
  1479. if gs.lockUse <> 0 then
  1480. EnterCriticalSection(HeapInc.gs.lock);
  1481. FlushToFree; { Performing it under gs.lock guarantees there will be no new toFree requests. }
  1482. FreeEmptyArenas; { Has to free all empty arenas, otherwise the chunk that contains only empty arenas can leak. }
  1483. {$if not defined(HAS_SYSOSFREE)}
  1484. { Prepend freeOS to gs.freeOS. }
  1485. lastFree := freeOS.last;
  1486. if Assigned(lastFree) then
  1487. begin
  1488. nextFree := gs.freeOS.first;
  1489. lastFree^.next := nextFree;
  1490. if Assigned(nextFree) then
  1491. nextFree^.prev := lastFree
  1492. else
  1493. gs.freeOS.last := lastFree;
  1494. gs.freeOS.first := freeOS.first;
  1495. freeOS.first := nil;
  1496. freeOS.last := nil;
  1497. end;
  1498. {$elseif defined(SUPPORT_INIT_HEAP_PROCESS_WIDE)}
  1499. { Remove from gs.threads, if present. }
  1500. if Assigned(prev) then
  1501. prev^.next := next
  1502. else if @self = gs.threads then { if prev = nil, then this ThreadState is either absent from gs.threads or is its first item. }
  1503. gs.threads := next;
  1504. if Assigned(next) then
  1505. next^.prev := prev;
  1506. prev := nil;
  1507. next := nil;
  1508. {$endif not HAS_SYSOSFREE | defined(SUPPORT_INIT_HEAP_PROCESS_WIDE)}
  1509. vOs := varOS;
  1510. while Assigned(vOs) do
  1511. begin
  1512. vOs^.threadState := nil;
  1513. p := pointer(vOs) + (VarOSChunkDataOffset + VarHeaderSize);
  1514. repeat
  1515. h := pVarHeader(p - VarHeaderSize)^.ch.h;
  1516. if (h and UsedFlag = 0) and (h >= MinSearchableVarHeaderAndPayload) then
  1517. gs.varFree.Add(p, pFreeVarChunk(p)^.binIndex);
  1518. inc(p, h and VarSizeMask);
  1519. until h and LastFlag <> 0;
  1520. vOs := vOs^.next;
  1521. end;
  1522. varOS := nil;
  1523. if gs.lockUse <> 0 then
  1524. LeaveCriticalSection(gs.lock);
  1525. {$ifdef HAS_SYSOSFREE}
  1526. ReplaceFreeOS1(nil); { Does not require gs.lock. }
  1527. {$endif HAS_SYSOSFREE}
  1528. end;
  1529. procedure HeapInc.ThreadState.AdoptArena(arena: pFixedArena);
  1530. var
  1531. sizeIndex: SizeUint;
  1532. nextArena: pFixedArena;
  1533. begin
  1534. sizeIndex := pCommonHeader(pointer(arena) + FixedArenaDataOffset)^.h and SizeIndexMask;
  1535. inc(used, arena^.usedSizeMinus1 + 1); { maxUsed is updated at the end of AdoptVarOwner. }
  1536. { Orphan frees all empty arenas, so adopted arena can’t be empty. }
  1537. if arena^.usedSizeMinus1 < arena^.almostFullThreshold + IndexToSize(sizeIndex) then
  1538. begin
  1539. { Add arena to partialArenas[sizeIndex]. }
  1540. nextArena := partialArenas[sizeIndex];
  1541. arena^.prev := nil;
  1542. arena^.next := nextArena;
  1543. if Assigned(nextArena) then
  1544. nextArena^.prev := arena;
  1545. partialArenas[sizeIndex] := arena;
  1546. end else
  1547. inc(allocatedByFullArenas[sizeIndex], pVarHeader(arena)[-1].ch.h and VarSizeMask);
  1548. end;
  1549. procedure HeapInc.ThreadState.AdoptVarOwner(p: pointer);
  1550. var
  1551. statv: SizeUint;
  1552. h: uint32;
  1553. vOs, osNext: pVarOSChunk;
  1554. begin
  1555. vOs := p + pVarHeader(p)[-1].ofsToOs;
  1556. vOs^.threadState := @self;
  1557. { Add OS chunk to varOS. }
  1558. vOs^.prev := nil;
  1559. osNext := varOS;
  1560. vOs^.next := osNext;
  1561. if Assigned(osNext) then
  1562. osNext^.prev := vOs;
  1563. varOS := vOs;
  1564. statv := allocated + vOs^.size;
  1565. allocated := statv;
  1566. inc(statv, gs.hugeUsed);
  1567. if statv > maxAllocated then
  1568. maxAllocated := statv;
  1569. p := pointer(vOs) + VarOSChunkDataOffset + VarHeaderSize;
  1570. repeat
  1571. h := pVarHeader(p - VarHeaderSize)^.ch.h;
  1572. if h and UsedFlag = 0 then
  1573. begin
  1574. if h >= MinSearchableVarHeaderAndPayload then
  1575. begin
  1576. gs.varFree.Remove(p);
  1577. varFree.Add(p, pFreeVarChunk(p)^.binIndex);
  1578. end;
  1579. end
  1580. else if h and FixedArenaFlag <> 0 then
  1581. AdoptArena(p)
  1582. else
  1583. inc(used, h and VarSizeMask); { maxUsed is updated after the loop. }
  1584. inc(p, h and VarSizeMask);
  1585. until h and LastFlag <> 0;
  1586. statv := used + gs.hugeUsed;
  1587. if statv > maxUsed then
  1588. maxUsed := statv;
  1589. end;
  1590. {$ifndef FPC_SECTION_THREADVARS}
  1591. procedure HeapInc.ThreadState.FixupSelfPtr;
  1592. var
  1593. vOs: pVarOSChunk;
  1594. begin
  1595. vOs := varOS;
  1596. while Assigned(vOs) do
  1597. begin
  1598. vOs^.threadState := @self;
  1599. vOs := vOs^.next;
  1600. end;
  1601. {$if defined(SUPPORT_INIT_HEAP_PROCESS_WIDE) and defined(HAS_SYSOSFREE)}
  1602. { Not sure if required... }
  1603. if Assigned(prev) then
  1604. prev^.next := @self
  1605. else
  1606. gs.threads := @self;
  1607. if Assigned(next) then
  1608. next^.prev := @self;
  1609. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE and HAS_SYSOSFREE}
  1610. end;
  1611. {$endif ndef FPC_SECTION_THREADVARS}
  1612. {$endif FPC_HAS_FEATURE_THREADING}
  1613. class function HeapInc.AllocFailed: pointer;
  1614. begin
  1615. if not ReturnNilIfGrowHeapFails then
  1616. HandleError(204);
  1617. result := nil;
  1618. end;
  1619. function SysGetFPCHeapStatus:TFPCHeapStatus;
  1620. var
  1621. ts: HeapInc.pThreadState;
  1622. hugeUsed: SizeUint;
  1623. begin
  1624. ts := @HeapInc.thisTs;
  1625. hugeUsed := HeapInc.gs.hugeUsed;
  1626. ts^.UpdateMaxStats(hugeUsed); { Cheat to avoid clearly implausible values like current > max. }
  1627. result.MaxHeapSize := ts^.maxAllocated;
  1628. result.MaxHeapUsed := ts^.maxUsed;
  1629. result.CurrHeapSize := hugeUsed + ts^.allocated;
  1630. result.CurrHeapUsed := hugeUsed + ts^.used;
  1631. result.CurrHeapFree := result.CurrHeapSize - result.CurrHeapUsed;
  1632. end;
  1633. function SysGetHeapStatus :THeapStatus;
  1634. var
  1635. fhs: TFPCHeapStatus;
  1636. begin
  1637. fhs := SysGetFPCHeapStatus;
  1638. FillChar((@result)^, sizeof(result), 0);
  1639. result.TotalAllocated := fhs.CurrHeapUsed;
  1640. result.TotalFree := fhs.CurrHeapSize - fhs.CurrHeapUsed;
  1641. result.TotalAddrSpace := fhs.CurrHeapSize;
  1642. end;
  1643. function SysGetMem(size : ptruint):pointer;
  1644. var
  1645. ts: HeapInc.pThreadState;
  1646. begin
  1647. ts := @HeapInc.thisTs;
  1648. if size <= HeapInc.MaxFixedHeaderAndPayload - HeapInc.CommonHeaderSize then
  1649. exit(ts^.AllocFixed(size));
  1650. {$ifdef FPC_HAS_FEATURE_THREADING}
  1651. if Assigned(ts^.toFree) then
  1652. ts^.FlushToFree;
  1653. {$endif}
  1654. if (size < GrowHeapSize2 div 2) { Approximate idea on the max size of the variable chunk. Approximate because size does not include headers but GrowHeapSize2 does. }
  1655. and (size <= HeapInc.MaxVarHeaderAndPayload - HeapInc.VarHeaderSize) then
  1656. result := ts^.AllocVar(size, false)
  1657. else
  1658. result := ts^.AllocHuge(size);
  1659. end;
  1660. function SysFreeMem(p: pointer): ptruint;
  1661. var
  1662. ts: HeapInc.pThreadState;
  1663. begin
  1664. if Assigned(p) then
  1665. begin
  1666. ts := @HeapInc.thisTs;
  1667. if HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h and HeapInc.FixedFlag <> 0 then
  1668. result := ts^.FreeFixed(p)
  1669. else if HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h <> HeapInc.HugeHeader then
  1670. result := ts^.FreeVar(p)
  1671. else
  1672. result := ts^.FreeHuge(p);
  1673. end
  1674. else
  1675. result := 0;
  1676. end;
  1677. function SysMemSize(p: pointer): ptruint;
  1678. var
  1679. h: uint32;
  1680. begin
  1681. if not Assigned(p) then
  1682. exit(0);
  1683. h := HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h;
  1684. if h and HeapInc.FixedFlag <> 0 then
  1685. result := HeapInc.IndexToSize(h and HeapInc.SizeIndexMask) - HeapInc.CommonHeaderSize
  1686. else if h <> HeapInc.HugeHeader then
  1687. result := HeapInc.pVarHeader(p - HeapInc.VarHeaderSize)^.ch.h and uint32(HeapInc.VarSizeMask) - HeapInc.VarHeaderSize
  1688. else
  1689. result := HeapInc.pHugeChunk(p - (HeapInc.HugeChunkDataOffset + HeapInc.CommonHeaderSize))^.size - (HeapInc.HugeChunkDataOffset + HeapInc.CommonHeaderSize);
  1690. end;
  1691. function SysReAllocMem(var p: pointer; size: ptruint):pointer;
  1692. var
  1693. ts: HeapInc.pThreadState;
  1694. h: uint32;
  1695. oldOrCopySize: SizeUint;
  1696. newp: pointer;
  1697. begin
  1698. result := p; { Use as old p value until freed etc. }
  1699. if (size = 0) or not Assigned(result) then { Special cases; check at once. }
  1700. begin
  1701. if size = 0 then
  1702. begin
  1703. SysFreeMem(result);
  1704. result := nil;
  1705. end else
  1706. result := SysGetMem(size);
  1707. p := result;
  1708. exit;
  1709. end;
  1710. h := HeapInc.pCommonHeader(result - HeapInc.CommonHeaderSize)^.h;
  1711. if h and HeapInc.FixedFlag <> 0 then
  1712. begin
  1713. if (size <= HeapInc.MaxFixedHeaderAndPayload - HeapInc.CommonHeaderSize)
  1714. and (h and HeapInc.SizeIndexMask = HeapInc.SizeMinus1ToIndex(size + (HeapInc.CommonHeaderSize - 1))) then
  1715. exit;
  1716. end else
  1717. begin
  1718. ts := @HeapInc.thisTs;
  1719. {$ifdef FPC_HAS_FEATURE_THREADING}
  1720. if Assigned(ts^.toFree) then
  1721. ts^.FlushToFree;
  1722. {$endif FPC_HAS_FEATURE_THREADING}
  1723. if h <> HeapInc.HugeHeader then
  1724. newp := ts^.TryResizeVar(result, size)
  1725. else
  1726. newp := ts^.TryResizeHuge(result, size);
  1727. if Assigned(newp) then
  1728. begin
  1729. p := newp;
  1730. exit(newp);
  1731. end;
  1732. end;
  1733. { Generic fallback: GetMem + Move + FreeMem. }
  1734. oldOrCopySize := SysMemSize(result);
  1735. newp := SysGetMem(size);
  1736. if not Assigned(newp) then
  1737. begin
  1738. { Don’t fail if shrinking. If growing failed, return nil, but keep the old p. }
  1739. if size > oldOrCopySize then
  1740. result := nil;
  1741. exit;
  1742. end;
  1743. p := newp;
  1744. if oldOrCopySize > size then
  1745. oldOrCopySize := size;
  1746. Move(result^, newp^, oldOrCopySize);
  1747. SysFreeMem(result);
  1748. result := newp;
  1749. end;
  1750. Function SysFreeMemSize(p: pointer; size: ptruint):ptruint;
  1751. begin
  1752. { can't free partial blocks, ignore size }
  1753. result := SysFreeMem(p);
  1754. end;
  1755. function SysAllocMem(size: ptruint): pointer;
  1756. begin
  1757. result := SysGetMem(size);
  1758. if Assigned(result) then
  1759. FillChar(result^, SysMemSize(result), 0);
  1760. end;
  1761. {*****************************************************************************
  1762. InitHeap
  1763. *****************************************************************************}
  1764. {$ifdef FPC_HAS_FEATURE_THREADING}
  1765. {$ifdef SUPPORT_INIT_HEAP_PROCESS_WIDE}
  1766. { DeferInitHeapProcessWide / DoneHeapProcessWide are meant to support DLL_PROCESS_ATTACH / DLL_PROCESS_DETACH.
  1767. Otherwise InitHeapThread + FinalizeHeap called per thread do their best with refcounting... }
  1768. procedure DeferInitHeapProcessWide;
  1769. begin
  1770. HeapInc.gs.askedForProcessWideLockInitialization := true; { Called before InitSystemThreads, cannot initialize the lock... }
  1771. end;
  1772. procedure DoneHeapProcessWide;
  1773. {$ifdef HAS_SYSOSFREE}
  1774. var
  1775. thisTs, nextTs, ts: HeapInc.pThreadState;
  1776. stolenTf: HeapInc.pFreeChunk;
  1777. {$endif HAS_SYSOSFREE}
  1778. begin
  1779. if HeapInc.gs.lockUse <> HeapInc.gs.LockInitializedProcessWide then
  1780. exit;
  1781. {$ifdef HAS_SYSOSFREE}
  1782. { We need to free all lingering data of all threads: to-free lists, empty arenas, “freeOS1”s.
  1783. For each particular thread ts, this is what ts^.Orphan does, but ts^.Orphan can’t be (easily) called from another thread:
  1784. it calls ts^.FlushToFree which calls SysFreeMem which is hardcoded to work with HeapInc.thisTs.
  1785. It’s not worth redesigning, or the common case of SysFreeMem that must indeed work with HeapInc.thisTs will be slower.
  1786. So we steal and zero ts^.toFree (ts^.FlushToFree is the only thing that prevents ts^.Orphan from working from threads other than ts), call ts^.Orphan,
  1787. then manually complete toFree requests on our behalf.
  1788. This entire thing is just to handle the case of unloading a DLL before terminating the thread that used this DLL
  1789. (https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/1173). }
  1790. thisTs := @HeapInc.thisTs;
  1791. nextTs := HeapInc.gs.threads;
  1792. while Assigned(nextTs) do
  1793. begin
  1794. ts := nextTs;
  1795. nextTs := ts^.next;
  1796. if ts = thisTs then { Used for executing toFrees and is orphaned the last. }
  1797. continue;
  1798. stolenTf := ts^.toFree;
  1799. ts^.toFree := nil;
  1800. ts^.Orphan;
  1801. HeapInc.ThreadState.FreeToFreeList(stolenTf);
  1802. end;
  1803. thisTs^.Orphan;
  1804. {$endif HAS_SYSOSFREE}
  1805. HeapInc.gs.lockUse := 0;
  1806. DoneCriticalSection(HeapInc.gs.lock);
  1807. end;
  1808. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE}
  1809. { This function will initialize the Heap manager and need to be called from
  1810. the initialization of the system unit }
  1811. procedure InitHeapThread;
  1812. {$if defined(SUPPORT_INIT_HEAP_PROCESS_WIDE) and defined(HAS_SYSOSFREE)}
  1813. var
  1814. ts, next: HeapInc.pThreadState;
  1815. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE and HAS_SYSOSFREE}
  1816. begin
  1817. {$ifdef SUPPORT_INIT_HEAP_PROCESS_WIDE}
  1818. if (HeapInc.gs.lockUse = 0) and HeapInc.gs.askedForProcessWideLockInitialization then
  1819. begin
  1820. HeapInc.gs.lockUse := HeapInc.gs.LockInitializedProcessWide;
  1821. InitCriticalSection(HeapInc.gs.lock);
  1822. end else
  1823. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE}
  1824. if (HeapInc.gs.lockUse >= 0) and ({$ifdef VER3_2} InterlockedIncrement {$else} AtomicIncrement {$endif} (HeapInc.gs.lockUse) = 1) then
  1825. InitCriticalSection(HeapInc.gs.lock);
  1826. {$if defined(SUPPORT_INIT_HEAP_PROCESS_WIDE) and defined(HAS_SYSOSFREE)}
  1827. { Add to gs.threads. }
  1828. ts := @HeapInc.thisTs;
  1829. EnterCriticalSection(HeapInc.gs.lock);
  1830. next := HeapInc.gs.threads;
  1831. { Check if already in gs.threads; this function can in principle be called twice on the same thread (or at least I did call redundant InitThread for some time...). }
  1832. if not Assigned(ts^.prev) and (ts <> next) then
  1833. begin
  1834. ts^.next := next;
  1835. if Assigned(next) then
  1836. next^.prev := ts;
  1837. HeapInc.gs.threads := ts;
  1838. end;
  1839. LeaveCriticalSection(HeapInc.gs.lock);
  1840. {$endif SUPPORT_INIT_HEAP_PROCESS_WIDE and HAS_SYSOSFREE}
  1841. end;
  1842. {$endif FPC_HAS_FEATURE_THREADING}
  1843. procedure InitHeap; public name '_FPC_InitHeap';
  1844. begin
  1845. { we cannot initialize the locks here yet, thread support is
  1846. not loaded yet }
  1847. end;
  1848. procedure RelocateHeap;
  1849. begin
  1850. {$ifdef FPC_HAS_FEATURE_THREADING}
  1851. if HeapInc.gs.lockUse <> 0 then
  1852. exit;
  1853. InitHeapThread; { Initializes the lock and sets lockUse = 1 (or maybe LockInitializedProcessWide). }
  1854. {$ifndef FPC_SECTION_THREADVARS}
  1855. { threadState pointers still point to main thread's thisTs, but they
  1856. have a reference to the global main thisTs, fix them to point
  1857. to the main thread specific variable.
  1858. even if section threadvars are used, this shouldn't cause problems as threadState pointers simply
  1859. do not change but we do not need it }
  1860. HeapInc.thisTs.FixupSelfPtr;
  1861. {$endif FPC_SECTION_THREADVARS}
  1862. if MemoryManager.RelocateHeap <> nil then
  1863. MemoryManager.RelocateHeap();
  1864. {$endif FPC_HAS_FEATURE_THREADING}
  1865. end;
  1866. procedure FinalizeHeap;
  1867. begin
  1868. { Do not try to do anything if the heap manager already reported an error }
  1869. if (errorcode=203) or (errorcode=204) then
  1870. exit;
  1871. {$if defined(FPC_HAS_FEATURE_THREADING)}
  1872. HeapInc.thisTs.Orphan;
  1873. if (HeapInc.gs.lockUse > 0) and ({$ifdef VER3_2} InterlockedDecrement {$else} AtomicDecrement {$endif} (HeapInc.gs.lockUse) = 0) then
  1874. DoneCriticalSection(HeapInc.gs.lock);
  1875. {$elseif defined(HAS_SYSOSFREE)}
  1876. HeapInc.thisTs.ReplaceFreeOS1(nil);
  1877. {$endif FPC_HAS_FEATURE_THREADING | defined(HAS_SYSOSFREE)}
  1878. end;
  1879. {$endif ndef HAS_MEMORYMANAGER}
  1880. {$endif FPC_HAS_FEATURE_HEAP}