heap.inc 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  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. {$ifdef DEBUG_HEAP_INC}
  336. procedure Dump(var f: text);
  337. {$endif}
  338. function ChooseFixedArenaSize(sizeIndex: SizeUint): SizeUint;
  339. function AllocFixed(size: SizeUint): pointer; {$ifndef DEBUG_HEAP_INC} inline; {$endif}
  340. function FreeFixed(p: pointer): SizeUint; {$ifndef DEBUG_HEAP_INC} inline; {$endif}
  341. procedure FreeEmptyArenas;
  342. function GetOSChunk(minSize, maxSize: SizeUint): pOSChunk; {$if defined(HAS_SYSOSFREE) or not defined(FPC_HAS_FEATURE_THREADING)} inline; {$endif}
  343. function AllocateOSChunk(minSize, maxSize: SizeUint): pOSChunk;
  344. function AllocVar(size: SizeUint; isArena: boolean): pointer;
  345. function FreeVar(p: pointer): SizeUint;
  346. function TryResizeVar(p: pointer; size: SizeUint): pointer;
  347. class function AddToHugeUsed(delta: SizeInt): SizeUint; static;
  348. function AllocHuge(size: SizeUint): pointer;
  349. function FreeHuge(p: pointer): SizeUint;
  350. function TryResizeHuge(p: pointer; size: SizeUint): pointer;
  351. procedure UpdateMaxStats(hugeUsed: SizeUint);
  352. {$ifdef FPC_HAS_FEATURE_THREADING}
  353. procedure PushToFree(p: pFreeChunk);
  354. procedure FlushToFree;
  355. procedure Orphan;
  356. procedure AdoptArena(arena: pFixedArena);
  357. procedure AdoptVarOwner(p: pointer); { Adopts the OS chunk that contains p. Must be performed under gs.lock. }
  358. {$ifndef FPC_SECTION_THREADVARS}
  359. procedure FixupSelfPtr;
  360. {$endif ndef FPC_SECTION_THREADVARS}
  361. {$endif FPC_HAS_FEATURE_THREADING}
  362. end;
  363. GlobalState = record
  364. hugeUsed: SizeUint; { Same as non-existing “hugeAllocated” as huge chunks don’t have free space.
  365. Atomic, but can be read unprotected if unreliability is tolerable.
  366. Huge chunks don’t have thread affinity, so are tracked here. Presently, this value is added to all memory statistics.
  367. Not a good idea and makes multithreaded statistics a strange and unreliable mix, but alternatives are even worse. }
  368. {$ifdef FPC_HAS_FEATURE_THREADING}
  369. lock: TRTLCriticalSection;
  370. lockUse: int32;
  371. { Like ThreadState.varFree but over orphaned OS chunks. Protected by gs.lock. }
  372. varFree: VarFreeMap;
  373. {$ifndef HAS_SYSOSFREE}
  374. freeOS: FreeOSChunkList;
  375. {$endif not HAS_SYSOSFREE}
  376. {$endif FPC_HAS_FEATURE_THREADING}
  377. end;
  378. class function AllocFailed: pointer; static;
  379. class var
  380. gs: GlobalState;
  381. {$ifdef FPC_HAS_FEATURE_THREADING}
  382. class threadvar
  383. {$endif FPC_HAS_FEATURE_THREADING}
  384. thisTs: ThreadState;
  385. const
  386. CommonHeaderSize = sizeof(CommonHeader);
  387. {$if MinFixedHeaderAndPayload < CommonHeaderSize + sizeof(FreeChunk)} {$error MinFixedHeaderAndPayload does not fit CommonHeader + FreeChunk.} {$endif}
  388. FixedArenaDataOffset = (sizeof(FixedArena) + CommonHeaderSize + Alignment - 1) and -Alignment - CommonHeaderSize;
  389. VarHeaderSize = sizeof(VarHeader);
  390. FreeVarTailSize = sizeof(FreeVarTail);
  391. VarOSChunkDataOffset = (sizeof(VarOSChunk) + VarHeaderSize + Alignment - 1) and -Alignment - VarHeaderSize;
  392. HugeChunkDataOffset = (sizeof(HugeChunk) + CommonHeaderSize + Alignment - 1) and -Alignment - CommonHeaderSize;
  393. MinAnyVarHeaderAndPayload = (sizeof(VarHeader) + sizeof(FreeVarChunk) + sizeof(FreeVarTail) + VarSizeQuant - 1) and -VarSizeQuant;
  394. end;
  395. class function HeapInc.SizeMinus1ToIndex(sizeMinus1: SizeUint): SizeUint;
  396. begin
  397. result := SizeMinus1Div16ToIndex[sizeMinus1 div 16];
  398. end;
  399. class function HeapInc.IndexToSize(sizeIndex: SizeUint): SizeUint;
  400. begin
  401. result := FixedSizes[sizeIndex];
  402. end;
  403. class function HeapInc.VarSizeToBinIndex(size: SizeUint; roundUp: boolean): SizeUint;
  404. var
  405. binClassIndex: SizeUint;
  406. begin
  407. { 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.
  408. 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,
  409. but the rest, in particular applying roundUp, is magic that is somehow equivalent to the original but uses 1/3 of its instructions. }
  410. inc(size, 1 shl FirstVarRangeP2 - MaxFixedHeaderAndPayload - ord(roundUp));
  411. binClassIndex := BsrDWord(uint32(size) or 1); { Off by +FirstVarRangeP2. “or 1” is not required logically, just triggers node_not_zero optimization. }
  412. result := binClassIndex * VarSizesPerClass + size shr (binClassIndex - (FirstVarRangeP2 - FirstVarStepP2)) - (FirstVarRangeP2 * VarSizesPerClass + 1 shl (FirstVarRangeP2 - FirstVarStepP2) + ord(not roundUp));
  413. end;
  414. class function HeapInc.BinIndexToVarSize(binIndex: SizeUint): SizeUint;
  415. begin
  416. result := binIndex div VarSizesPerClass;
  417. result := MaxFixedHeaderAndPayload + (SizeUint(1) shl result - 1) shl FirstVarRangeP2 + (1 + binIndex mod VarSizesPerClass) shl (FirstVarStepP2 + result);
  418. end;
  419. {$ifndef HAS_SYSOSFREE}
  420. function HeapInc.FreeOSChunkList.Get(minSize, maxSize: SizeUint): pOSChunk;
  421. var
  422. prev, next: pFreeOSChunk;
  423. begin
  424. result := first;
  425. while Assigned(result) and not ((result^.size >= minSize) and (result^.size <= maxSize)) do
  426. result := result^.next;
  427. if not Assigned(result) then
  428. exit;
  429. prev := result^.prev;
  430. next := result^.next;
  431. if Assigned(prev) then
  432. prev^.next := next
  433. else
  434. first := next;
  435. if Assigned(next) then
  436. next^.prev := prev
  437. else
  438. last := prev;
  439. end;
  440. {$endif not HAS_SYSOSFREE}
  441. procedure HeapInc.VarFreeMap.Add(c: pFreeVarChunk; binIndex: SizeUint);
  442. var
  443. next: pFreeVarChunk;
  444. iL0: SizeUint;
  445. vL0 {$ifdef HEAP_INC_USE_SETS}, vL1 {$endif}: uint32;
  446. begin
  447. if binIndex >= VarSizesCount then
  448. if binIndex >= MaxVarHeaderAndPayload then { Large sizes go to the last bin, assuming searches never search for more than MaxVarHeaderAndPayload. }
  449. binIndex := VarSizesCount - 1
  450. else
  451. binIndex := VarSizeToBinIndex(binIndex, false);
  452. next := bins[binIndex];
  453. c^.prev := nil;
  454. c^.next := next;
  455. c^.binIndex := binIndex;
  456. bins[binIndex] := c;
  457. if Assigned(next) then
  458. next^.prev := c
  459. else
  460. begin
  461. iL0 := binIndex div L0BinSize;
  462. vL0 := L0[iL0];
  463. {$ifdef HEAP_INC_USE_SETS}
  464. if vL0 = 0 then
  465. begin
  466. vL1 := L1;
  467. Include(Set32(vL1), iL0);
  468. L1 := vL1;
  469. end;
  470. Include(Set32(vL0), binIndex mod L0BinSize);
  471. L0[iL0] := vL0;
  472. {$else}
  473. if vL0 = 0 then
  474. L1 := L1 or uint32(1) shl iL0;
  475. L0[iL0] := vL0 or uint32(1) shl (binIndex mod L0BinSize);
  476. {$endif}
  477. end;
  478. end;
  479. procedure HeapInc.VarFreeMap.Remove(c: pFreeVarChunk);
  480. var
  481. prev, next: pFreeVarChunk;
  482. binIndex, iL0: SizeUint;
  483. v: uint32;
  484. begin
  485. prev := c^.prev;
  486. next := c^.next;
  487. if Assigned(next) then
  488. next^.prev := prev;
  489. if Assigned(prev) then
  490. prev^.next := next
  491. else
  492. begin
  493. binIndex := c^.binIndex;
  494. bins[binIndex] := next;
  495. if not Assigned(next) then
  496. begin
  497. iL0 := binIndex div L0BinSize;
  498. {$ifdef HEAP_INC_USE_SETS}
  499. v := L0[iL0];
  500. Exclude(Set32(v), binIndex mod L0BinSize);
  501. L0[iL0] := v;
  502. if v = 0 then
  503. begin
  504. v := L1;
  505. Exclude(Set32(v), iL0);
  506. L1 := v;
  507. end;
  508. {$else}
  509. v := L0[iL0] xor (uint32(1) shl (binIndex mod L0BinSize));
  510. L0[iL0] := v;
  511. if v = 0 then
  512. L1 := L1 xor (uint32(1) shl iL0);
  513. {$endif}
  514. end;
  515. end;
  516. end;
  517. function HeapInc.VarFreeMap.Find(binIndex: SizeUint): pFreeVarChunk;
  518. var
  519. mask: uint32;
  520. begin
  521. result := bins[binIndex];
  522. if Assigned(result) then
  523. exit;
  524. 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. }
  525. if mask <> 0 then
  526. 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. }
  527. mask := L1 and (SizeUint(-2) shl (binIndex div L0BinSize));
  528. if mask <> 0 then
  529. begin
  530. binIndex := BsfDWord(mask or 1 shl (L0BinSize - 1)); { Index at L0. }
  531. 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]. }
  532. end;
  533. end;
  534. function HeapInc.VarFreeMap.FindSmaller(binIndex: SizeUint): pFreeVarChunk;
  535. var
  536. mask: uint32;
  537. begin
  538. mask := L0[binIndex div L0BinSize] and (uint32(1) shl (binIndex mod L0BinSize) - 1);
  539. if mask <> 0 then
  540. exit(bins[binIndex and SizeUint(-L0BinSize) + BsrDWord(mask or 1)]);
  541. result := nil;
  542. mask := L1 and (uint32(1) shl (binIndex div L0BinSize) - 1);
  543. if mask <> 0 then
  544. begin
  545. binIndex := BsrDWord(mask or 1);
  546. result := bins[binIndex * L0BinSize + BsrDWord(L0[binIndex] or 1)];
  547. end;
  548. end;
  549. {$ifdef DEBUG_HEAP_INC}
  550. procedure HeapInc.ThreadState.Dump(var f: text);
  551. var
  552. i: SizeInt;
  553. fix: pFixedArena;
  554. fr: pFreeOSChunk;
  555. {$ifdef FPC_HAS_FEATURE_THREADING}
  556. tf: pFreeChunk;
  557. {$endif}
  558. vf: pFreeVarChunk;
  559. vOs: pVarOSChunk;
  560. p: pointer;
  561. needLE, anything: boolean;
  562. procedure MaybeLE;
  563. begin
  564. if needLE then
  565. writeln(f);
  566. needLE := false;
  567. end;
  568. procedure DumpVarFree(const varFree: VarFreeMap; const name: shortstring);
  569. var
  570. i: SizeInt;
  571. begin
  572. if varFree.L1 = 0 then
  573. exit;
  574. MaybeLE;
  575. write(f, name, LineEnding, 'L1:');
  576. for i := 0 to VarSizesCount div L0BinSize - 1 do
  577. if varFree.L1 shr i and 1 <> 0 then
  578. begin
  579. write(f, ' #', i, ' ', BinIndexToVarSize(i * L0BinSize), '-');
  580. if i = VarSizesCount div L0BinSize - 1 then
  581. write(f, 'inf')
  582. else
  583. write(f, BinIndexToVarSize((i + 1) * L0BinSize) - 1);
  584. end;
  585. writeln(f);
  586. write(f, 'L0 (bins):');
  587. for i := 0 to VarSizesCount - 1 do
  588. begin
  589. if varFree.L0[SizeUint(i) div L0BinSize] shr (SizeUint(i) mod L0BinSize) and 1 <> 0 then
  590. begin
  591. write(f, ' #', i, ' ', BinIndexToVarSize(i), '-');
  592. if i = VarSizesCount - 1 then
  593. write(f, 'inf')
  594. else
  595. write(f, BinIndexToVarSize(i + 1) - 1);
  596. end;
  597. if Assigned(varFree.bins[i]) then
  598. begin
  599. write(f, ' (');
  600. vf := varFree.bins[i];
  601. repeat
  602. if Assigned(vf^.prev) then write(f, ' ');
  603. write(f, HexStr(PtrUint(vf), 1 + BsrQWord(PtrUint(vf)) div 4), ':', pVarHeader(vf)[-1].ch.h and VarSizeMask);
  604. vf := vf^.next;
  605. until not Assigned(vf);
  606. write(f, ')');
  607. end;
  608. end;
  609. writeln(f);
  610. needLE := true;
  611. end;
  612. begin
  613. writeln(f, 'used = ', used, ', allocated = ', allocated, ', hugeUsed = ', gs.hugeUsed, ', maxUsed = ', maxUsed, ', maxAllocated = ', maxAllocated);
  614. needLE := true;
  615. anything := false;
  616. for i := 0 to FixedSizesCount - 1 do
  617. begin
  618. if not Assigned(partialArenas[i]) and (allocatedByFullArenas[i] = 0) then
  619. continue;
  620. MaybeLE;
  621. anything := true;
  622. write(f, 'Size #', i, ' (', IndexToSize(i), '):');
  623. if allocatedByFullArenas[i] <> 0 then
  624. write(f, ' allocatedByFullArenas = ', allocatedByFullArenas[i]);
  625. if Assigned(partialArenas[i]) then
  626. begin
  627. writeln(f);
  628. fix := partialArenas[i];
  629. repeat
  630. writeln(f, 'arena size = ', pVarHeader(fix)[-1].ch.h and VarSizeMask - VarHeaderSize - FixedArenaDataOffset, ', usedSizeMinus1 = ', fix^.usedSizeMinus1, ', almostFullThreshold = ', fix^.almostFullThreshold);
  631. fix := fix^.next;
  632. until not Assigned(fix);
  633. end
  634. else if allocatedByFullArenas[i] <> 0 then
  635. writeln(f);
  636. end;
  637. needLE := needLE or anything;
  638. if nEmptyArenas <> 0 then
  639. begin
  640. MaybeLE;
  641. writeln(f, 'nEmptyArenas = ', nEmptyArenas);
  642. needLE := true;
  643. end;
  644. vOs := varOS;
  645. while Assigned(vOs) do
  646. begin
  647. MaybeLE;
  648. writeln(f, 'Var OS chunk, size ', vOs^.size);
  649. p := pointer(vOs) + (VarOSChunkDataOffset + VarHeaderSize);
  650. repeat
  651. write(f, HexStr(p), ': size = ', pVarHeader(p - VarHeaderSize)^.ch.h and VarSizeMask, ', ofsToOs = ', pVarHeader(p - VarHeaderSize)^.ofsToOs);
  652. if pVarHeader(p - VarHeaderSize)^.ch.h and UsedFlag <> 0 then
  653. write(f, ', used')
  654. else
  655. begin
  656. write(f, ', f r e e');
  657. if pVarHeader(p - VarHeaderSize)^.ch.h and LastFlag = 0 then
  658. write(f, ' (tail ', pFreeVarTail(p + pVarHeader(p - VarHeaderSize)^.ch.h - VarHeaderSize - FreeVarTailSize)^.size, ')');
  659. end;
  660. if pVarHeader(p - VarHeaderSize)^.ch.h and LastFlag <> 0 then
  661. write(f, ', last');
  662. if pVarHeader(p - VarHeaderSize)^.ch.h and PrevIsFreeFlag <> 0 then
  663. write(f, ', prev. is free');
  664. if pVarHeader(p - VarHeaderSize)^.ch.h and FixedArenaFlag <> 0 then
  665. write(f, ', fixed arena');
  666. writeln(f);
  667. if pVarHeader(p - VarHeaderSize)^.ch.h and LastFlag <> 0 then
  668. break;
  669. p := p + pVarHeader(p - VarHeaderSize)^.ch.h and VarSizeMask;
  670. until false;
  671. needLE := true;
  672. vOs := vOs^.next;
  673. end;
  674. fr := {$ifdef HAS_SYSOSFREE} freeOS1 {$else} freeOS.first {$endif};
  675. if Assigned(fr) then
  676. begin
  677. MaybeLE;
  678. repeat
  679. writeln(f, 'Free OS: ', HexStr(fr), ', size = ', fr^.size);
  680. {$ifndef HAS_SYSOSFREE} fr := fr^.next; {$endif}
  681. until {$ifdef HAS_SYSOSFREE} true {$else} not Assigned(fr) {$endif};
  682. needLE := true;
  683. end;
  684. DumpVarFree(varFree, 'varFree');
  685. {$ifdef FPC_HAS_FEATURE_THREADING}
  686. DumpVarFree(gs.varFree, 'Orphaned varFree');
  687. tf := toFree;
  688. if Assigned(tf) then
  689. begin
  690. MaybeLE;
  691. write(f, 'To-free:');
  692. repeat
  693. if pCommonHeader(pointer(tf) - CommonHeaderSize)^.h and FixedFlag <> 0 then
  694. write(f, ' f ', CommonHeaderSize + SysMemSize(tf))
  695. else
  696. write(f, ' v ', VarHeaderSize + SysMemSize(tf));
  697. tf := tf^.next;
  698. until not Assigned(tf);
  699. writeln(f);
  700. end;
  701. {$endif FPC_HAS_FEATURE_THREADING}
  702. end;
  703. {$endif DEBUG_HEAP_INC}
  704. function HeapInc.ThreadState.ChooseFixedArenaSize(sizeIndex: SizeUint): SizeUint;
  705. begin
  706. result := (allocatedByFullArenas[sizeIndex] div 8 + (FixedArenaSizeQuant - 1)) and SizeUint(-FixedArenaSizeQuant); { 12.5% of memory allocated by the size. }
  707. if result < MinFixedArenaSize then
  708. result := MinFixedArenaSize;
  709. if result > MaxFixedArenaSize then
  710. result := MaxFixedArenaSize;
  711. dec(result, result shr (FirstVarRangeP2 - FirstVarStepP2)); { Prettier fit into OS chunks. }
  712. end;
  713. function HeapInc.ThreadState.AllocFixed(size: SizeUint): pointer;
  714. var
  715. sizeIndex, sizeUp, statv: SizeUint;
  716. usedSizeMinus1: int32;
  717. arena, nextArena: pFixedArena;
  718. begin
  719. sizeIndex := SizeMinus1ToIndex(size + (CommonHeaderSize - 1));
  720. arena := partialArenas[sizeIndex];
  721. if not Assigned(arena) then
  722. begin
  723. {$ifdef FPC_HAS_FEATURE_THREADING}
  724. if Assigned(toFree) then
  725. begin
  726. FlushToFree;
  727. arena := partialArenas[sizeIndex];
  728. end;
  729. if not Assigned(arena) then
  730. {$endif FPC_HAS_FEATURE_THREADING}
  731. begin
  732. arena := emptyArenas;
  733. if Assigned(arena) then
  734. begin
  735. emptyArenas := arena^.next;
  736. dec(nEmptyArenas);
  737. end else
  738. begin
  739. arena := AllocVar(ChooseFixedArenaSize(sizeIndex), true);
  740. if not Assigned(arena) then
  741. exit(nil);
  742. { 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. }
  743. pCommonHeader(pointer(arena) + FixedArenaDataOffset)^.h := uint32(not sizeIndex);
  744. end;
  745. if pCommonHeader(pointer(arena) + FixedArenaDataOffset)^.h and SizeIndexMask = uint32(sizeIndex) then
  746. { Lucky! Just don’t reset the chunk and use its old freelist. }
  747. else
  748. begin
  749. arena^.firstFreeChunk := nil;
  750. arena^.usedSizeMinus1 := uint32(-1);
  751. arena^.almostFullThreshold := pVarHeader(arena)[-1].ch.h and VarSizeMask - 2 * IndexToSize(sizeIndex) - (VarHeaderSize + FixedArenaDataOffset); { available space - 2 * chunk size. }
  752. end;
  753. { Add arena to partialArenas[sizeIndex], which is nil. Careful: AllocVar above should not call FlushToFree, or this assumption might be violated. }
  754. arena^.prev := nil;
  755. arena^.next := nil;
  756. partialArenas[sizeIndex] := arena;
  757. end;
  758. end;
  759. sizeUp := IndexToSize(sizeIndex); { Not reusing the “size” variable saved a register at the time of writing this comment. }
  760. inc(used, sizeUp);
  761. { arena from partialArenas has either free chunk or free unformatted space for a new chunk. }
  762. usedSizeMinus1 := int32(arena^.usedSizeMinus1);
  763. arena^.usedSizeMinus1 := uint32(usedSizeMinus1 + int32(sizeUp));
  764. result := arena^.firstFreeChunk;
  765. if Assigned(result) then
  766. begin
  767. { This branch is much more likely (when compiling FPC: 9×), so comes first. }
  768. arena^.firstFreeChunk := pFreeChunk(result)^.next;
  769. if usedSizeMinus1 < int32(arena^.almostFullThreshold) then { Arena is still not full? Uses usedSizeMinus1 value before adding sizeUp, as assumed by almostFullThreshold. }
  770. exit;
  771. end else
  772. begin
  773. { Freelist is empty, so “formattedSize” = usedSizeMinus1 + 1. This “+ 1” is folded into constants. }
  774. result := pointer(arena) + (FixedArenaDataOffset + CommonHeaderSize + 1) + usedSizeMinus1;
  775. pCommonHeader(result - CommonHeadersize)^.h := uint32(int32(sizeIndex) + int32(usedSizeMinus1 shl FixedArenaOffsetShift) +
  776. (FixedFlag + (FixedArenaDataOffset + CommonHeaderSize + 1) shl FixedArenaOffsetShift) { ← const });
  777. if usedSizeMinus1 < int32(arena^.almostFullThreshold) then { Arena is still not full? }
  778. exit;
  779. end;
  780. { 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.) }
  781. inc(allocatedByFullArenas[sizeIndex], pVarHeader(arena)[-1].ch.h and VarSizeMask);
  782. { Remove arena from partialArenas[sizeIndex]. (It was first.) }
  783. nextArena := arena^.next;
  784. partialArenas[sizeIndex] := nextArena;
  785. if Assigned(nextArena) then
  786. nextArena^.prev := nil;
  787. { And since this is unlikely, it won’t hurt to update maxUsed (unlike doing it in the common path). }
  788. statv := used + gs.hugeUsed;
  789. if statv > maxUsed then
  790. maxUsed := statv;
  791. end;
  792. function HeapInc.ThreadState.FreeFixed(p: pointer): SizeUint;
  793. var
  794. sizeIndex: SizeUint;
  795. usedSizeMinus1: int32;
  796. arena, prevArena, nextArena: pFixedArena;
  797. {$ifdef FPC_HAS_FEATURE_THREADING}
  798. ts: pThreadState;
  799. {$endif FPC_HAS_FEATURE_THREADING}
  800. begin
  801. arena := p - pCommonHeader(p - CommonHeaderSize)^.h shr FixedArenaOffsetShift;
  802. {$ifdef FPC_HAS_FEATURE_THREADING}
  803. { 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. }
  804. if pVarOSChunk(pointer(arena) + pVarHeader(arena)[-1].ofsToOs)^.threadState <> @self then
  805. begin
  806. EnterCriticalSection(gs.lock);
  807. ts := pVarOSChunk(pointer(arena) + pVarHeader(arena)[-1].ofsToOs)^.threadState;
  808. if Assigned(ts) then
  809. begin
  810. { Despite atomic Push lock must be held as otherwise target thread might end and destroy its threadState.
  811. However, target thread won’t block to free p, so PushToFree instantly invalidates p. }
  812. result := IndexToSize(pCommonHeader(p - CommonHeaderSize)^.h and SizeIndexMask) - CommonHeaderSize;
  813. ts^.PushToFree(p);
  814. LeaveCriticalSection(gs.lock);
  815. exit;
  816. end;
  817. AdoptVarOwner(arena); { ...And continue! }
  818. LeaveCriticalSection(gs.lock);
  819. end;
  820. {$endif FPC_HAS_FEATURE_THREADING}
  821. pFreeChunk(p)^.next := arena^.firstFreeChunk;
  822. arena^.firstFreeChunk := p;
  823. sizeIndex := pCommonHeader(p - CommonHeaderSize)^.h and SizeIndexMask;
  824. result := IndexToSize(sizeIndex);
  825. dec(used, result);
  826. usedSizeMinus1 := int32(arena^.usedSizeMinus1) - int32(result);
  827. arena^.usedSizeMinus1 := uint32(usedSizeMinus1);
  828. dec(result, CommonHeaderSize);
  829. { “(usedSizeMinus1 = -1) or (usedSizeMinus1 >= arena^.almostFullThreshold)” as 1 comparison. }
  830. if uint32(usedSizeMinus1) >= arena^.almostFullThreshold then
  831. if usedSizeMinus1 <> -1 then
  832. begin
  833. dec(allocatedByFullArenas[sizeIndex], pVarHeader(arena)[-1].ch.h and VarSizeMask);
  834. { Add arena to partialArenas[sizeIndex]. }
  835. nextArena := partialArenas[sizeIndex];
  836. arena^.prev := nil;
  837. arena^.next := nextArena;
  838. if Assigned(nextArena) then
  839. nextArena^.prev := arena;
  840. partialArenas[sizeIndex] := arena;
  841. end else
  842. begin
  843. { Remove arena from partialArenas[sizeIndex], add to emptyArenas (maybe). }
  844. prevArena := arena^.prev;
  845. nextArena := arena^.next;
  846. if Assigned(prevArena) then
  847. prevArena^.next := nextArena
  848. else
  849. partialArenas[sizeIndex] := nextArena;
  850. if Assigned(nextArena) then
  851. nextArena^.prev := prevArena;
  852. if nEmptyArenas < MaxKeptFixedArenas then
  853. begin
  854. arena^.next := emptyArenas;
  855. emptyArenas := arena;
  856. inc(nEmptyArenas);
  857. end else
  858. FreeVar(arena);
  859. end;
  860. end;
  861. procedure HeapInc.ThreadState.FreeEmptyArenas;
  862. var
  863. arena: pFixedArena;
  864. begin
  865. while nEmptyArenas > 0 do
  866. begin
  867. arena := emptyArenas;
  868. emptyArenas := arena^.next;
  869. dec(nEmptyArenas);
  870. FreeVar(arena);
  871. end;
  872. end;
  873. function HeapInc.ThreadState.GetOSChunk(minSize, maxSize: SizeUint): pOSChunk;
  874. {$if defined(FPC_HAS_FEATURE_THREADING) and not defined(HAS_SYSOSFREE)}
  875. var
  876. statv: SizeUint;
  877. {$endif FPC_HAS_FEATURE_THREADING and not HAS_SYSOSFREE}
  878. begin
  879. {$ifdef HAS_SYSOSFREE}
  880. result := freeOS1;
  881. if Assigned(result) then
  882. if (result^.size >= minSize) and (result^.size <= maxSize) then
  883. freeOS1 := nil
  884. else
  885. result := nil;
  886. {$else HAS_SYSOSFREE}
  887. result := freeOS.Get(minSize, maxSize);
  888. {$ifdef FPC_HAS_FEATURE_THREADING}
  889. if not Assigned(result) and Assigned(gs.freeOS.first) then { Racing precheck. }
  890. begin
  891. EnterCriticalSection(gs.lock);
  892. result := gs.freeOS.Get(minSize, maxSize);
  893. LeaveCriticalSection(gs.lock);
  894. if Assigned(result) then
  895. begin
  896. statv := allocated + result^.size;
  897. allocated := statv;
  898. inc(statv, gs.hugeUsed);
  899. if statv > maxAllocated then
  900. maxAllocated := statv;
  901. end;
  902. end;
  903. {$endif FPC_HAS_FEATURE_THREADING}
  904. {$endif HAS_SYSOSFREE}
  905. end;
  906. function HeapInc.ThreadState.AllocateOSChunk(minSize, maxSize: SizeUint): pOSChunk;
  907. var
  908. query, statv: SizeUint;
  909. begin
  910. 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. }
  911. if query > maxSize then { Limit by maxSize (usually GrowHeapSize2). }
  912. query := maxSize;
  913. if query < minSize then { But of course allocate at least the amount requested. Also triggers if maxSize was wrong (smaller than minSize). }
  914. query := minSize;
  915. query := (query + (OSChunkVarSizeQuant - 1)) and SizeUint(-OSChunkVarSizeQuant); { Quantize. }
  916. result := SysOSAlloc(query);
  917. if not Assigned(result) and (query > minSize) then
  918. begin
  919. query := minSize;
  920. result := SysOSAlloc(query);
  921. end;
  922. if not Assigned(result) then
  923. exit(AllocFailed);
  924. result^.size := query;
  925. statv := allocated + query;
  926. allocated := statv;
  927. inc(statv, gs.hugeUsed);
  928. if statv > maxAllocated then
  929. maxAllocated := statv;
  930. end;
  931. function HeapInc.ThreadState.AllocVar(size: SizeUint; isArena: boolean): pointer;
  932. var
  933. fv: pFreeVarChunk absolute result;
  934. tailFv: pFreeVarChunk;
  935. osChunk, osNext: pVarOSChunk;
  936. binIndex, vSizeFlags, statv: SizeUint;
  937. begin
  938. { Search varFree for (roughly) smallest chunk ≥ size. }
  939. binIndex := VarSizeToBinIndex(size + VarHeaderSize, true);
  940. { Round the size up to the bin size.
  941. 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. }
  942. size := BinIndexToVarSize(binIndex);
  943. repeat { break = found fv or osChunk. }
  944. fv := varFree.Find(binIndex);
  945. if Assigned(fv) then
  946. break;
  947. { If allocating arena, try to allocate less than requested, within arena size limitations. }
  948. if isArena and (binIndex > MinArenaBinIndex) then
  949. begin
  950. fv := varFree.FindSmaller(binIndex);
  951. if Assigned(fv) and (fv^.binIndex >= MinArenaBinIndex) then
  952. begin
  953. size := pVarHeader(fv)[-1].ch.h and VarSizeMask; { Use the entire chunk. }
  954. break;
  955. end;
  956. fv := nil;
  957. end;
  958. { Try reusing empty OS chunk. }
  959. osChunk := pVarOSChunk(GetOSChunk(VarOSChunkDataOffset + size, GrowHeapSize2));
  960. if Assigned(osChunk) then
  961. break;
  962. { If there are empty arenas, free them and retry. }
  963. if nEmptyArenas > 0 then
  964. begin
  965. FreeEmptyArenas;
  966. continue;
  967. end;
  968. {$ifdef FPC_HAS_FEATURE_THREADING}
  969. { Try reusing an orphaned chunk. }
  970. fv := gs.varFree.Find(binIndex); { Preliminary search without blocking, assuming varFree.Find doesn’t do anything that can go wrong. }
  971. if Assigned(fv) then
  972. begin
  973. EnterCriticalSection(gs.lock);
  974. fv := gs.varFree.Find(binIndex); { True search. }
  975. if Assigned(fv) then
  976. AdoptVarOwner(fv); { Moves fv to own varFree. }
  977. LeaveCriticalSection(gs.lock);
  978. if Assigned(fv) then
  979. break;
  980. end;
  981. {$endif FPC_HAS_FEATURE_THREADING}
  982. osChunk := pVarOSChunk(AllocateOSChunk(VarOSChunkDataOffset + size, GrowHeapSize2));
  983. if Assigned(osChunk) then
  984. break;
  985. exit; { (nil) as fv is nil and mapped to result. }
  986. until false;
  987. if not Assigned(fv) then
  988. begin
  989. {$ifdef FPC_HAS_FEATURE_THREADING}
  990. osChunk^.threadState := @self;
  991. {$endif}
  992. { Add osChunk to varOS. }
  993. osNext := varOS;
  994. osChunk^.prev := nil;
  995. osChunk^.next := osNext;
  996. if Assigned(osNext) then
  997. osNext^.prev := osChunk;
  998. varOS := osChunk;
  999. { Format new free var chunk spanning the entire osChunk. FreeVarTail is not required. }
  1000. fv := pointer(osChunk) + (VarOSChunkDataOffset + VarHeaderSize);
  1001. pVarHeader(result - VarHeaderSize)^.ofsToOs := -(VarOSChunkDataOffset + VarHeaderSize);
  1002. pVarHeader(result - VarHeaderSize)^.ch.h := (uint32(osChunk^.size) - VarOSChunkDataOffset) and VarSizeMask + LastFlag;
  1003. end else
  1004. varFree.Remove(fv);
  1005. { Result will be allocated at the beginning of fv; maybe format the remainder and add it back to varFree. }
  1006. vSizeFlags := pVarHeader(fv)[-1].ch.h - size; { Inherits LastFlag. }
  1007. if
  1008. { Allow over-allocating arenas by up to MaxArenaOverallocation.
  1009. Harmless check if not an arena, assuming (MaxArenaOverallocation and -VarSizeQuant + VarSizeQuant) >= MinSearchableVarHeaderAndPayload. }
  1010. (vSizeFlags >= MaxArenaOverallocation and -VarSizeQuant + VarSizeQuant) or
  1011. { Allow leaving a non-searchable tail if non-last.
  1012. “vSizeFlags >= MinAnyVarHeaderAndPayload” if non-last, “vSizeFlags >= MinSearchableVarHeaderAndPayload” if last. }
  1013. not isArena and (vSizeFlags >= MinAnyVarHeaderAndPayload + (MinSearchableVarHeaderAndPayload - MinAnyVarHeaderAndPayload) div LastFlag * (vSizeFlags and LastFlag)) then
  1014. begin
  1015. pVarHeader(result - VarHeaderSize)^.ch.h := uint32(size) + UsedFlag;
  1016. tailFv := result + size; { fv (result) = allocated block, tailFv = remainder. }
  1017. pVarHeader(pointer(tailFv) - VarHeaderSize)^.ofsToOs := pVarHeader(result - VarHeaderSize)^.ofsToOs - int32(size);
  1018. pVarHeader(pointer(tailFv) - VarHeaderSize)^.ch.h := vSizeFlags;
  1019. { Chunk to the right retains its PrevFreeFlag. }
  1020. if vSizeFlags and LastFlag = 0 then
  1021. pFreeVarTail(pointer(tailFv) + vSizeFlags - (VarHeaderSize + FreeVarTailSize))^.size := vSizeFlags;
  1022. if vSizeFlags >= MinSearchableVarHeaderAndPayload then
  1023. varFree.Add(tailFv, vSizeFlags); { Rounding down, so not masking is ok. }
  1024. end else
  1025. begin
  1026. { Use the entire chunk. }
  1027. inc(vSizeFlags, size);
  1028. pVarHeader(result - VarHeaderSize)^.ch.h := uint32(vSizeFlags) + UsedFlag;
  1029. if vSizeFlags and LastFlag = 0 then
  1030. dec(pVarHeader(result + vSizeFlags - VarHeaderSize)^.ch.h, PrevIsFreeFlag);
  1031. size := vSizeFlags and VarSizeMask;
  1032. end;
  1033. if isArena then
  1034. inc(pVarHeader(result)[-1].ch.h, FixedArenaFlag) { Arenas aren’t counted in “used” directly. }
  1035. else
  1036. inc(used, size);
  1037. { Update maxUsed regardless. }
  1038. statv := used + gs.hugeUsed;
  1039. if statv > maxUsed then
  1040. maxUsed := statv;
  1041. end;
  1042. function HeapInc.ThreadState.FreeVar(p: pointer): SizeUint;
  1043. var
  1044. fSizeFlags, hNext, prevSize: SizeUint;
  1045. osChunk, osPrev, osNext: pVarOSChunk;
  1046. {$ifdef FPC_HAS_FEATURE_THREADING}
  1047. pts: ^pThreadState;
  1048. {$endif FPC_HAS_FEATURE_THREADING}
  1049. {$ifndef HAS_SYSOSFREE}
  1050. freeOsNext: pFreeOSChunk;
  1051. fOs: ^FreeOSChunkList;
  1052. {$endif not HAS_SYSOSFREE}
  1053. begin
  1054. {$ifdef FPC_HAS_FEATURE_THREADING}
  1055. pts := @pVarOSChunk(p + pVarHeader(p - VarHeaderSize)^.ofsToOs)^.threadState;
  1056. if pts^ <> @self then
  1057. begin
  1058. EnterCriticalSection(gs.lock);
  1059. if Assigned(pts^) then
  1060. begin
  1061. { Despite atomic Push lock must be held as otherwise target thread might end and destroy its threadState.
  1062. However, target thread won’t block to free p, so PushToFree instantly invalidates p. }
  1063. result := pVarHeader(p - VarHeaderSize)^.ch.h and VarSizeMask - VarHeaderSize;
  1064. pts^^.PushToFree(p);
  1065. LeaveCriticalSection(gs.lock);
  1066. exit;
  1067. end;
  1068. AdoptVarOwner(p); { ...And continue! }
  1069. LeaveCriticalSection(gs.lock);
  1070. end;
  1071. {$endif FPC_HAS_FEATURE_THREADING}
  1072. fSizeFlags := pVarHeader(p - VarHeaderSize)^.ch.h;
  1073. result := fSizeFlags and VarSizeMask;
  1074. if fSizeFlags and FixedArenaFlag = 0 then
  1075. dec(used, result)
  1076. else
  1077. dec(fSizeFlags, FixedArenaFlag);
  1078. { 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. }
  1079. if fSizeFlags and LastFlag = 0 then
  1080. begin
  1081. hNext := pVarHeader(p + result - VarHeaderSize)^.ch.h;
  1082. if uint32(hNext) and UsedFlag = 0 then
  1083. begin
  1084. inc(fSizeFlags, hNext); { Inherit LastFlag, other p2 flags must be 0. }
  1085. if hNext >= MinSearchableVarHeaderAndPayload then { Logically “hNext and VarSizeMask”. }
  1086. varFree.Remove(p + result);
  1087. { Chunk to the right retains its PrevFreeFlag. }
  1088. end;
  1089. end;
  1090. if fSizeFlags and PrevIsFreeFlag <> 0 then
  1091. begin
  1092. prevSize := pFreeVarTail(p - (VarHeaderSize + FreeVarTailSize))^.size;
  1093. dec(p, prevSize);
  1094. fSizeFlags := fSizeFlags - PrevIsFreeFlag + prevSize;
  1095. if uint32(prevSize) >= MinSearchableVarHeaderAndPayload then
  1096. varFree.Remove(p);
  1097. end;
  1098. { Turn p into a free chunk and add it back to varFree...
  1099. unless it spans the entire OS chunk, in which case instead move the chunk from varOS to freeOS1 / freeOS. }
  1100. if (fSizeFlags and LastFlag = 0) or (pVarHeader(p - VarHeaderSize)^.ofsToOs <> -(VarOSChunkDataOffset + VarHeaderSize)) then
  1101. begin
  1102. dec(fSizeFlags, UsedFlag);
  1103. pVarHeader(p - VarHeaderSize)^.ch.h := fSizeFlags;
  1104. varFree.Add(p, fSizeFlags);
  1105. if fSizeFlags and LastFlag = 0 then
  1106. begin
  1107. pVarHeader(p + fSizeFlags - VarHeaderSize)^.ch.h := pVarHeader(p + fSizeFlags - VarHeaderSize)^.ch.h or PrevIsFreeFlag; { Could have it already. }
  1108. pFreeVarTail(p + fSizeFlags - (VarHeaderSize + FreeVarTailSize))^.size := fSizeFlags;
  1109. end;
  1110. end else
  1111. begin
  1112. osChunk := p - (VarOSChunkDataOffset + VarHeaderSize);
  1113. { Remove osChunk from varOS. }
  1114. osPrev := osChunk^.prev;
  1115. osNext := osChunk^.next;
  1116. if Assigned(osPrev) then
  1117. osPrev^.next := osNext
  1118. else
  1119. varOS := osNext;
  1120. if Assigned(osNext) then
  1121. osNext^.prev := osPrev;
  1122. {$ifdef HAS_SYSOSFREE}
  1123. { Move to freeOS1, discarding old freeOS1. }
  1124. if Assigned(freeOS1) then
  1125. begin
  1126. dec(allocated, freeOS1^.size);
  1127. SysOSFree(freeOS1, freeOS1^.size);
  1128. end;
  1129. freeOS1 := pFreeOSChunk(osChunk);
  1130. {$else HAS_SYSOSFREE}
  1131. fOs := @freeOS;
  1132. { Share if huge. }
  1133. {$ifdef FPC_HAS_FEATURE_THREADING}
  1134. if osChunk^.size > GrowHeapSize2 then
  1135. begin
  1136. fOs := @gs.freeOS;
  1137. EnterCriticalSection(gs.lock);
  1138. end;
  1139. {$endif FPC_HAS_FEATURE_THREADING}
  1140. { Add to fOs. }
  1141. freeOsNext := fOs^.first;
  1142. osChunk^.prev := nil;
  1143. osChunk^.next := freeOsNext;
  1144. if Assigned(freeOsNext) then
  1145. freeOsNext^.prev := osChunk
  1146. else
  1147. fOs^.last := pFreeOSChunk(osChunk);
  1148. fOs^.first := pFreeOSChunk(osChunk);
  1149. {$ifdef FPC_HAS_FEATURE_THREADING}
  1150. if fOs <> @freeOS then
  1151. begin
  1152. dec(allocated, osChunk^.size); { gs.freeOS aren’t counted anywhere, for now. }
  1153. LeaveCriticalSection(gs.lock);
  1154. end;
  1155. {$endif FPC_HAS_FEATURE_THREADING}
  1156. {$endif HAS_SYSOSFREE}
  1157. end;
  1158. dec(result, VarHeaderSize);
  1159. end;
  1160. function HeapInc.ThreadState.TryResizeVar(p: pointer; size: SizeUint): pointer;
  1161. var
  1162. ar: pointer absolute result;
  1163. fv, fp: pointer;
  1164. arSizeFlags, prevSize2, maxFv, minFragment, fSizeFlags, hNext, hNext2, oldph: uint32;
  1165. prevSize, binIndex, oldpsize, statv, arSize: SizeUint;
  1166. {$ifdef FPC_HAS_FEATURE_THREADING}
  1167. pts: ^pThreadState;
  1168. {$endif FPC_HAS_FEATURE_THREADING}
  1169. begin
  1170. result := nil;
  1171. 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. }
  1172. or (uint32(size) <= MaxFixedHeaderAndPayload - CommonHeaderSize)
  1173. then
  1174. exit;
  1175. {$ifdef FPC_HAS_FEATURE_THREADING}
  1176. pts := @pVarOSChunk(p + pVarHeader(p - VarHeaderSize)^.ofsToOs)^.threadState;
  1177. if pts^ <> @self then
  1178. begin
  1179. if Assigned(pts^) then { Pretest to avoid acquiring the lock. }
  1180. exit;
  1181. EnterCriticalSection(gs.lock);
  1182. if Assigned(pts^) then
  1183. begin
  1184. LeaveCriticalSection(gs.lock);
  1185. exit;
  1186. end;
  1187. AdoptVarOwner(p); { ...And continue! }
  1188. LeaveCriticalSection(gs.lock);
  1189. end;
  1190. {$endif FPC_HAS_FEATURE_THREADING}
  1191. { Round the size up, but only if supported by VarSizeToBinIndex: chunks can be reallocated to the sizes larger than MaxVarHeaderAndPayload. }
  1192. if uint32(size) <= MaxVarHeaderAndPayload - VarHeaderSize then
  1193. begin
  1194. binIndex := VarSizeToBinIndex(size + VarHeaderSize, true);
  1195. size := BinIndexToVarSize(binIndex);
  1196. end else
  1197. size := uint32(uint32(size) + (VarHeaderSize + VarSizeQuant - 1)) and uint32(-VarSizeQuant); { Just do the strictly necessary quantization... }
  1198. { ar + arSizeFlags (from “around”) is the chunk made from p and its adjacent free chunks. }
  1199. ar := p;
  1200. arSizeFlags := pVarHeader(ar - VarHeaderSize)^.ch.h;
  1201. if arSizeFlags and LastFlag = 0 then
  1202. begin
  1203. hNext := pVarHeader(ar + arSizeFlags and VarSizeMask - VarHeaderSize)^.ch.h;
  1204. if hNext and UsedFlag = 0 then
  1205. inc(arSizeFlags, hNext); { Inherit LastFlag, other flags must be 0. }
  1206. end;
  1207. if arSizeFlags and PrevIsFreeFlag <> 0 then
  1208. begin
  1209. prevSize := pFreeVarTail(ar - (VarHeaderSize + FreeVarTailSize))^.size;
  1210. dec(ar, prevSize);
  1211. inc(arSizeFlags, prevSize);
  1212. end;
  1213. if uint32(size) > arSizeFlags then { “ar” has no way to fit the new chunk. }
  1214. exit(nil);
  1215. { Check if there is a better place... }
  1216. maxFv := arSizeFlags div 4 * 3;
  1217. 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. }
  1218. begin
  1219. fv := varFree.Find(binIndex);
  1220. if Assigned(fv)
  1221. { fv may be one of the chunks around; in this case, ignore it. Checked as unsigned(fv - ar) < arSize. }
  1222. and (PtrUint(PtrInt(PtrUint(fv)) - PtrInt(PtrUint(ar))) >= arSizeFlags) { Logically “arSizeFlags and VarSizeMask”. }
  1223. { To justify moving FAR, better place should be CONSIDERABLY better: say, <75% of the ar. }
  1224. and (pVarHeader(fv)[-1].ch.h < maxFv) { Ignore masking, this is a rough check anyway. }
  1225. then
  1226. exit(nil);
  1227. end;
  1228. { So p will be placed inside “ar” after all. It is either moved to the beginning of “ar” or stays in place.
  1229. There might be no choice but to move: reallocating A in
  1230. [free 1,000][A 1,000][free 1,000]
  1231. to 2,500 bytes has to move, resulting in
  1232. [A 2,500][free 500].
  1233. But if there is a choice, moving might be or not be worth it. If we have
  1234. [free 5,000][A 1,000][free 5,000]
  1235. then moving will give
  1236. [A 1,000][free 10,000]
  1237. and that’s the point — [free 10,000] is better than 2 × [free 5,000]. But if we have
  1238. [free 64][A 1,000][free 9,936]
  1239. then moving for the sake of defragmenting these 64 bytes is definitely a waste of time.
  1240. 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. }
  1241. if arSizeFlags and PrevIsFreeFlag <> 0 then
  1242. begin
  1243. prevSize2 := pFreeVarTail(p - (VarHeaderSize + FreeVarTailSize))^.size;
  1244. { Consider (not) moving... }
  1245. dec(arSizeFlags, prevSize2); { Temporarily (or not) remove prevSize from arSizeFlags. This corresponds to the size available without moving. }
  1246. minFragment := uint32(size) div 8;
  1247. if (arSizeFlags < uint32(size)) { Size does not fit without moving? }
  1248. or (prevSize2 >= minFragment) and (uint32(arSizeFlags - uint32(size)) >= minFragment) { There are large enough fragments on both sides? }
  1249. then
  1250. begin
  1251. if prevSize2 >= MinSearchableVarHeaderAndPayload then
  1252. varFree.Remove(ar);
  1253. inc(arSizeFlags, prevSize2 - PrevIsFreeFlag); { Add prevSize back, and remove PrevIsFreeFlag. }
  1254. { Move(p^, ar^, ...) is postponed, see below. }
  1255. end else
  1256. { Not moving; finish the removal of the previous chunk from “ar”. arSizeFlags is already decreased by prevSize, and keeps PrevIsFreeFlag. }
  1257. ar := p; { Same as inc(ar, prevSize2). }
  1258. end;
  1259. { Remove the free chunk after p. Note that:
  1260. — Under some circumstances, it can be overwritten with Move, so Move must be postponed.
  1261. — This section might decide that TryResizeVar is a complete no-op and exit “early”, and this decision depends on the decision to move,
  1262. so the decision to move must be made first.
  1263. 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).
  1264. Without shortcutting the no-op case, this entire section can be simply moved above the previous one and postponing Move would not be required. }
  1265. oldph := pVarHeader(p)[-1].ch.h;
  1266. oldpsize := oldph and VarSizeMask;
  1267. if (uint32(size) = uint32(oldpsize)) and (ar = p) then
  1268. { TryResizeVar was a no-op, and with some explicit efforts we managed to write nothing by this point,
  1269. so we use our last chance to get out. }
  1270. exit;
  1271. if oldph and LastFlag = 0 then
  1272. begin
  1273. hNext2 := pVarHeader(p + oldpsize - VarHeaderSize)^.ch.h;
  1274. if (hNext2 and UsedFlag = 0) and (hNext2 >= MinSearchableVarHeaderAndPayload) then
  1275. varFree.Remove(p + oldpsize);
  1276. end;
  1277. dec(used, oldpsize);
  1278. if ar <> p then
  1279. begin
  1280. if uint32(size) < uint32(oldpsize) then { oldpsize is reused as “moveSize”. }
  1281. oldpsize := uint32(size);
  1282. Move(p^, ar^, oldpsize - VarHeaderSize);
  1283. end;
  1284. { Format the free chunk after ar, or its absence. }
  1285. fSizeFlags := uint32(arSizeFlags - uint32(size)) and (VarSizeMask or LastFlag);
  1286. if fSizeFlags >= uint32(MinAnyVarHeaderAndPayload + (MinSearchableVarHeaderAndPayload - MinAnyVarHeaderAndPayload) div LastFlag * (fSizeFlags and LastFlag)) then
  1287. begin
  1288. dec(arSizeFlags, fSizeFlags);
  1289. arSize := arSizeFlags and VarSizeMask;
  1290. fp := ar + arSize;
  1291. pVarHeader(fp)[-1].ofsToOs := pVarHeader(ar)[-1].ofsToOs - int32(arSize);
  1292. pVarHeader(fp)[-1].ch.h := fSizeFlags;
  1293. if fSizeFlags and LastFlag = 0 then
  1294. begin
  1295. pFreeVarTail(fp + fSizeFlags - (VarHeaderSize + FreeVarTailSize))^.size := fSizeFlags;
  1296. pVarHeader(fp + fSizeFlags)[-1].ch.h := pVarHeader(fp + fSizeFlags)[-1].ch.h or PrevIsFreeFlag; { May have had it already. }
  1297. end;
  1298. if fSizeFlags >= MinSearchableVarHeaderAndPayload then
  1299. varFree.Add(fp, fSizeFlags);
  1300. end
  1301. else if arSizeFlags and LastFlag = 0 then
  1302. 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. }
  1303. pVarHeader(ar)[-1].ch.h := arSizeFlags;
  1304. statv := used + arSizeFlags and VarSizeMask;
  1305. used := statv;
  1306. inc(statv, gs.hugeUsed);
  1307. if statv > maxUsed then
  1308. maxUsed := statv;
  1309. end;
  1310. { If SysOSFree is available, huge chunks aren’t cached by any means.
  1311. If SysOSFree is not available, there’s no choice but to cache them.
  1312. Caching is done directly into gs.freeOS if FPC_HAS_FEATURE_THREADING, otherwise ThreadState.freeOS. }
  1313. class function HeapInc.ThreadState.AddToHugeUsed(delta: SizeInt): SizeUint;
  1314. begin
  1315. {$if not defined(FPC_HAS_FEATURE_THREADING)}
  1316. result := SizeUint(SizeInt(gs.hugeUsed) + delta);
  1317. gs.hugeUsed := result;
  1318. {$elseif not defined(VER3_2)}
  1319. result := AtomicIncrement(gs.hugeUsed, SizeUint(delta));
  1320. {$elseif sizeof(SizeInt) = sizeof(int64)}
  1321. result := SizeUint(delta + InterlockedExchangeAdd64(SizeInt(gs.hugeUsed), delta));
  1322. {$else}
  1323. result := SizeUint(delta + InterlockedExchangeAdd(SizeInt(gs.hugeUsed), delta));
  1324. {$endif}
  1325. end;
  1326. function HeapInc.ThreadState.AllocHuge(size: SizeUint): pointer;
  1327. var
  1328. userSize: SizeUint;
  1329. begin
  1330. userSize := size;
  1331. size := (size + (HugeChunkDataOffset + CommonHeaderSize + OSChunkVarSizeQuant - 1)) and SizeUint(-OSChunkVarSizeQuant);
  1332. if size < userSize then { Overflow. }
  1333. exit(AllocFailed);
  1334. {$ifdef HAS_SYSOSFREE}
  1335. result := SysOSAlloc(size);
  1336. if not Assigned(result) then
  1337. exit(AllocFailed);
  1338. pHugeChunk(result)^.size := size;
  1339. {$else HAS_SYSOSFREE}
  1340. result := GetOSChunk(size, High(SizeUint));
  1341. if not Assigned(result) then
  1342. begin
  1343. result := AllocateOSChunk(size, High(SizeUint));
  1344. if not Assigned(result) then
  1345. exit; { AllocateOSChunk throws an error if required. }
  1346. end;
  1347. size := pOSChunk(result)^.size;
  1348. dec(allocated, size); { After GetOSChunk* chunk size is counted in “allocated”; don’t count. }
  1349. {$endif HAS_SYSOSFREE}
  1350. pCommonHeader(result + HugeChunkDataOffset)^.h := HugeHeader;
  1351. inc(result, HugeChunkDataOffset + CommonHeaderSize);
  1352. UpdateMaxStats(AddToHugeUsed(size));
  1353. end;
  1354. function HeapInc.ThreadState.FreeHuge(p: pointer): SizeUint;
  1355. {$ifndef HAS_SYSOSFREE}
  1356. var
  1357. fOs: ^FreeOSChunkList;
  1358. osPrev: pOSChunk;
  1359. {$endif ndef HAS_SYSOSFREE}
  1360. begin
  1361. dec(p, HugeChunkDataOffset + CommonHeaderSize);
  1362. result := pHugeChunk(p)^.size;
  1363. AddToHugeUsed(-SizeInt(result));
  1364. {$ifndef HAS_SYSOSFREE} { But you’d better have SysOSFree... }
  1365. {$ifdef FPC_HAS_FEATURE_THREADING}
  1366. fOs := @gs.freeOS; { gs.freeOS aren’t counted anywhere (for now). }
  1367. EnterCriticalSection(gs.lock);
  1368. {$else FPC_HAS_FEATURE_THREADING}
  1369. fOs := @freeOS;
  1370. 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. }
  1371. {$endif FPC_HAS_FEATURE_THREADING}
  1372. { Turn p into FreeOSChunk and add to fOs; add to the end to reduce the chance for this chunk to be reused
  1373. (other OS chunks are added to the beginning and searched from the beginning). }
  1374. osPrev := fOs^.last;
  1375. pFreeOSChunk(p)^.prev := osPrev;
  1376. pFreeOSChunk(p)^.next := nil;
  1377. if Assigned(osPrev) then
  1378. osPrev^.next := p
  1379. else
  1380. fOs^.first := p;
  1381. fOs^.last := p;
  1382. {$ifdef FPC_HAS_FEATURE_THREADING} LeaveCriticalSection(gs.lock); {$endif}
  1383. {$endif ndef HAS_SYSOSFREE}
  1384. {$ifdef HAS_SYSOSFREE} SysOSFree(p, result); {$endif}
  1385. dec(result, HugeChunkDataOffset + CommonHeaderSize);
  1386. end;
  1387. function HeapInc.ThreadState.TryResizeHuge(p: pointer; size: SizeUint): pointer;
  1388. var
  1389. userSize, oldSize: SizeUint;
  1390. begin
  1391. userSize := size;
  1392. size := (size + (HugeChunkDataOffset + CommonHeaderSize + OSChunkVarSizeQuant - 1)) and SizeUint(-OSChunkVarSizeQuant);
  1393. if (size < userSize) or { Overflow. }
  1394. (size < GrowHeapSize2 div 4) { Limit on shrinking huge chunks. }
  1395. then
  1396. exit(nil);
  1397. oldSize := pHugeChunk(p - (HugeChunkDataOffset + CommonHeaderSize))^.size;
  1398. if size = oldSize then
  1399. exit(p);
  1400. {$ifdef FPC_SYSTEM_HAS_SYSOSREALLOC}
  1401. result := SysOSRealloc(p - (HugeChunkDataOffset + CommonHeaderSize), oldSize, size);
  1402. if Assigned(result) then
  1403. begin
  1404. UpdateMaxStats(AddToHugeUsed(SizeInt(size) - SizeInt(oldSize)));
  1405. pHugeChunk(result)^.size := size;
  1406. inc(result, HugeChunkDataOffset + CommonHeaderSize);
  1407. end;
  1408. {$else FPC_SYSTEM_HAS_SYSOSREALLOC}
  1409. result := nil; { Just don’t. Note shrinking 20 Mb to 19 will require temporary 39 because of this. }
  1410. {$endif FPC_SYSTEM_HAS_SYSOSREALLOC}
  1411. end;
  1412. procedure HeapInc.ThreadState.UpdateMaxStats(hugeUsed: SizeUint);
  1413. var
  1414. statv: SizeUint;
  1415. begin
  1416. statv := used + hugeUsed;
  1417. if statv > maxUsed then
  1418. maxUsed := statv;
  1419. statv := allocated + hugeUsed;
  1420. if statv > maxAllocated then
  1421. maxAllocated := statv;
  1422. end;
  1423. {$ifdef FPC_HAS_FEATURE_THREADING}
  1424. procedure HeapInc.ThreadState.PushToFree(p: pFreeChunk);
  1425. var
  1426. next: pFreeChunk;
  1427. begin
  1428. repeat
  1429. next := toFree;
  1430. p^.next := next;
  1431. WriteBarrier; { Write p after p^.next. }
  1432. until InterlockedCompareExchange(toFree, p, next) = next;
  1433. end;
  1434. procedure HeapInc.ThreadState.FlushToFree;
  1435. var
  1436. tf, nx: pFreeChunk;
  1437. begin
  1438. tf := InterlockedExchange(toFree, nil);
  1439. while Assigned(tf) do
  1440. begin
  1441. ReadDependencyBarrier; { Read toFree^.next after toFree. }
  1442. nx := tf^.next;
  1443. SysFreeMem(tf);
  1444. tf := nx;
  1445. end;
  1446. end;
  1447. procedure HeapInc.ThreadState.Orphan;
  1448. var
  1449. vOs: pVarOSChunk;
  1450. p: pointer;
  1451. h: uint32;
  1452. {$ifndef HAS_SYSOSFREE}
  1453. lastFree, nextFree: pFreeOSChunk;
  1454. {$endif not HAS_SYSOSFREE}
  1455. begin
  1456. if gs.lockUse > 0 then
  1457. EnterCriticalSection(HeapInc.gs.lock);
  1458. FlushToFree; { Performing it under gs.lock guarantees there will be no new toFree requests. }
  1459. FreeEmptyArenas; { Has to free all empty arenas, otherwise the chunk that contains only empty arenas can leak. }
  1460. {$ifndef HAS_SYSOSFREE}
  1461. { Prepend freeOS to gs.freeOS. }
  1462. lastFree := freeOS.last;
  1463. if Assigned(lastFree) then
  1464. begin
  1465. nextFree := gs.freeOS.first;
  1466. lastFree^.next := nextFree;
  1467. if Assigned(nextFree) then
  1468. nextFree^.prev := lastFree
  1469. else
  1470. gs.freeOS.last := lastFree;
  1471. gs.freeOS.first := freeOS.first;
  1472. { Zeroing is probably required, because Orphan is called from FinalizeHeap which is called from DoneThread which can be called twice, according to this comment from syswin.inc: }
  1473. // DoneThread; { Assume everything is idempotent there }
  1474. freeOS.first := nil;
  1475. freeOS.last := nil;
  1476. end;
  1477. {$endif not HAS_SYSOSFREE}
  1478. vOs := varOS;
  1479. while Assigned(vOs) do
  1480. begin
  1481. vOs^.threadState := nil;
  1482. p := pointer(vOs) + (VarOSChunkDataOffset + VarHeaderSize);
  1483. repeat
  1484. h := pVarHeader(p - VarHeaderSize)^.ch.h;
  1485. if (h and UsedFlag = 0) and (h >= MinSearchableVarHeaderAndPayload) then
  1486. gs.varFree.Add(p, pFreeVarChunk(p)^.binIndex);
  1487. inc(p, h and VarSizeMask);
  1488. until h and LastFlag <> 0;
  1489. vOs := vOs^.next;
  1490. end;
  1491. varOS := nil;
  1492. if gs.lockUse > 0 then
  1493. LeaveCriticalSection(gs.lock);
  1494. {$ifdef HAS_SYSOSFREE}
  1495. if Assigned(freeOS1) then
  1496. begin
  1497. SysOSFree(freeOS1, freeOS1^.size); { Does not require gs.lock. }
  1498. freeOS1 := nil;
  1499. end;
  1500. {$endif HAS_SYSOSFREE}
  1501. end;
  1502. procedure HeapInc.ThreadState.AdoptArena(arena: pFixedArena);
  1503. var
  1504. sizeIndex: SizeUint;
  1505. nextArena: pFixedArena;
  1506. begin
  1507. sizeIndex := pCommonHeader(pointer(arena) + FixedArenaDataOffset)^.h and SizeIndexMask;
  1508. inc(used, arena^.usedSizeMinus1 + 1); { maxUsed is updated at the end of AdoptVarOwner. }
  1509. { Orphan frees all empty arenas, so adopted arena can’t be empty. }
  1510. if arena^.usedSizeMinus1 < arena^.almostFullThreshold + IndexToSize(sizeIndex) then
  1511. begin
  1512. { Add arena to partialArenas[sizeIndex]. }
  1513. nextArena := partialArenas[sizeIndex];
  1514. arena^.prev := nil;
  1515. arena^.next := nextArena;
  1516. if Assigned(nextArena) then
  1517. nextArena^.prev := arena;
  1518. partialArenas[sizeIndex] := arena;
  1519. end else
  1520. inc(allocatedByFullArenas[sizeIndex], pVarHeader(arena)[-1].ch.h and VarSizeMask);
  1521. end;
  1522. procedure HeapInc.ThreadState.AdoptVarOwner(p: pointer);
  1523. var
  1524. statv: SizeUint;
  1525. h: uint32;
  1526. vOs, osNext: pVarOSChunk;
  1527. begin
  1528. vOs := p + pVarHeader(p)[-1].ofsToOs;
  1529. vOs^.threadState := @self;
  1530. { Add OS chunk to varOS. }
  1531. vOs^.prev := nil;
  1532. osNext := varOS;
  1533. vOs^.next := osNext;
  1534. if Assigned(osNext) then
  1535. osNext^.prev := vOs;
  1536. varOS := vOs;
  1537. statv := allocated + vOs^.size;
  1538. allocated := statv;
  1539. inc(statv, gs.hugeUsed);
  1540. if statv > maxAllocated then
  1541. maxAllocated := statv;
  1542. p := pointer(vOs) + VarOSChunkDataOffset + VarHeaderSize;
  1543. repeat
  1544. h := pVarHeader(p - VarHeaderSize)^.ch.h;
  1545. if h and UsedFlag = 0 then
  1546. begin
  1547. if h >= MinSearchableVarHeaderAndPayload then
  1548. begin
  1549. gs.varFree.Remove(p);
  1550. varFree.Add(p, pFreeVarChunk(p)^.binIndex);
  1551. end;
  1552. end
  1553. else if h and FixedArenaFlag <> 0 then
  1554. AdoptArena(p)
  1555. else
  1556. inc(used, h and VarSizeMask); { maxUsed is updated after the loop. }
  1557. inc(p, h and VarSizeMask);
  1558. until h and LastFlag <> 0;
  1559. statv := used + gs.hugeUsed;
  1560. if statv > maxUsed then
  1561. maxUsed := statv;
  1562. end;
  1563. {$ifndef FPC_SECTION_THREADVARS}
  1564. procedure HeapInc.ThreadState.FixupSelfPtr;
  1565. var
  1566. vOs: pVarOSChunk;
  1567. begin
  1568. vOs := varOS;
  1569. while Assigned(vOs) do
  1570. begin
  1571. vOs^.threadState := @self;
  1572. vOs := vOs^.next;
  1573. end;
  1574. end;
  1575. {$endif ndef FPC_SECTION_THREADVARS}
  1576. {$endif FPC_HAS_FEATURE_THREADING}
  1577. class function HeapInc.AllocFailed: pointer;
  1578. begin
  1579. if not ReturnNilIfGrowHeapFails then
  1580. HandleError(204);
  1581. result := nil;
  1582. end;
  1583. function SysGetFPCHeapStatus:TFPCHeapStatus;
  1584. var
  1585. ts: HeapInc.pThreadState;
  1586. hugeUsed: SizeUint;
  1587. begin
  1588. ts := @HeapInc.thisTs;
  1589. hugeUsed := HeapInc.gs.hugeUsed;
  1590. ts^.UpdateMaxStats(hugeUsed); { Cheat to avoid clearly implausible values like current > max. }
  1591. result.MaxHeapSize := ts^.maxAllocated;
  1592. result.MaxHeapUsed := ts^.maxUsed;
  1593. result.CurrHeapSize := hugeUsed + ts^.allocated;
  1594. result.CurrHeapUsed := hugeUsed + ts^.used;
  1595. result.CurrHeapFree := result.CurrHeapSize - result.CurrHeapUsed;
  1596. end;
  1597. function SysGetHeapStatus :THeapStatus;
  1598. var
  1599. fhs: TFPCHeapStatus;
  1600. begin
  1601. fhs := SysGetFPCHeapStatus;
  1602. FillChar((@result)^, sizeof(result), 0);
  1603. result.TotalAllocated := fhs.CurrHeapUsed;
  1604. result.TotalFree := fhs.CurrHeapSize - fhs.CurrHeapUsed;
  1605. result.TotalAddrSpace := fhs.CurrHeapSize;
  1606. end;
  1607. function SysGetMem(size : ptruint):pointer;
  1608. var
  1609. ts: HeapInc.pThreadState;
  1610. begin
  1611. ts := @HeapInc.thisTs;
  1612. if size <= HeapInc.MaxFixedHeaderAndPayload - HeapInc.CommonHeaderSize then
  1613. exit(ts^.AllocFixed(size));
  1614. {$ifdef FPC_HAS_FEATURE_THREADING}
  1615. if Assigned(ts^.toFree) then
  1616. ts^.FlushToFree;
  1617. {$endif}
  1618. 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. }
  1619. and (size <= HeapInc.MaxVarHeaderAndPayload - HeapInc.VarHeaderSize) then
  1620. result := ts^.AllocVar(size, false)
  1621. else
  1622. result := ts^.AllocHuge(size);
  1623. end;
  1624. function SysFreeMem(p: pointer): ptruint;
  1625. var
  1626. ts: HeapInc.pThreadState;
  1627. begin
  1628. if Assigned(p) then
  1629. begin
  1630. ts := @HeapInc.thisTs;
  1631. if HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h and HeapInc.FixedFlag <> 0 then
  1632. result := ts^.FreeFixed(p)
  1633. else if HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h <> HeapInc.HugeHeader then
  1634. result := ts^.FreeVar(p)
  1635. else
  1636. result := ts^.FreeHuge(p);
  1637. end
  1638. else
  1639. result := 0;
  1640. end;
  1641. function SysMemSize(p: pointer): ptruint;
  1642. var
  1643. h: uint32;
  1644. begin
  1645. if not Assigned(p) then
  1646. exit(0);
  1647. h := HeapInc.pCommonHeader(p - HeapInc.CommonHeaderSize)^.h;
  1648. if h and HeapInc.FixedFlag <> 0 then
  1649. result := HeapInc.IndexToSize(h and HeapInc.SizeIndexMask) - HeapInc.CommonHeaderSize
  1650. else if h <> HeapInc.HugeHeader then
  1651. result := HeapInc.pVarHeader(p - HeapInc.VarHeaderSize)^.ch.h and uint32(HeapInc.VarSizeMask) - HeapInc.VarHeaderSize
  1652. else
  1653. result := HeapInc.pHugeChunk(p - (HeapInc.HugeChunkDataOffset + HeapInc.CommonHeaderSize))^.size - (HeapInc.HugeChunkDataOffset + HeapInc.CommonHeaderSize);
  1654. end;
  1655. function SysReAllocMem(var p: pointer; size: ptruint):pointer;
  1656. var
  1657. ts: HeapInc.pThreadState;
  1658. h: uint32;
  1659. oldOrCopySize: SizeUint;
  1660. newp: pointer;
  1661. begin
  1662. result := p; { Use as old p value until freed etc. }
  1663. if (size = 0) or not Assigned(result) then { Special cases; check at once. }
  1664. begin
  1665. if size = 0 then
  1666. begin
  1667. SysFreeMem(result);
  1668. result := nil;
  1669. end else
  1670. result := SysGetMem(size);
  1671. p := result;
  1672. exit;
  1673. end;
  1674. h := HeapInc.pCommonHeader(result - HeapInc.CommonHeaderSize)^.h;
  1675. if h and HeapInc.FixedFlag <> 0 then
  1676. begin
  1677. if (size <= HeapInc.MaxFixedHeaderAndPayload - HeapInc.CommonHeaderSize)
  1678. and (h and HeapInc.SizeIndexMask = HeapInc.SizeMinus1ToIndex(size + (HeapInc.CommonHeaderSize - 1))) then
  1679. exit;
  1680. end else
  1681. begin
  1682. ts := @HeapInc.thisTs;
  1683. {$ifdef FPC_HAS_FEATURE_THREADING}
  1684. if Assigned(ts^.toFree) then
  1685. ts^.FlushToFree;
  1686. {$endif FPC_HAS_FEATURE_THREADING}
  1687. if h <> HeapInc.HugeHeader then
  1688. newp := ts^.TryResizeVar(result, size)
  1689. else
  1690. newp := ts^.TryResizeHuge(result, size);
  1691. if Assigned(newp) then
  1692. begin
  1693. p := newp;
  1694. exit(newp);
  1695. end;
  1696. end;
  1697. { Generic fallback: GetMem + Move + FreeMem. }
  1698. oldOrCopySize := SysMemSize(result);
  1699. newp := SysGetMem(size);
  1700. if not Assigned(newp) then
  1701. begin
  1702. { Don’t fail if shrinking. If growing failed, return nil, but keep the old p. }
  1703. if size > oldOrCopySize then
  1704. result := nil;
  1705. exit;
  1706. end;
  1707. p := newp;
  1708. if oldOrCopySize > size then
  1709. oldOrCopySize := size;
  1710. Move(result^, newp^, oldOrCopySize);
  1711. SysFreeMem(result);
  1712. result := newp;
  1713. end;
  1714. Function SysFreeMemSize(p: pointer; size: ptruint):ptruint;
  1715. begin
  1716. { can't free partial blocks, ignore size }
  1717. result := SysFreeMem(p);
  1718. end;
  1719. function SysAllocMem(size: ptruint): pointer;
  1720. begin
  1721. result := SysGetMem(size);
  1722. if Assigned(result) then
  1723. FillChar(result^, SysMemSize(result), 0);
  1724. end;
  1725. {*****************************************************************************
  1726. InitHeap
  1727. *****************************************************************************}
  1728. { This function will initialize the Heap manager and need to be called from
  1729. the initialization of the system unit }
  1730. {$ifdef FPC_HAS_FEATURE_THREADING}
  1731. procedure InitHeapThread;
  1732. begin
  1733. if HeapInc.gs.lockUse>0 then
  1734. InterlockedIncrement(HeapInc.gs.lockUse);
  1735. end;
  1736. {$endif}
  1737. procedure InitHeap; public name '_FPC_InitHeap';
  1738. begin
  1739. { we cannot initialize the locks here yet, thread support is
  1740. not loaded yet }
  1741. end;
  1742. procedure RelocateHeap;
  1743. begin
  1744. {$ifdef FPC_HAS_FEATURE_THREADING}
  1745. if HeapInc.gs.lockUse > 0 then
  1746. exit;
  1747. HeapInc.gs.lockUse := 1;
  1748. InitCriticalSection(HeapInc.gs.lock);
  1749. {$ifndef FPC_SECTION_THREADVARS}
  1750. { threadState pointers still point to main thread's thisTs, but they
  1751. have a reference to the global main thisTs, fix them to point
  1752. to the main thread specific variable.
  1753. even if section threadvars are used, this shouldn't cause problems as threadState pointers simply
  1754. do not change but we do not need it }
  1755. HeapInc.thisTs.FixupSelfPtr;
  1756. {$endif FPC_SECTION_THREADVARS}
  1757. if MemoryManager.RelocateHeap <> nil then
  1758. MemoryManager.RelocateHeap();
  1759. {$endif FPC_HAS_FEATURE_THREADING}
  1760. end;
  1761. procedure FinalizeHeap;
  1762. begin
  1763. { Do not try to do anything if the heap manager already reported an error }
  1764. if (errorcode=203) or (errorcode=204) then
  1765. exit;
  1766. {$if defined(FPC_HAS_FEATURE_THREADING)}
  1767. HeapInc.thisTs.Orphan;
  1768. if (HeapInc.gs.lockUse > 0) and (InterlockedDecrement(HeapInc.gs.lockUse) = 0) then
  1769. DoneCriticalSection(HeapInc.gs.lock);
  1770. {$elseif defined(HAS_SYSOSFREE)}
  1771. if Assigned(HeapInc.thisTs.freeOS1) then
  1772. begin
  1773. dec(HeapInc.thisTs.allocated, HeapInc.thisTs.freeOS1^.size); { Just in case... }
  1774. SysOSFree(HeapInc.thisTs.freeOS1, HeapInc.thisTs.freeOS1^.size);
  1775. HeapInc.thisTs.freeOS1 := nil; { Just in case... }
  1776. end;
  1777. {$endif FPC_HAS_FEATURE_THREADING | defined(HAS_SYSOSFREE)}
  1778. end;
  1779. {$endif ndef HAS_MEMORYMANAGER}
  1780. {$endif FPC_HAS_FEATURE_HEAP}