-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcmalloc.pas
116 lines (90 loc) · 2.76 KB
/
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
{
tcmalloc.pas
This unit installs a custom memory manager that delegates all memory management
to the TCMalloc.dll file
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at https://mozilla.org/MPL/2.0/.
}
unit tcmalloc;
interface
{$DEBUGINFO OFF}
const
TCMallocDll = 'libtcmalloc.dll';
procedure ReleaseFreeMemory;
procedure SetMemoryReleaseRate(rate: Double);
function GetMemoryReleaseRate: Double;
// returns some details about the current allocated memory.
// Note that end of lines are in the Linux format
function GetStats(Detailed: Boolean = False): string;
implementation
type
size_t = NativeUInt;
function tc_malloc(Size: size_t): Pointer; cdecl; external TCMallocDLL;
function tc_calloc(Num: size_t; Size: size_t): Pointer; cdecl; external TCMallocDLL;
function tc_realloc(P: Pointer; Size: size_t): Pointer; cdecl; external TCMallocDLL;
procedure tc_free(P: Pointer); cdecl; external TCMallocDLL;
procedure MallocExtension_ReleaseFreeMemory(); cdecl; external TCMallocDLL;
procedure MallocExtension_SetMemoryReleaseRate(rate: Double); cdecl; external TCMallocDLL;
function MallocExtension_GetMemoryReleaseRate: Double; cdecl; external TCMallocDLL;
procedure MallocExtension_GetStats(Buffer: PAnsiChar; Length: Integer); cdecl; external TCMallocDLL;
procedure ReleaseFreeMemory;
begin
MallocExtension_ReleaseFreeMemory;
end;
procedure SetMemoryReleaseRate(rate: Double);
begin
MallocExtension_SetMemoryReleaseRate(rate);
end;
function GetMemoryReleaseRate: Double;
begin
Result := MallocExtension_GetMemoryReleaseRate;
end;
function GetStats(Detailed: Boolean = False): string;
var
Buffer: TArray<AnsiChar>;
begin
if Detailed then
SetLength(Buffer, 20 * 1024)
else
SetLength(Buffer, 4 * 1024);
MallocExtension_GetStats(@Buffer[0], Length(Buffer));
Result := string(PAnsiChar(@Buffer[0]));
end;
function GetMem(Size: NativeInt): Pointer;
begin
Result := tc_malloc(size);
end;
function FreeMem(P: Pointer): Integer;
begin
tc_free(P);
Result := 0;
end;
function ReallocMem(P: Pointer; Size: NativeInt): Pointer;
begin
Result := tc_realloc(P, Size);
end;
function AllocMem(Size: NativeInt): Pointer;
begin
Result := tc_calloc(1, Size);
end;
function RegisterUnregisterExpectedMemoryLeak(P: Pointer): Boolean;
begin
Result := False;
end;
const
MemoryManager: TMemoryManagerEx = (
GetMem: GetMem;
FreeMem: FreeMem;
ReallocMem: ReallocMem;
AllocMem: AllocMem;
RegisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak;
UnregisterExpectedMemoryLeak: RegisterUnregisterExpectedMemoryLeak
);
procedure InstallTCMalloc;
begin
SetMemoryManager(MemoryManager);
end;
initialization
InstallTCMalloc;
end.