1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2011 by the Free Pascal development team.
Tiny heap manager for the i8086 near heap, embedded targets, etc.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{ The heap, implemented here is TP7-compatible in the i8086 far data memory
models. It's basically a linked list of free blocks, which are kept ordered by
start address. The FreeList variable points to the start of the list. Each
free block, except the last one, contains a TTinyHeapBlock structure, which
holds the block size and a pointer to the next free block. The HeapPtr
variable points to the last free block, indicating the end of the list. The
last block is special in that it doesn't contain a TTinyHeapBlock structure.
Instead its size is determined by the pointer difference (HeapEnd-HeapPtr).
It *can* become zero sized, when all the memory inside of it is allocated, in
which case, HeapPtr will become equal to HeapEnd. }
{$ifdef FPC_TINYHEAP_HUGE}
{$HugePointerArithmeticNormalization On}
{$HugePointerComparisonNormalization On}
{$endif FPC_TINYHEAP_HUGE}
type
{ TTinyHeapMemBlockSize holds the size of an *allocated* memory block,
and is written at position:
memblockstart-sizeof(TTinyHeapMemBlockSize) }
PTinyHeapMemBlockSize = ^TTinyHeapMemBlockSize; {$ifdef FPC_TINYHEAP_HUGE}huge;{$endif}
TTinyHeapMemBlockSize = PtrUInt;
{ TTinyHeapFreeBlockSize holds the size of a *free* memory block, as a
part of the TTinyHeapBlock structure }
{$ifdef FPC_TINYHEAP_HUGE}
TTinyHeapFreeBlockSize = record
OfsSize: Word;
SegSize: Word;
end;
{$else FPC_TINYHEAP_HUGE}
TTinyHeapFreeBlockSize = PtrUInt;
{$endif FPC_TINYHEAP_HUGE}
TTinyHeapPointerArithmeticType = ^Byte; {$ifdef FPC_TINYHEAP_HUGE}huge;{$endif}
PTinyHeapBlock = ^TTinyHeapBlock;
TTinyHeapBlock = record
Next: PTinyHeapBlock;
Size: TTinyHeapFreeBlockSize;
end;
const
TinyHeapMinBlock = sizeof(TTinyHeapBlock);
TinyHeapMaxBlock = High(ptruint) - sizeof(TTinyHeapBlock) - sizeof(TTinyHeapMemBlockSize);
TinyHeapAllocGranularity = sizeof(TTinyHeapBlock);
procedure RegisterTinyHeapBlock(AAddress: Pointer; ASize:{$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif}); forward;
function EncodeTinyHeapFreeBlockSize(Size: PtrUInt): TTinyHeapFreeBlockSize; {$ifndef FPC_TINYHEAP_HUGE} inline; {$endif}
begin
{$ifdef FPC_TINYHEAP_HUGE}
EncodeTinyHeapFreeBlockSize.OfsSize := Size and 15;
EncodeTinyHeapFreeBlockSize.SegSize := Size shr 4;
{$else FPC_TINYHEAP_HUGE}
EncodeTinyHeapFreeBlockSize := Size;
{$endif FPC_TINYHEAP_HUGE}
end;
function DecodeTinyHeapFreeBlockSize(Size: TTinyHeapFreeBlockSize): PtrUInt; {$ifndef FPC_TINYHEAP_HUGE} inline; {$endif}
begin
{$ifdef FPC_TINYHEAP_HUGE}
DecodeTinyHeapFreeBlockSize := (PtrUInt(Size.SegSize) shl 4) + Size.OfsSize;
{$else FPC_TINYHEAP_HUGE}
DecodeTinyHeapFreeBlockSize := Size;
{$endif FPC_TINYHEAP_HUGE}
end;
procedure InternalTinyFreeMem(Addr: Pointer; Size: PtrUInt); forward;
function FindSize(p: pointer): TTinyHeapMemBlockSize;
begin
FindSize := PTinyHeapMemBlockSize(p)[-1];
end;
function SysGetMem(Size: ptruint): pointer;
var
p, prev, p2: PTinyHeapBlock;
AllocSize, RestSize: ptruint;
begin
{$ifdef DEBUG_TINY_HEAP}
Write('SysGetMem(', Size, ')=');
{$endif DEBUG_TINY_HEAP}
if size>TinyHeapMaxBlock then
HandleError(203);
AllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
p := FreeList;
prev := nil;
while (p<>HeapPtr) and (DecodeTinyHeapFreeBlockSize(p^.Size) < AllocSize) do
begin
prev := p;
p := p^.Next;
end;
if p<>HeapPtr then
begin
result := @PTinyHeapMemBlockSize(p)[1];
if DecodeTinyHeapFreeBlockSize(p^.Size)-AllocSize >= TinyHeapMinBlock then
RestSize := DecodeTinyHeapFreeBlockSize(p^.Size)-AllocSize
else
begin
AllocSize := DecodeTinyHeapFreeBlockSize(p^.Size);
RestSize := 0;
end;
if RestSize > 0 then
begin
p2 := pointer(TTinyHeapPointerArithmeticType(p)+AllocSize);
p2^.Next := p^.Next;
p2^.Size := EncodeTinyHeapFreeBlockSize(RestSize);
if prev = nil then
FreeList := p2
else
prev^.next := p2;
end
else
begin
if prev = nil then
FreeList := p^.Next
else
prev^.next := p^.next;
end;
PTinyHeapMemBlockSize(p)^ := size;
end
else
begin
{ p=HeapPtr }
if PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr))<AllocSize then
begin
{ align to 16 bytes }
AllocSize:= (AllocSize + $f) and (not $f);
p:=SysOSAlloc(AllocSize);
if assigned(p) then
begin
{ This needs toi be fixed because
HeapEnd and HeapSize are not updated correctly
if p > HeapPtr then
begin
prev:=HeapPtr;
HeapPtr:=p;
end
else }
begin
{$ifdef DEBUG_TINY_HEAP}
Writeln('SysAlloc returned: ',HexStr(p));
{$endif DEBUG_TINY_HEAP}
RegisterTinyHeapBlock(p,AllocSize);
{ Recursive call }
SysGetMem:=SysGetMem(Size);
exit;
end;
end
else
begin
if ReturnNilIfGrowHeapFails then
begin
Result := nil;
exit;
end
else
HandleError(203);
end;
end;
result := @PTinyHeapMemBlockSize(HeapPtr)[1];
PTinyHeapMemBlockSize(HeapPtr)^ := size;
HeapPtr := pointer(TTinyHeapPointerArithmeticType(HeapPtr)+AllocSize);
if prev = nil then
FreeList := HeapPtr
else
prev^.next := HeapPtr;
end;
{$ifdef DEBUG_TINY_HEAP}
Writeln(HexStr(Result));
{$endif DEBUG_TINY_HEAP}
end;
function TinyGetAlignedMem(Size, Alignment: ptruint): pointer;
var
mem: Pointer;
memp: ptruint;
begin
if alignment <= sizeof(pointer) then
result := GetMem(size)
else
begin
mem := GetMem(Size+Alignment-1);
memp := align(ptruint(mem), Alignment);
InternalTinyFreeMem(mem, TTinyHeapPointerArithmeticType(memp)-TTinyHeapPointerArithmeticType(mem));
result := pointer(memp);
end;
end;
procedure InternalTinyFreeMem(Addr: Pointer; Size: PtrUInt);
var
p, prev: PTinyHeapBlock;
begin
p := FreeList;
prev := nil;
while (p<>HeapPtr) and (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(Addr)) do
begin
prev := p;
p := p^.Next;
end;
{ join with previous block? }
if assigned(prev) and ((TTinyHeapPointerArithmeticType(prev)+DecodeTinyHeapFreeBlockSize(prev^.Size)) = TTinyHeapPointerArithmeticType(Addr)) then
begin
Addr:=prev;
Size:=DecodeTinyHeapFreeBlockSize(prev^.size)+Size;
end
else
if assigned(prev) then
prev^.Next := Addr
else
FreeList := Addr;
{ join with next block? }
if TTinyHeapPointerArithmeticType(p)=(TTinyHeapPointerArithmeticType(Addr)+Size) then
begin
if p=HeapPtr then
HeapPtr:=Addr
else
begin
PTinyHeapBlock(Addr)^.Next:=p^.Next;
PTinyHeapBlock(Addr)^.Size:=EncodeTinyHeapFreeBlockSize(Size+DecodeTinyHeapFreeBlockSize(p^.Size));
end;
end
else
begin
PTinyHeapBlock(Addr)^.Next:=p;
PTinyHeapBlock(Addr)^.Size:=EncodeTinyHeapFreeBlockSize(Size);
end;
end;
function SysFreeMem(p: Pointer): ptruint;
var
sz: ptruint;
begin
{$ifdef DEBUG_TINY_HEAP}
Writeln('SysFreeMem(', HexStr(p), ')');
{$endif DEBUG_TINY_HEAP}
if p=nil then
begin
result:=0;
exit;
end;
if (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(HeapOrg)) or
(TTinyHeapPointerArithmeticType(p) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
HandleError(204);
sz := Align(FindSize(p)+SizeOf(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
InternalTinyFreeMem(@PTinyHeapMemBlockSize(p)[-1], sz);
result := sz;
end;
function SysFreeMemSize(p: Pointer; Size: Ptruint): ptruint;
begin
result := SysFreeMem(p);
end;
function SysMemSize(p: pointer): ptruint;
begin
result := findsize(p);
end;
function SysTryResizeMem(var p: pointer; size: ptruint) : boolean;
begin
result := false;
end;
function SysAllocMem(size: ptruint): pointer;
begin
result := SysGetMem(size);
if result<>nil then
FillChar(result^,SysMemSize(result),0);
end;
function SysReAllocMem(var p: pointer; size: ptruint):pointer;
var
oldsize, OldAllocSize, NewAllocSize: ptruint;
after_block, before_block, before_before_block: PTinyHeapBlock;
after_block_size, before_block_size: PtrUInt;
new_after_block: PTinyHeapBlock;
begin
{$ifdef DEBUG_TINY_HEAP}
Write('SysReAllocMem(', HexStr(p), ',', size, ')=');
{$endif DEBUG_TINY_HEAP}
if size=0 then
begin
SysFreeMem(p);
result := nil;
p := nil;
end
else if p=nil then
begin
result := AllocMem(size);
p := result;
end
else
begin
if (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(HeapOrg)) or
(TTinyHeapPointerArithmeticType(p) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
HandleError(204);
if size>TinyHeapMaxBlock then
HandleError(203);
oldsize := FindSize(p);
OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
if OldAllocSize = NewAllocSize then
begin
{ old and new size are the same after alignment, so the memory block is already allocated }
{ we just need to update the size }
PTinyHeapMemBlockSize(p)[-1] := size;
if size > oldsize then
FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
end
else if OldAllocSize > NewAllocSize then
begin
{ we're decreasing the memory block size, so we can just free the remaining memory at the end }
PTinyHeapMemBlockSize(p)[-1] := size;
InternalTinyFreeMem(Pointer(TTinyHeapPointerArithmeticType(p)+(NewAllocSize-PtrUInt(SizeOf(TTinyHeapMemBlockSize)))), OldAllocSize-NewAllocSize);
end
else
begin
{ we're increasing the memory block size. First, find if there are free memory blocks immediately
before and after our memory block. }
after_block := FreeList;
before_block := nil;
before_before_block := nil;
while (after_block<>HeapPtr) and (TTinyHeapPointerArithmeticType(after_block) < TTinyHeapPointerArithmeticType(p)) do
begin
before_before_block := before_block;
before_block := after_block;
after_block := after_block^.Next;
end;
{ is after_block immediately after our block? }
if after_block=Pointer(TTinyHeapPointerArithmeticType(p)+(OldAllocSize-PtrUInt(SizeOf(TTinyHeapMemBlockSize)))) then
begin
if after_block = HeapPtr then
after_block_size := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr))
else
after_block_size := DecodeTinyHeapFreeBlockSize(after_block^.size);
end
else
after_block_size := 0;
{ is there enough room after the block? }
if (OldAllocSize+after_block_size)>=NewAllocSize then
begin
if after_block = HeapPtr then
begin
HeapPtr:=Pointer(TTinyHeapPointerArithmeticType(HeapPtr)+(NewAllocSize-OldAllocSize));
if assigned(before_block) then
before_block^.Next := HeapPtr
else
FreeList := HeapPtr;
end
else
begin
if (NewAllocSize-OldAllocSize)=after_block_size then
begin
if assigned(before_block) then
before_block^.Next := after_block^.Next
else
FreeList := after_block^.Next;
end
else
begin
new_after_block := PTinyHeapBlock(TTinyHeapPointerArithmeticType(after_block)+(NewAllocSize-OldAllocSize));
new_after_block^.Next:=after_block^.Next;
new_after_block^.Size:=EncodeTinyHeapFreeBlockSize(after_block_size-(NewAllocSize-OldAllocSize));
if assigned(before_block) then
before_block^.Next := new_after_block
else
FreeList := new_after_block;
end;
end;
PTinyHeapMemBlockSize(p)[-1] := size;
FillChar((TTinyHeapPointerArithmeticType(p)+oldsize)^, size-oldsize, 0);
end
else
begin
{ is before_block immediately before our block? }
if assigned(before_block) and (Pointer(TTinyHeapPointerArithmeticType(before_block)+DecodeTinyHeapFreeBlockSize(before_block^.Size))=Pointer(TTinyHeapPointerArithmeticType(p)-SizeOf(TTinyHeapMemBlockSize))) then
before_block_size := DecodeTinyHeapFreeBlockSize(before_block^.Size)
else
before_block_size := 0;
{ if there's enough space, we can slide our current block back and reclaim before_block }
if (before_block_size<NewAllocSize) and ((before_block_size+OldAllocSize+after_block_size)>=NewAllocSize) and
{ todo: implement this also for after_block_size>0 }
(after_block_size>0) then
begin
if (before_block_size+OldAllocSize+after_block_size)=NewAllocSize then
begin
if after_block=HeapPtr then
begin
HeapPtr := HeapEnd;
if assigned(before_before_block) then
before_before_block^.Next := HeapPtr
else
FreeList := HeapPtr;
end
else
if assigned(before_before_block) then
before_before_block^.Next := after_block^.Next
else
FreeList := after_block^.Next;
end;
Result := Pointer(TTinyHeapPointerArithmeticType(before_block)+SizeOf(TTinyHeapMemBlockSize));
Move(p^, Result^, oldsize);
PTinyHeapMemBlockSize(before_block)^ := size;
if (before_block_size+OldAllocSize+after_block_size)>NewAllocSize then
begin
new_after_block := PTinyHeapBlock(TTinyHeapPointerArithmeticType(before_block)+NewAllocSize);
new_after_block^.Next:=after_block^.Next;
new_after_block^.Size:=EncodeTinyHeapFreeBlockSize(before_block_size+after_block_size-(NewAllocSize-OldAllocSize));
if assigned(before_before_block) then
before_before_block^.Next := new_after_block
else
FreeList := new_after_block;
end;
FillChar((TTinyHeapPointerArithmeticType(Result)+oldsize)^, size-oldsize, 0);
p := Result;
end
else
begin
result := AllocMem(size);
if result <> nil then
begin
if oldsize > size then
oldsize := size;
move(pbyte(p)^, pbyte(result)^, oldsize);
end;
SysFreeMem(p);
p := result;
end;
end;
end;
end;
{$ifdef DEBUG_TINY_HEAP}
Writeln(HexStr(result));
{$endif DEBUG_TINY_HEAP}
end;
function MemAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
var
p: PTinyHeapBlock;
begin
MemAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
if MemAvail > 0 then
Dec(MemAvail, SizeOf(TTinyHeapMemBlockSize));
p := FreeList;
while p <> HeapPtr do
begin
Inc(MemAvail, DecodeTinyHeapFreeBlockSize(p^.Size)-SizeOf(TTinyHeapMemBlockSize));
p := p^.Next;
end;
end;
function MaxAvail: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif};
var
p: PTinyHeapBlock;
begin
MaxAvail := PtrUInt(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
p := FreeList;
while p <> HeapPtr do
begin
if DecodeTinyHeapFreeBlockSize(p^.Size) > MaxAvail then
MaxAvail := DecodeTinyHeapFreeBlockSize(p^.Size);
p := p^.Next;
end;
if MaxAvail > 0 then
Dec(MaxAvail, SizeOf(TTinyHeapMemBlockSize));
end;
procedure Mark(var p: Pointer);
begin
p := HeapPtr;
end;
procedure Release(var p: Pointer);
begin
HeapPtr := p;
FreeList := p;
end;
procedure InternalTinyAlign(var AAddress: Pointer; var ASize: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif});
var
alignment_inc: smallint;
begin
alignment_inc := TTinyHeapPointerArithmeticType(align(AAddress,TinyHeapAllocGranularity))-TTinyHeapPointerArithmeticType(AAddress);
Inc(AAddress,alignment_inc);
Dec(ASize,alignment_inc);
Dec(ASize,ASize mod TinyHeapAllocGranularity);
end;
{ Strongly simplified version of RegisterTinyHeapBlock, which can be used when
the heap is only a single contiguous memory block. If you want to add
multiple blocks to the heap, you should use RegisterTinyHeapBlock instead. }
procedure RegisterTinyHeapBlock_Simple(AAddress: Pointer; ASize:{$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif});
begin
{$ifdef DEBUG_TINY_HEAP}
Writeln('RegisterTinyHeapBlock_Simple(', HexStr(AAddress), ',', ASize, ')');
{$endif DEBUG_TINY_HEAP}
InternalTinyAlign(AAddress, ASize);
HeapSize:=HeapSize + ASize;
HeapOrg:=AAddress;
HeapPtr:=AAddress;
FreeList:=AAddress;
HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
end;
{ Strongly simplified version of RegisterTinyHeapBlock, which can be used when
the heap is only a single contiguous memory block and the address and size
are already aligned on a TinyHeapAllocGranularity boundary. }
procedure RegisterTinyHeapBlock_Simple_Prealigned(AAddress: Pointer; ASize: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif});
begin
{$ifdef DEBUG_TINY_HEAP}
Writeln('RegisterTinyHeapBlock_Simple_Prealigned(', HexStr(AAddress), ',', ASize, ')');
{$endif DEBUG_TINY_HEAP}
HeapOrg:=AAddress;
HeapPtr:=AAddress;
FreeList:=AAddress;
HeapSize:=HeapSize + ASize;
HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
end;
procedure RegisterTinyHeapBlock(AAddress: pointer; ASize: {$ifdef FPC_TINYHEAP_HUGE}LongInt{$else}PtrUInt{$endif});
var
alignment_inc: smallint;
p: PTinyHeapBlock;
begin
{$ifdef DEBUG_TINY_HEAP}
Writeln('RegisterTinyHeapBlock(', HexStr(AAddress), ',', ASize, ')');
{$endif DEBUG_TINY_HEAP}
InternalTinyAlign(AAddress, ASize);
HeapSize:=HeapSize + ASize;
if HeapOrg=nil then
begin
HeapOrg:=AAddress;
HeapPtr:=AAddress;
FreeList:=AAddress;
HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
end
else
begin
if (TTinyHeapPointerArithmeticType(HeapOrg) > TTinyHeapPointerArithmeticType(AAddress)) then
HeapOrg:=AAddress;
if TTinyHeapPointerArithmeticType(AAddress) > TTinyHeapPointerArithmeticType(HeapEnd) then
begin
if TTinyHeapPointerArithmeticType(HeapPtr) = TTinyHeapPointerArithmeticType(HeapEnd) then
begin
if FreeList=HeapPtr then
FreeList:=AAddress
else
begin
p:=FreeList;
while p^.Next<>HeapPtr do
p:=p^.Next;
PTinyHeapBlock(p)^.Next:=AAddress;
end;
end
else
begin
PTinyHeapBlock(HeapPtr)^.Size:=EncodeTinyHeapFreeBlockSize(TTinyHeapPointerArithmeticType(HeapEnd)-TTinyHeapPointerArithmeticType(HeapPtr));
PTinyHeapBlock(HeapPtr)^.Next:=AAddress;
end;
HeapPtr:=AAddress;
HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize);
end
else if TTinyHeapPointerArithmeticType(AAddress) = TTinyHeapPointerArithmeticType(HeapEnd) then
HeapEnd:=Pointer(TTinyHeapPointerArithmeticType(AAddress)+ASize)
else
InternalTinyFreeMem(AAddress, ASize);
end;
end;
function SysGetFPCHeapStatus : TFPCHeapStatus;
{
TFPCHeapStatus = record
MaxHeapSize,
MaxHeapUsed,
CurrHeapSize,
CurrHeapUsed,
CurrHeapFree : ptruint;
end;
}
begin
SysGetFPCHeapStatus.MaxHeapSize:=MaxAvail;
{ How can we compute this? }
SysGetFPCHeapStatus.MaxHeapUsed:=0;
SysGetFPCHeapStatus.CurrHeapFree:=MemAvail;
SysGetFPCHeapStatus.CurrHeapUsed:=HeapSize-SysGetFPCHeapStatus.CurrHeapFree;
SysGetFPCHeapStatus.CurrHeapSize:=HeapSize;
end;
function SysGetHeapStatus : THeapStatus;
begin
SysGetHeapStatus.TotalAddrSpace:= HeapSize;
SysGetHeapStatus.TotalUncommitted:= 0;
SysGetHeapStatus.TotalCommitted:= 0;
SysGetHeapStatus.TotalAllocated:= HeapSize-MemAvail;
SysGetHeapStatus.TotalFree:= MemAvail;
SysGetHeapStatus.FreeSmall:= 0;
SysGetHeapStatus.FreeBig:= 0;
SysGetHeapStatus.Unused:= 0;
SysGetHeapStatus.Overhead:= 0;
SysGetHeapStatus.HeapErrorCode:= 0;
end;
procedure FinalizeHeap;
begin
end;
|