tinyheap.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. sz: 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. result := nil
  233. else
  234. result := AllocMem(size);
  235. if result <> nil then
  236. begin
  237. if p <> nil then
  238. begin
  239. sz := FindSize(p);
  240. if sz > size then
  241. sz := size;
  242. move(pbyte(p)^, pbyte(result)^, sz);
  243. end;
  244. end;
  245. SysTinyFreeMem(p);
  246. p := result;
  247. {$ifdef DEBUG_TINY_HEAP}
  248. Writeln(HexStr(result));
  249. {$endif DEBUG_TINY_HEAP}
  250. end;
  251. function MemAvail: PtrUInt;
  252. var
  253. p: PTinyHeapBlock;
  254. begin
  255. MemAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  256. if MemAvail > 0 then
  257. Dec(MemAvail, SizeOf(TTinyHeapMemBlockSize));
  258. p := FreeList;
  259. while p <> HeapPtr do
  260. begin
  261. Inc(MemAvail, DecodeTinyHeapFreeBlockSize(p^.Size)-SizeOf(TTinyHeapMemBlockSize));
  262. p := p^.Next;
  263. end;
  264. end;
  265. function MaxAvail: PtrUInt;
  266. var
  267. p: PTinyHeapBlock;
  268. begin
  269. MaxAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  270. p := FreeList;
  271. while p <> HeapPtr do
  272. begin
  273. if DecodeTinyHeapFreeBlockSize(p^.Size) > MaxAvail then
  274. MaxAvail := DecodeTinyHeapFreeBlockSize(p^.Size);
  275. p := p^.Next;
  276. end;
  277. if MaxAvail > 0 then
  278. Dec(MaxAvail, SizeOf(TTinyHeapMemBlockSize));
  279. end;
  280. procedure InternalTinyAlign(var AAddress: Pointer; ASize: PtrUInt);
  281. var
  282. alignment_inc: smallint;
  283. begin
  284. alignment_inc := TTinyHeapPointerArithmeticType(align(AAddress,TinyHeapAllocGranularity))-TTinyHeapPointerArithmeticType(AAddress);
  285. Inc(AAddress,alignment_inc);
  286. Dec(ASize,alignment_inc);
  287. Dec(ASize,ASize mod TinyHeapAllocGranularity);
  288. end;
  289. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  290. the heap is only a single contiguous memory block. If you want to add
  291. multiple blocks to the heap, you should use RegisterTinyHeapBlock instead. }
  292. procedure RegisterTinyHeapBlock_Simple(AAddress: Pointer; ASize: PtrUInt);
  293. begin
  294. {$ifdef DEBUG_TINY_HEAP}
  295. Writeln('RegisterTinyHeapBlock_Simple(', HexStr(AAddress), ',', ASize, ')');
  296. {$endif DEBUG_TINY_HEAP}
  297. InternalTinyAlign(AAddress, ASize);
  298. HeapOrg:=AAddress;
  299. HeapPtr:=AAddress;
  300. FreeList:=AAddress;
  301. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  302. end;
  303. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  304. the heap is only a single contiguous memory block and the address and size
  305. are already aligned on a TinyHeapAllocGranularity boundary. }
  306. procedure RegisterTinyHeapBlock_Simple_Prealigned(AAddress: Pointer; ASize: PtrUInt);
  307. begin
  308. {$ifdef DEBUG_TINY_HEAP}
  309. Writeln('RegisterTinyHeapBlock_Simple_Prealigned(', HexStr(AAddress), ',', ASize, ')');
  310. {$endif DEBUG_TINY_HEAP}
  311. HeapOrg:=AAddress;
  312. HeapPtr:=AAddress;
  313. FreeList:=AAddress;
  314. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  315. end;
  316. procedure RegisterTinyHeapBlock(AAddress: pointer; ASize: ptruint);
  317. var
  318. alignment_inc: smallint;
  319. p: PTinyHeapBlock;
  320. begin
  321. {$ifdef DEBUG_TINY_HEAP}
  322. Writeln('RegisterTinyHeapBlock(', HexStr(AAddress), ',', ASize, ')');
  323. {$endif DEBUG_TINY_HEAP}
  324. InternalTinyAlign(AAddress, ASize);
  325. if HeapOrg=nil then
  326. begin
  327. HeapOrg:=AAddress;
  328. HeapPtr:=AAddress;
  329. FreeList:=AAddress;
  330. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  331. end
  332. else
  333. begin
  334. if (TTinyHeapPointerArithmeticType(HeapOrg) > TTinyHeapPointerArithmeticType(AAddress)) then
  335. HeapOrg:=AAddress;
  336. if TTinyHeapPointerArithmeticType(AAddress) > TTinyHeapPointerArithmeticType(HeapEnd) then
  337. begin
  338. if TTinyHeapPointerArithmeticType(HeapPtr) = TTinyHeapPointerArithmeticType(HeapEnd) then
  339. begin
  340. if FreeList=HeapPtr then
  341. FreeList:=AAddress
  342. else
  343. begin
  344. p:=FreeList;
  345. while p^.Next<>HeapPtr do
  346. p:=p^.Next;
  347. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  348. end;
  349. end
  350. else
  351. begin
  352. PTinyHeapBlock(HeapPtr)^.Size:=EncodeTinyHeapFreeBlockSize(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  353. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  354. end;
  355. HeapPtr:=AAddress;
  356. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  357. end
  358. else if TTinyHeapPointerArithmeticType(AAddress) = TTinyHeapPointerArithmeticType(HeapEnd) then
  359. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize)
  360. else
  361. InternalTinyFreeMem(AAddress, ASize);
  362. end;
  363. end;
  364. const
  365. TinyHeapMemoryManager: TMemoryManager = (
  366. NeedLock: false; // Obsolete
  367. GetMem: @SysTinyGetMem;
  368. FreeMem: @SysTinyFreeMem;
  369. FreeMemSize: @SysTinyFreeMemSize;
  370. AllocMem: @SysTinyAllocMem;
  371. ReAllocMem: @SysTinyReAllocMem;
  372. MemSize: @SysTinyMemSize;
  373. InitThread: nil;
  374. DoneThread: nil;
  375. RelocateHeap: nil;
  376. GetHeapStatus: nil;
  377. GetFPCHeapStatus: nil;
  378. );