-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathboehmgc.pas
142 lines (110 loc) · 2.2 KB
/
boehmgc.pas
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
unit BoehmGC;
interface
function GCInstalled: Boolean;
implementation
const
LibName = 'libgc';
function GCMalloc(Size: PtrInt): Pointer; cdecl; external LibName name 'GC_malloc';
type
TByteArray = array [0 .. MaxInt] of Byte;
PPtrInt = ^PtrInt;
PByteArray = ^TByteArray;
function CMemSize(P: Pointer): PtrInt;
begin
if P = nil then
begin
Result := 0;
end
else
begin
Dec(P, SizeOf(PtrInt));
Result := PPtrInt(P)^;
end;
end;
function Min(A, B: PtrInt): PtrInt;
begin
if A > B then
begin
Result := B;
end
else
begin
Result := A;
end;
end;
function CGetMem(Size: PtrInt): Pointer;
begin
Result := GCMalloc(Size + SizeOf(PtrInt));
if Result <> nil then
begin
PPtrInt(Result)^ := Size;
Inc(Result, SizeOf(PtrInt));
end;
end;
function CFreeMem(P: Pointer): PtrInt;
begin
Result := 0; // Pretend to have free it...
end;
function CFreeMemSize(P: Pointer; Size: PtrInt): PtrInt;
begin
Result := CFreeMem(P);
end;
function CAllocMem(Size: PtrInt): Pointer;
begin
Result := CGetMem(Size);
end;
function CReAllocMem(var P: Pointer; Size: PtrInt): Pointer;
begin
if Size <> 0 then
begin
Result := CGetMem(Size);
Move(P^, Result^, Min(Size, CMemSize(P)) - 1);
end
else
begin
Result := nil;
end;
P := Result;
end;
function CGetHeapStatus:THeapStatus;
var
Res: THeapStatus;
begin
FillChar(Res, SizeOf(Res), 0);
Result := Res;
end;
function CGetFPCHeapStatus:TFPCHeapStatus;
var
Res: TFPCHeapStatus;
begin
FillChar(Res, SizeOf(Res), 0);
Result := Res;
end;
const
GCMemoryManager : TMemoryManager =
(
NeedLock : false;
GetMem : @CGetmem;
FreeMem : @CFreeMem;
FreememSize : @CFreememSize;
AllocMem : @CAllocMem;
ReallocMem : @CReAllocMem;
MemSize : @CMemSize;
GetHeapStatus : @CGetHeapStatus;
GetFPCHeapStatus: @CGetFPCHeapStatus;
);
function GCInstalled: Boolean;
var
Manager: TMemoryManager;
begin
GetMemoryManager(Manager);
Result := @Manager.GetMem = @CGetMem;
end;
var
OldMemoryManager: TMemoryManager;
initialization
GetMemoryManager (OldMemoryManager);
SetMemoryManager (GCmemoryManager);
finalization
SetMemoryManager (OldMemoryManager);
end.