tinyheap.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. oldsize := FindSize(p);
  245. OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  246. NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  247. if OldAllocSize = NewAllocSize then
  248. begin
  249. { old and new size are the same after alignment, so the memory block is already allocated }
  250. { we just need to update the size }
  251. PTinyHeapMemBlockSize(p)[-1] := size;
  252. if size > oldsize then
  253. FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
  254. end
  255. else if OldAllocSize > NewAllocSize then
  256. begin
  257. { we're decreasing the memory block size, so we can just free the remaining memory at the end }
  258. PTinyHeapMemBlockSize(p)[-1] := size;
  259. InternalTinyFreeMem(Pointer(TTinyHeapPointerArithmeticType(p)+(NewAllocSize-PtrUInt(SizeOf(TTinyHeapMemBlockSize)))), OldAllocSize-NewAllocSize);
  260. end
  261. else
  262. begin
  263. result := AllocMem(size);
  264. if result <> nil then
  265. begin
  266. if oldsize > size then
  267. oldsize := size;
  268. move(pbyte(p)^, pbyte(result)^, oldsize);
  269. end;
  270. SysTinyFreeMem(p);
  271. p := result;
  272. end;
  273. end;
  274. {$ifdef DEBUG_TINY_HEAP}
  275. Writeln(HexStr(result));
  276. {$endif DEBUG_TINY_HEAP}
  277. end;
  278. function MemAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
  279. var
  280. p: PTinyHeapBlock;
  281. begin
  282. MemAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  283. if MemAvail > 0 then
  284. Dec(MemAvail, SizeOf(TTinyHeapMemBlockSize));
  285. p := FreeList;
  286. while p <> HeapPtr do
  287. begin
  288. Inc(MemAvail, DecodeTinyHeapFreeBlockSize(p^.Size)-SizeOf(TTinyHeapMemBlockSize));
  289. p := p^.Next;
  290. end;
  291. end;
  292. function MaxAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
  293. var
  294. p: PTinyHeapBlock;
  295. begin
  296. MaxAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  297. p := FreeList;
  298. while p <> HeapPtr do
  299. begin
  300. if DecodeTinyHeapFreeBlockSize(p^.Size) > MaxAvail then
  301. MaxAvail := DecodeTinyHeapFreeBlockSize(p^.Size);
  302. p := p^.Next;
  303. end;
  304. if MaxAvail > 0 then
  305. Dec(MaxAvail, SizeOf(TTinyHeapMemBlockSize));
  306. end;
  307. procedure Mark(var p: Pointer);
  308. begin
  309. p := HeapPtr;
  310. end;
  311. procedure Release(var p: Pointer);
  312. begin
  313. HeapPtr := p;
  314. FreeList := p;
  315. end;
  316. procedure InternalTinyAlign(var AAddress: Pointer; ASize: PtrUInt);
  317. var
  318. alignment_inc: smallint;
  319. begin
  320. alignment_inc := TTinyHeapPointerArithmeticType(align(AAddress,TinyHeapAllocGranularity))-TTinyHeapPointerArithmeticType(AAddress);
  321. Inc(AAddress,alignment_inc);
  322. Dec(ASize,alignment_inc);
  323. Dec(ASize,ASize mod TinyHeapAllocGranularity);
  324. end;
  325. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  326. the heap is only a single contiguous memory block. If you want to add
  327. multiple blocks to the heap, you should use RegisterTinyHeapBlock instead. }
  328. procedure RegisterTinyHeapBlock_Simple(AAddress: Pointer; ASize: PtrUInt);
  329. begin
  330. {$ifdef DEBUG_TINY_HEAP}
  331. Writeln('RegisterTinyHeapBlock_Simple(', HexStr(AAddress), ',', ASize, ')');
  332. {$endif DEBUG_TINY_HEAP}
  333. InternalTinyAlign(AAddress, ASize);
  334. HeapOrg:=AAddress;
  335. HeapPtr:=AAddress;
  336. FreeList:=AAddress;
  337. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  338. end;
  339. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  340. the heap is only a single contiguous memory block and the address and size
  341. are already aligned on a TinyHeapAllocGranularity boundary. }
  342. procedure RegisterTinyHeapBlock_Simple_Prealigned(AAddress: Pointer; ASize: PtrUInt);
  343. begin
  344. {$ifdef DEBUG_TINY_HEAP}
  345. Writeln('RegisterTinyHeapBlock_Simple_Prealigned(', HexStr(AAddress), ',', ASize, ')');
  346. {$endif DEBUG_TINY_HEAP}
  347. HeapOrg:=AAddress;
  348. HeapPtr:=AAddress;
  349. FreeList:=AAddress;
  350. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  351. end;
  352. procedure RegisterTinyHeapBlock(AAddress: pointer; ASize: ptruint);
  353. var
  354. alignment_inc: smallint;
  355. p: PTinyHeapBlock;
  356. begin
  357. {$ifdef DEBUG_TINY_HEAP}
  358. Writeln('RegisterTinyHeapBlock(', HexStr(AAddress), ',', ASize, ')');
  359. {$endif DEBUG_TINY_HEAP}
  360. InternalTinyAlign(AAddress, ASize);
  361. if HeapOrg=nil then
  362. begin
  363. HeapOrg:=AAddress;
  364. HeapPtr:=AAddress;
  365. FreeList:=AAddress;
  366. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  367. end
  368. else
  369. begin
  370. if (TTinyHeapPointerArithmeticType(HeapOrg) > TTinyHeapPointerArithmeticType(AAddress)) then
  371. HeapOrg:=AAddress;
  372. if TTinyHeapPointerArithmeticType(AAddress) > TTinyHeapPointerArithmeticType(HeapEnd) then
  373. begin
  374. if TTinyHeapPointerArithmeticType(HeapPtr) = TTinyHeapPointerArithmeticType(HeapEnd) then
  375. begin
  376. if FreeList=HeapPtr then
  377. FreeList:=AAddress
  378. else
  379. begin
  380. p:=FreeList;
  381. while p^.Next<>HeapPtr do
  382. p:=p^.Next;
  383. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  384. end;
  385. end
  386. else
  387. begin
  388. PTinyHeapBlock(HeapPtr)^.Size:=EncodeTinyHeapFreeBlockSize(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  389. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  390. end;
  391. HeapPtr:=AAddress;
  392. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  393. end
  394. else if TTinyHeapPointerArithmeticType(AAddress) = TTinyHeapPointerArithmeticType(HeapEnd) then
  395. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize)
  396. else
  397. InternalTinyFreeMem(AAddress, ASize);
  398. end;
  399. end;
  400. const
  401. TinyHeapMemoryManager: TMemoryManager = (
  402. NeedLock: false; // Obsolete
  403. GetMem: @SysTinyGetMem;
  404. FreeMem: @SysTinyFreeMem;
  405. FreeMemSize: @SysTinyFreeMemSize;
  406. AllocMem: @SysTinyAllocMem;
  407. ReAllocMem: @SysTinyReAllocMem;
  408. MemSize: @SysTinyMemSize;
  409. InitThread: nil;
  410. DoneThread: nil;
  411. RelocateHeap: nil;
  412. GetHeapStatus: nil;
  413. GetFPCHeapStatus: nil;
  414. );