tinyheap.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2011 by the Free Pascal development team.
  4. Tiny heap manager for the i8086 near heap, embedded targets, etc.
  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. { The heap, implemented here is TP7-compatible in the i8086 far data memory
  12. models. It's basically a linked list of free blocks, which are kept ordered by
  13. start address. The FreeList variable points to the start of the list. Each
  14. free block, except the last one, contains a TTinyHeapBlock structure, which
  15. holds the block size and a pointer to the next free block. The HeapPtr
  16. variable points to the last free block, indicating the end of the list. The
  17. last block is special in that it doesn't contain a TTinyHeapBlock structure.
  18. Instead its size is determined by the pointer difference (HeapEnd-HeapPtr).
  19. It *can* become zero sized, when all the memory inside of it is allocated, in
  20. which case, HeapPtr will become equal to HeapEnd. }
  21. {$ifdef FPC_TINYHEAP_HUGE}
  22. {$HugePointerArithmeticNormalization On}
  23. {$HugePointerComparisonNormalization On}
  24. {$endif FPC_TINYHEAP_HUGE}
  25. type
  26. { TTinyHeapMemBlockSize holds the size of an *allocated* memory block,
  27. and is written at position:
  28. memblockstart-sizeof(TTinyHeapMemBlockSize) }
  29. PTinyHeapMemBlockSize = ^TTinyHeapMemBlockSize; {$ifdef FPC_TINYHEAP_HUGE}huge;{$endif}
  30. TTinyHeapMemBlockSize = PtrUInt;
  31. { TTinyHeapFreeBlockSize holds the size of a *free* memory block, as a
  32. part of the TTinyHeapBlock structure }
  33. {$ifdef FPC_TINYHEAP_HUGE}
  34. TTinyHeapFreeBlockSize = record
  35. OfsSize: Word;
  36. SegSize: Word;
  37. end;
  38. {$else FPC_TINYHEAP_HUGE}
  39. TTinyHeapFreeBlockSize = PtrUInt;
  40. {$endif FPC_TINYHEAP_HUGE}
  41. TTinyHeapPointerArithmeticType = ^Byte; {$ifdef FPC_TINYHEAP_HUGE}huge;{$endif}
  42. PTinyHeapBlock = ^TTinyHeapBlock;
  43. TTinyHeapBlock = record
  44. Next: PTinyHeapBlock;
  45. Size: TTinyHeapFreeBlockSize;
  46. end;
  47. const
  48. TinyHeapMinBlock = sizeof(TTinyHeapBlock);
  49. TinyHeapAllocGranularity = sizeof(TTinyHeapBlock);
  50. function EncodeTinyHeapFreeBlockSize(Size: PtrUInt): TTinyHeapFreeBlockSize; inline;
  51. begin
  52. {$ifdef FPC_TINYHEAP_HUGE}
  53. EncodeTinyHeapFreeBlockSize.OfsSize := Size and 15;
  54. EncodeTinyHeapFreeBlockSize.SegSize := Size shr 4;
  55. {$else FPC_TINYHEAP_HUGE}
  56. EncodeTinyHeapFreeBlockSize := Size;
  57. {$endif FPC_TINYHEAP_HUGE}
  58. end;
  59. function DecodeTinyHeapFreeBlockSize(Size: TTinyHeapFreeBlockSize): PtrUInt; inline;
  60. begin
  61. {$ifdef FPC_TINYHEAP_HUGE}
  62. DecodeTinyHeapFreeBlockSize := (PtrUInt(Size.SegSize) shl 4) + Size.OfsSize;
  63. {$else FPC_TINYHEAP_HUGE}
  64. DecodeTinyHeapFreeBlockSize := Size;
  65. {$endif FPC_TINYHEAP_HUGE}
  66. end;
  67. procedure InternalTinyFreeMem(Addr: Pointer; Size: PtrUInt); forward;
  68. function FindSize(p: pointer): TTinyHeapMemBlockSize;
  69. begin
  70. FindSize := PTinyHeapMemBlockSize(p)[-1];
  71. end;
  72. function SysTinyGetMem(Size: ptruint): pointer;
  73. var
  74. p, prev, p2: PTinyHeapBlock;
  75. AllocSize, RestSize: ptruint;
  76. begin
  77. {$ifdef DEBUG_TINY_HEAP}
  78. Write('SysTinyGetMem(', Size, ')=');
  79. {$endif DEBUG_TINY_HEAP}
  80. AllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  81. p := FreeList;
  82. prev := nil;
  83. while (p<>HeapPtr) and (DecodeTinyHeapFreeBlockSize(p^.Size) < AllocSize) do
  84. begin
  85. prev := p;
  86. p := p^.Next;
  87. end;
  88. if p<>HeapPtr then
  89. begin
  90. result := @PTinyHeapMemBlockSize(p)[1];
  91. if DecodeTinyHeapFreeBlockSize(p^.Size)-AllocSize >= TinyHeapMinBlock then
  92. RestSize := DecodeTinyHeapFreeBlockSize(p^.Size)-AllocSize
  93. else
  94. begin
  95. AllocSize := DecodeTinyHeapFreeBlockSize(p^.Size);
  96. RestSize := 0;
  97. end;
  98. if RestSize > 0 then
  99. begin
  100. p2 := pointer(TTinyHeapPointerArithmeticType(p)+AllocSize);
  101. p2^.Next := p^.Next;
  102. p2^.Size := EncodeTinyHeapFreeBlockSize(RestSize);
  103. if prev = nil then
  104. FreeList := p2
  105. else
  106. prev^.next := p2;
  107. end
  108. else
  109. begin
  110. if prev = nil then
  111. FreeList := p^.Next
  112. else
  113. prev^.next := p^.next;
  114. end;
  115. PTinyHeapMemBlockSize(p)^ := size;
  116. end
  117. else
  118. begin
  119. { p=HeapPtr }
  120. if PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr))<AllocSize then
  121. if ReturnNilIfGrowHeapFails then
  122. Result := nil
  123. else
  124. HandleError(203);
  125. result := @PTinyHeapMemBlockSize(HeapPtr)[1];
  126. PTinyHeapMemBlockSize(HeapPtr)^ := size;
  127. HeapPtr := pointer(TTinyHeapPointerArithmeticType(HeapPtr)+AllocSize);
  128. if prev = nil then
  129. FreeList := HeapPtr
  130. else
  131. prev^.next := HeapPtr;
  132. end;
  133. {$ifdef DEBUG_TINY_HEAP}
  134. Writeln(HexStr(Result));
  135. {$endif DEBUG_TINY_HEAP}
  136. end;
  137. function TinyGetAlignedMem(Size, Alignment: ptruint): pointer;
  138. var
  139. mem: Pointer;
  140. memp: ptruint;
  141. begin
  142. if alignment <= sizeof(pointer) then
  143. result := GetMem(size)
  144. else
  145. begin
  146. mem := GetMem(Size+Alignment-1);
  147. memp := align(ptruint(mem), Alignment);
  148. InternalTinyFreeMem(mem, TTinyHeapPointerArithmeticType(memp)-TTinyHeapPointerArithmeticType(mem));
  149. result := pointer(memp);
  150. end;
  151. end;
  152. procedure InternalTinyFreeMem(Addr: Pointer; Size: PtrUInt);
  153. var
  154. p, prev: PTinyHeapBlock;
  155. begin
  156. p := FreeList;
  157. prev := nil;
  158. while (p<>HeapPtr) and (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(Addr)) do
  159. begin
  160. prev := p;
  161. p := p^.Next;
  162. end;
  163. { join with previous block? }
  164. if assigned(prev) and ((TTinyHeapPointerArithmeticType(prev)+DecodeTinyHeapFreeBlockSize(prev^.Size)) = TTinyHeapPointerArithmeticType(Addr)) then
  165. begin
  166. Addr:=prev;
  167. Size:=DecodeTinyHeapFreeBlockSize(prev^.size)+Size;
  168. end
  169. else
  170. if assigned(prev) then
  171. prev^.Next := Addr
  172. else
  173. FreeList := Addr;
  174. { join with next block? }
  175. if TTinyHeapPointerArithmeticType(p)=(TTinyHeapPointerArithmeticType(Addr)+Size) then
  176. begin
  177. if p=HeapPtr then
  178. HeapPtr:=Addr
  179. else
  180. begin
  181. PTinyHeapBlock(Addr)^.Next:=p^.Next;
  182. PTinyHeapBlock(Addr)^.Size:=EncodeTinyHeapFreeBlockSize(Size+DecodeTinyHeapFreeBlockSize(p^.Size));
  183. end;
  184. end
  185. else
  186. begin
  187. PTinyHeapBlock(Addr)^.Next:=p;
  188. PTinyHeapBlock(Addr)^.Size:=EncodeTinyHeapFreeBlockSize(Size);
  189. end;
  190. end;
  191. function SysTinyFreeMem(Addr: Pointer): ptruint;
  192. var
  193. sz: ptruint;
  194. begin
  195. {$ifdef DEBUG_TINY_HEAP}
  196. Writeln('SysTinyFreeMem(', HexStr(Addr), ')');
  197. {$endif DEBUG_TINY_HEAP}
  198. if addr=nil then
  199. begin
  200. result:=0;
  201. exit;
  202. end;
  203. if (TTinyHeapPointerArithmeticType(addr) < TTinyHeapPointerArithmeticType(HeapOrg)) or
  204. (TTinyHeapPointerArithmeticType(addr) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
  205. HandleError(204);
  206. sz := Align(FindSize(addr)+SizeOf(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  207. InternalTinyFreeMem(@PTinyHeapMemBlockSize(addr)[-1], sz);
  208. result := sz;
  209. end;
  210. function SysTinyFreeMemSize(Addr: Pointer; Size: Ptruint): ptruint;
  211. begin
  212. result := SysTinyFreeMem(addr);
  213. end;
  214. function SysTinyMemSize(p: pointer): ptruint;
  215. begin
  216. result := findsize(p);
  217. end;
  218. function SysTinyAllocMem(size: ptruint): pointer;
  219. begin
  220. result := SysTinyGetMem(size);
  221. if result<>nil then
  222. FillChar(result^,SysTinyMemSize(result),0);
  223. end;
  224. function SysTinyReAllocMem(var p: pointer; size: ptruint):pointer;
  225. var
  226. oldsize, OldAllocSize, NewAllocSize: ptruint;
  227. begin
  228. {$ifdef DEBUG_TINY_HEAP}
  229. Write('SysTinyReAllocMem(', HexStr(p), ',', size, ')=');
  230. {$endif DEBUG_TINY_HEAP}
  231. if size=0 then
  232. begin
  233. SysTinyFreeMem(p);
  234. result := nil;
  235. p := nil;
  236. end
  237. else if p=nil then
  238. begin
  239. result := AllocMem(size);
  240. p := result;
  241. end
  242. else
  243. begin
  244. if (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(HeapOrg)) or
  245. (TTinyHeapPointerArithmeticType(p) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
  246. HandleError(204);
  247. oldsize := FindSize(p);
  248. OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  249. NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  250. if OldAllocSize = NewAllocSize then
  251. begin
  252. { old and new size are the same after alignment, so the memory block is already allocated }
  253. { we just need to update the size }
  254. PTinyHeapMemBlockSize(p)[-1] := size;
  255. if size > oldsize then
  256. FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
  257. end
  258. else if OldAllocSize > NewAllocSize then
  259. begin
  260. { we're decreasing the memory block size, so we can just free the remaining memory at the end }
  261. PTinyHeapMemBlockSize(p)[-1] := size;
  262. InternalTinyFreeMem(Pointer(TTinyHeapPointerArithmeticType(p)+(NewAllocSize-PtrUInt(SizeOf(TTinyHeapMemBlockSize)))), OldAllocSize-NewAllocSize);
  263. end
  264. else
  265. begin
  266. result := AllocMem(size);
  267. if result <> nil then
  268. begin
  269. if oldsize > size then
  270. oldsize := size;
  271. move(pbyte(p)^, pbyte(result)^, oldsize);
  272. end;
  273. SysTinyFreeMem(p);
  274. p := result;
  275. end;
  276. end;
  277. {$ifdef DEBUG_TINY_HEAP}
  278. Writeln(HexStr(result));
  279. {$endif DEBUG_TINY_HEAP}
  280. end;
  281. function MemAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
  282. var
  283. p: PTinyHeapBlock;
  284. begin
  285. MemAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  286. if MemAvail > 0 then
  287. Dec(MemAvail, SizeOf(TTinyHeapMemBlockSize));
  288. p := FreeList;
  289. while p <> HeapPtr do
  290. begin
  291. Inc(MemAvail, DecodeTinyHeapFreeBlockSize(p^.Size)-SizeOf(TTinyHeapMemBlockSize));
  292. p := p^.Next;
  293. end;
  294. end;
  295. function MaxAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
  296. var
  297. p: PTinyHeapBlock;
  298. begin
  299. MaxAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  300. p := FreeList;
  301. while p <> HeapPtr do
  302. begin
  303. if DecodeTinyHeapFreeBlockSize(p^.Size) > MaxAvail then
  304. MaxAvail := DecodeTinyHeapFreeBlockSize(p^.Size);
  305. p := p^.Next;
  306. end;
  307. if MaxAvail > 0 then
  308. Dec(MaxAvail, SizeOf(TTinyHeapMemBlockSize));
  309. end;
  310. procedure Mark(var p: Pointer);
  311. begin
  312. p := HeapPtr;
  313. end;
  314. procedure Release(var p: Pointer);
  315. begin
  316. HeapPtr := p;
  317. FreeList := p;
  318. end;
  319. procedure InternalTinyAlign(var AAddress: Pointer; ASize: PtrUInt);
  320. var
  321. alignment_inc: smallint;
  322. begin
  323. alignment_inc := TTinyHeapPointerArithmeticType(align(AAddress,TinyHeapAllocGranularity))-TTinyHeapPointerArithmeticType(AAddress);
  324. Inc(AAddress,alignment_inc);
  325. Dec(ASize,alignment_inc);
  326. Dec(ASize,ASize mod TinyHeapAllocGranularity);
  327. end;
  328. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  329. the heap is only a single contiguous memory block. If you want to add
  330. multiple blocks to the heap, you should use RegisterTinyHeapBlock instead. }
  331. procedure RegisterTinyHeapBlock_Simple(AAddress: Pointer; ASize: PtrUInt);
  332. begin
  333. {$ifdef DEBUG_TINY_HEAP}
  334. Writeln('RegisterTinyHeapBlock_Simple(', HexStr(AAddress), ',', ASize, ')');
  335. {$endif DEBUG_TINY_HEAP}
  336. InternalTinyAlign(AAddress, ASize);
  337. HeapOrg:=AAddress;
  338. HeapPtr:=AAddress;
  339. FreeList:=AAddress;
  340. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  341. end;
  342. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  343. the heap is only a single contiguous memory block and the address and size
  344. are already aligned on a TinyHeapAllocGranularity boundary. }
  345. procedure RegisterTinyHeapBlock_Simple_Prealigned(AAddress: Pointer; ASize: PtrUInt);
  346. begin
  347. {$ifdef DEBUG_TINY_HEAP}
  348. Writeln('RegisterTinyHeapBlock_Simple_Prealigned(', HexStr(AAddress), ',', ASize, ')');
  349. {$endif DEBUG_TINY_HEAP}
  350. HeapOrg:=AAddress;
  351. HeapPtr:=AAddress;
  352. FreeList:=AAddress;
  353. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  354. end;
  355. procedure RegisterTinyHeapBlock(AAddress: pointer; ASize: ptruint);
  356. var
  357. alignment_inc: smallint;
  358. p: PTinyHeapBlock;
  359. begin
  360. {$ifdef DEBUG_TINY_HEAP}
  361. Writeln('RegisterTinyHeapBlock(', HexStr(AAddress), ',', ASize, ')');
  362. {$endif DEBUG_TINY_HEAP}
  363. InternalTinyAlign(AAddress, ASize);
  364. if HeapOrg=nil then
  365. begin
  366. HeapOrg:=AAddress;
  367. HeapPtr:=AAddress;
  368. FreeList:=AAddress;
  369. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  370. end
  371. else
  372. begin
  373. if (TTinyHeapPointerArithmeticType(HeapOrg) > TTinyHeapPointerArithmeticType(AAddress)) then
  374. HeapOrg:=AAddress;
  375. if TTinyHeapPointerArithmeticType(AAddress) > TTinyHeapPointerArithmeticType(HeapEnd) then
  376. begin
  377. if TTinyHeapPointerArithmeticType(HeapPtr) = TTinyHeapPointerArithmeticType(HeapEnd) then
  378. begin
  379. if FreeList=HeapPtr then
  380. FreeList:=AAddress
  381. else
  382. begin
  383. p:=FreeList;
  384. while p^.Next<>HeapPtr do
  385. p:=p^.Next;
  386. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  387. end;
  388. end
  389. else
  390. begin
  391. PTinyHeapBlock(HeapPtr)^.Size:=EncodeTinyHeapFreeBlockSize(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  392. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  393. end;
  394. HeapPtr:=AAddress;
  395. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  396. end
  397. else if TTinyHeapPointerArithmeticType(AAddress) = TTinyHeapPointerArithmeticType(HeapEnd) then
  398. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize)
  399. else
  400. InternalTinyFreeMem(AAddress, ASize);
  401. end;
  402. end;
  403. const
  404. TinyHeapMemoryManager: TMemoryManager = (
  405. NeedLock: false; // Obsolete
  406. GetMem: @SysTinyGetMem;
  407. FreeMem: @SysTinyFreeMem;
  408. FreeMemSize: @SysTinyFreeMemSize;
  409. AllocMem: @SysTinyAllocMem;
  410. ReAllocMem: @SysTinyReAllocMem;
  411. MemSize: @SysTinyMemSize;
  412. InitThread: nil;
  413. DoneThread: nil;
  414. RelocateHeap: nil;
  415. GetHeapStatus: nil;
  416. GetFPCHeapStatus: nil;
  417. );