-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpc_tcmalloc.pas
147 lines (118 loc) · 2.78 KB
/
fpc_tcmalloc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Unit fpc_tcmalloc;
Interface
{$LINKLIB tcmalloc_minimal}
const
LibName = 'libtcmalloc_minimal';
Function tc_malloc (Size: ptruint): Pointer; cdecl; external LibName name 'tc_malloc';
Procedure tc_free (P: Pointer); cdecl; external LibName name 'tc_free';
Function tc_realloc(P: Pointer; Size: ptruint): Pointer; cdecl; external LibName name 'tc_realloc';
Implementation
type pptruint = ^ptruint;
Function TCGetMem(Size: ptruint): Pointer;
begin
TCGetMem := tc_malloc(Size + sizeof(ptruint));
if (TCGetMem <> nil) then
begin
pptrint(TCGetMem)^ := Size;
Inc(TCGetMem, sizeof(ptruint));
end;
end;
Function TCFreeMem(P: Pointer): ptruint;
begin
If (P <> nil) then
Dec(P, sizeof(ptruint));
tc_free(P);
TCFreeMem := 0;
end;
Function TCFreeMemSize(P: Pointer; Size: ptruint): ptruint;
begin
if Size <= 0 then
begin
if Size = 0 then
exit;
runerror(204);
end;
if P <> nil then
begin
if Size <> pptruint(P - sizeof(ptruint))^ then
runerror(204);
end;
TCFreeMemSize := TCFreeMem(P);
end;
Function TCAllocMem(Size: ptruint): Pointer;
var
TotalSize: ptruint;
begin
TotalSize := Size + sizeof(ptruint);
TCAllocMem := tc_malloc(TotalSize);
if TCAllocMem <> nil then
begin
FillByte(TCAllocMem, TotalSize, 0);
pptruint(TCAllocMem)^ := Size;
Inc(TCAllocMem, sizeof(ptruint));
end;
end;
Function TCReAllocMem(var P: Pointer; Size: ptruint): Pointer;
begin
if Size = 0 then
begin
if P <> nil then
begin
dec(P, sizeof(ptruint));
tc_free(P);
P := nil;
end;
end
else
begin
Inc(Size, sizeof(ptruint));
if P = nil then
P := tc_malloc(Size)
else
begin
Dec(P, sizeof(ptruint));
P := tc_realloc(P, Size);
end;
if P <> nil then
begin
pptruint(p)^ := Size - sizeof(ptruint);
Inc(P, sizeof(ptruint));
end;
end;
TCReallocMem := P;
end;
Function TCMemSize(P: Pointer): ptruint;
begin
TCMemSize := pptruint(P - sizeof(ptruint))^;
end;
(* TODO! *)
Function TCGetHeapStatus: THeapStatus;
begin
FillChar(TCGetHeapStatus, sizeof(TCGetHeapStatus), 0);
end;
Function TCGetFPCHeapStatus: TFPCHeapStatus;
begin
FillChar(TCGetFPCHeapStatus, sizeof(TCGetFPCHeapStatus), 0);
end;
Const TCMemoryManager: TMemoryManager =
(
NeedLock: false;
GetMem: @TCGetMem;
FreeMem: @TCFreeMem;
FreeMemSize: @TCFreeMemSize;
AllocMem: @TCAllocMem;
ReallocMem: @TCReAllocMem;
MemSize: @TCMemSize;
InitThread: Nil;
DoneThread: Nil;
RelocateHeap: Nil;
GetHeapStatus: @TCGetHeapStatus;
GetFPCHeapStatus: @TCGetFPCHeapStatus
);
Var PreviousMemoryManager: TMemoryManager;
Initialization
GetMemoryManager(PreviousMemoryManager);
SetMemoryManager(TCMemoryManager);
Finalization
SetMemoryManager(PreviousMemoryManager);
end.