-
Notifications
You must be signed in to change notification settings - Fork 1
/
fpc_jemalloc.pas
149 lines (118 loc) · 2.63 KB
/
fpc_jemalloc.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
147
148
Unit fpc_jemalloc;
Interface
{$LINKLIB jemalloc}
const
LibName = 'libjemalloc';
Function je_malloc(Size: ptruint): Pointer; cdecl; external LibName name 'je_malloc';
Procedure je_free(P: Pointer); cdecl; external LibName name 'je_free';
Function je_realloc(P: Pointer; Size: ptruint): Pointer; cdecl; external LibName name 'je_realloc';
Implementation
type pptruint = ^ptruint;
Function JEGetMem(Size: ptruint): Pointer;
begin
JEGetMem := je_malloc(Size + sizeof(ptruint));
if JEGetMem <> nil then
begin
pptrint(JEGetMem)^ := Size;
Inc(JEGetMem, sizeof(ptruint));
end;
end;
Function JEFreeMem(P: Pointer): ptruint;
begin
if P <> nil then
Dec(P, sizeof(ptruint));
je_free(P);
JEFreeMem := 0;
end;
Function JEFreeMemSize(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;
JEFreeMemSize := JEFreeMem(P);
end;
Function JEAllocMem(Size: ptruint): Pointer;
var
TotalSize: ptruint;
begin
TotalSize := Size + sizeof(ptruint);
JEAllocMem := je_malloc(TotalSize);
if JEAllocMem <> nil then
begin
FillByte(JEAllocMem, TotalSize, 0);
pptruint(JEAllocMem)^ := Size;
Inc(JEAllocMem, sizeof(ptruint));
end;
end;
Function JEReAllocMem(var P: Pointer; Size: ptruint): Pointer;
begin
if Size = 0 then
begin
if P <> Nil then
begin
Dec(P, sizeof(ptruint));
je_free(P);
P := nil;
end;
end
else
begin
Inc(Size, sizeof(ptruint));
if P = nil then
P := je_malloc(Size)
else
begin
Dec(P, sizeof(ptruint));
P := je_realloc(P, Size);
end;
if P <> nil then
begin
pptruint(P)^ := Size - sizeof(ptruint);
Inc(P, sizeof(ptruint));
end;
end;
JEReAllocMem := P;
end;
Function JEMemSize(P: Pointer): ptruint;
begin
JEMemSize := pptruint(P - sizeof(ptruint))^;
end;
{ TODO }
Function JEGetHeapStatus: THeapStatus;
begin
FillChar(JEGetHeapStatus, sizeof(JEGetHeapStatus), 0);
end;
Function JEGetFPCHeapStatus: TFPCHeapStatus;
begin
FillChar(JEGetFPCHeapStatus, sizeof(JEGetHeapStatus), 0);
end;
const JEMemoryManager: TMemoryManager =
(
NeedLock: false (* TODO: verify *);
GetMem: @JEGetMem;
FreeMem: @JEFreeMem;
FreeMemSize: @JEFreeMemSize;
AllocMem: @JEAllocMem;
ReAllocMem: @JEReAllocMem;
MemSize: @JEMemSize;
InitThread: Nil;
DoneThread: Nil;
RelocateHeap: Nil;
GetHeapStatus: @JEGetHeapStatus;
GetFPCHeapStatus: @JEGetFPCHeapStatus;
);
var PreviousMemoryManager: TMemoryManager;
Initialization
GetMemoryManager(PreviousMemoryManager);
SetMemoryManager(JEMemoryManager);
Finalization
SetMemoryManager(PreviousMemoryManager);
end.