tinyheap.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. after_block, before_block: PTinyHeapBlock;
  228. after_block_size: PtrUInt;
  229. new_after_block: PTinyHeapBlock;
  230. begin
  231. {$ifdef DEBUG_TINY_HEAP}
  232. Write('SysTinyReAllocMem(', HexStr(p), ',', size, ')=');
  233. {$endif DEBUG_TINY_HEAP}
  234. if size=0 then
  235. begin
  236. SysTinyFreeMem(p);
  237. result := nil;
  238. p := nil;
  239. end
  240. else if p=nil then
  241. begin
  242. result := AllocMem(size);
  243. p := result;
  244. end
  245. else
  246. begin
  247. if (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(HeapOrg)) or
  248. (TTinyHeapPointerArithmeticType(p) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
  249. HandleError(204);
  250. oldsize := FindSize(p);
  251. OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  252. NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
  253. if OldAllocSize = NewAllocSize then
  254. begin
  255. { old and new size are the same after alignment, so the memory block is already allocated }
  256. { we just need to update the size }
  257. PTinyHeapMemBlockSize(p)[-1] := size;
  258. if size > oldsize then
  259. FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
  260. end
  261. else if OldAllocSize > NewAllocSize then
  262. begin
  263. { we're decreasing the memory block size, so we can just free the remaining memory at the end }
  264. PTinyHeapMemBlockSize(p)[-1] := size;
  265. InternalTinyFreeMem(Pointer(TTinyHeapPointerArithmeticType(p)+(NewAllocSize-PtrUInt(SizeOf(TTinyHeapMemBlockSize)))), OldAllocSize-NewAllocSize);
  266. end
  267. else
  268. begin
  269. { we're increasing the memory block size. First, find if there are free memory blocks immediately
  270. before and after our memory block. }
  271. after_block := FreeList;
  272. before_block := nil;
  273. while (after_block<>HeapPtr) and (TTinyHeapPointerArithmeticType(after_block) < TTinyHeapPointerArithmeticType(p)) do
  274. begin
  275. before_block := after_block;
  276. after_block := after_block^.Next;
  277. end;
  278. { is after_block immediately after our block? }
  279. if after_block=Pointer(TTinyHeapPointerArithmeticType(p)+(OldAllocSize-PtrUInt(SizeOf(TTinyHeapMemBlockSize)))) then
  280. begin
  281. if after_block = HeapPtr then
  282. after_block_size := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr))
  283. else
  284. after_block_size := DecodeTinyHeapFreeBlockSize(after_block^.size);
  285. end
  286. else
  287. after_block_size := 0;
  288. { is there enough room after the block? }
  289. if (OldAllocSize+after_block_size)>=NewAllocSize then
  290. begin
  291. if after_block = HeapPtr then
  292. begin
  293. HeapPtr:=Pointer(TTinyHeapPointerArithmeticType(HeapPtr)+(NewAllocSize-OldAllocSize));
  294. if assigned(before_block) then
  295. before_block^.Next := HeapPtr
  296. else
  297. FreeList := HeapPtr;
  298. end
  299. else
  300. begin
  301. if (NewAllocSize-OldAllocSize)=after_block_size then
  302. begin
  303. if assigned(before_block) then
  304. before_block^.Next := after_block^.Next
  305. else
  306. FreeList := after_block^.Next;
  307. end
  308. else
  309. begin
  310. new_after_block := PTinyHeapBlock(TTinyHeapPointerArithmeticType(after_block)+(NewAllocSize-OldAllocSize));
  311. new_after_block^.Next:=after_block^.Next;
  312. new_after_block^.Size:=EncodeTinyHeapFreeBlockSize(after_block_size-(NewAllocSize-OldAllocSize));
  313. if assigned(before_block) then
  314. before_block^.Next := new_after_block
  315. else
  316. FreeList := new_after_block;
  317. end;
  318. end;
  319. PTinyHeapMemBlockSize(p)[-1] := size;
  320. FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
  321. end
  322. else
  323. begin
  324. { is before_block immediately before our block? }
  325. //if assigned(before_block) and (Pointer(TTinyHeapPointerArithmeticType(before_block)+DecodeTinyHeapFreeBlockSize(before_block^.Size))=Pointer(TTinyHeapPointerArithmeticType(p)-SizeOf(TTinyHeapMemBlockSize))) then
  326. // ;
  327. result := AllocMem(size);
  328. if result <> nil then
  329. begin
  330. if oldsize > size then
  331. oldsize := size;
  332. move(pbyte(p)^, pbyte(result)^, oldsize);
  333. end;
  334. SysTinyFreeMem(p);
  335. p := result;
  336. end;
  337. end;
  338. end;
  339. {$ifdef DEBUG_TINY_HEAP}
  340. Writeln(HexStr(result));
  341. {$endif DEBUG_TINY_HEAP}
  342. end;
  343. function MemAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
  344. var
  345. p: PTinyHeapBlock;
  346. begin
  347. MemAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  348. if MemAvail > 0 then
  349. Dec(MemAvail, SizeOf(TTinyHeapMemBlockSize));
  350. p := FreeList;
  351. while p <> HeapPtr do
  352. begin
  353. Inc(MemAvail, DecodeTinyHeapFreeBlockSize(p^.Size)-SizeOf(TTinyHeapMemBlockSize));
  354. p := p^.Next;
  355. end;
  356. end;
  357. function MaxAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
  358. var
  359. p: PTinyHeapBlock;
  360. begin
  361. MaxAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  362. p := FreeList;
  363. while p <> HeapPtr do
  364. begin
  365. if DecodeTinyHeapFreeBlockSize(p^.Size) > MaxAvail then
  366. MaxAvail := DecodeTinyHeapFreeBlockSize(p^.Size);
  367. p := p^.Next;
  368. end;
  369. if MaxAvail > 0 then
  370. Dec(MaxAvail, SizeOf(TTinyHeapMemBlockSize));
  371. end;
  372. procedure Mark(var p: Pointer);
  373. begin
  374. p := HeapPtr;
  375. end;
  376. procedure Release(var p: Pointer);
  377. begin
  378. HeapPtr := p;
  379. FreeList := p;
  380. end;
  381. procedure InternalTinyAlign(var AAddress: Pointer; ASize: PtrUInt);
  382. var
  383. alignment_inc: smallint;
  384. begin
  385. alignment_inc := TTinyHeapPointerArithmeticType(align(AAddress,TinyHeapAllocGranularity))-TTinyHeapPointerArithmeticType(AAddress);
  386. Inc(AAddress,alignment_inc);
  387. Dec(ASize,alignment_inc);
  388. Dec(ASize,ASize mod TinyHeapAllocGranularity);
  389. end;
  390. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  391. the heap is only a single contiguous memory block. If you want to add
  392. multiple blocks to the heap, you should use RegisterTinyHeapBlock instead. }
  393. procedure RegisterTinyHeapBlock_Simple(AAddress: Pointer; ASize: PtrUInt);
  394. begin
  395. {$ifdef DEBUG_TINY_HEAP}
  396. Writeln('RegisterTinyHeapBlock_Simple(', HexStr(AAddress), ',', ASize, ')');
  397. {$endif DEBUG_TINY_HEAP}
  398. InternalTinyAlign(AAddress, ASize);
  399. HeapOrg:=AAddress;
  400. HeapPtr:=AAddress;
  401. FreeList:=AAddress;
  402. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  403. end;
  404. { Strongly simplified version of RegisterTinyHeapBlock, which can be used when
  405. the heap is only a single contiguous memory block and the address and size
  406. are already aligned on a TinyHeapAllocGranularity boundary. }
  407. procedure RegisterTinyHeapBlock_Simple_Prealigned(AAddress: Pointer; ASize: PtrUInt);
  408. begin
  409. {$ifdef DEBUG_TINY_HEAP}
  410. Writeln('RegisterTinyHeapBlock_Simple_Prealigned(', HexStr(AAddress), ',', ASize, ')');
  411. {$endif DEBUG_TINY_HEAP}
  412. HeapOrg:=AAddress;
  413. HeapPtr:=AAddress;
  414. FreeList:=AAddress;
  415. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  416. end;
  417. procedure RegisterTinyHeapBlock(AAddress: pointer; ASize: ptruint);
  418. var
  419. alignment_inc: smallint;
  420. p: PTinyHeapBlock;
  421. begin
  422. {$ifdef DEBUG_TINY_HEAP}
  423. Writeln('RegisterTinyHeapBlock(', HexStr(AAddress), ',', ASize, ')');
  424. {$endif DEBUG_TINY_HEAP}
  425. InternalTinyAlign(AAddress, ASize);
  426. if HeapOrg=nil then
  427. begin
  428. HeapOrg:=AAddress;
  429. HeapPtr:=AAddress;
  430. FreeList:=AAddress;
  431. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  432. end
  433. else
  434. begin
  435. if (TTinyHeapPointerArithmeticType(HeapOrg) > TTinyHeapPointerArithmeticType(AAddress)) then
  436. HeapOrg:=AAddress;
  437. if TTinyHeapPointerArithmeticType(AAddress) > TTinyHeapPointerArithmeticType(HeapEnd) then
  438. begin
  439. if TTinyHeapPointerArithmeticType(HeapPtr) = TTinyHeapPointerArithmeticType(HeapEnd) then
  440. begin
  441. if FreeList=HeapPtr then
  442. FreeList:=AAddress
  443. else
  444. begin
  445. p:=FreeList;
  446. while p^.Next<>HeapPtr do
  447. p:=p^.Next;
  448. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  449. end;
  450. end
  451. else
  452. begin
  453. PTinyHeapBlock(HeapPtr)^.Size:=EncodeTinyHeapFreeBlockSize(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
  454. PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
  455. end;
  456. HeapPtr:=AAddress;
  457. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
  458. end
  459. else if TTinyHeapPointerArithmeticType(AAddress) = TTinyHeapPointerArithmeticType(HeapEnd) then
  460. HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize)
  461. else
  462. InternalTinyFreeMem(AAddress, ASize);
  463. end;
  464. end;
  465. const
  466. TinyHeapMemoryManager: TMemoryManager = (
  467. NeedLock: false; // Obsolete
  468. GetMem: @SysTinyGetMem;
  469. FreeMem: @SysTinyFreeMem;
  470. FreeMemSize: @SysTinyFreeMemSize;
  471. AllocMem: @SysTinyAllocMem;
  472. ReAllocMem: @SysTinyReAllocMem;
  473. MemSize: @SysTinyMemSize;
  474. InitThread: nil;
  475. DoneThread: nil;
  476. RelocateHeap: nil;
  477. GetHeapStatus: nil;
  478. GetFPCHeapStatus: nil;
  479. );