-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbbmalloc.pas
172 lines (135 loc) · 3.93 KB
/
tbbmalloc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
unit tbbmalloc;
interface
uses
windows;
const
CTbbMallocDll = 'tbbmalloc.dll';
// ScalableAllocationResult
TBBMALLOC_OK = 0;
TBBMALLOC_INVALID_PARAM = 1;
TBBMALLOC_UNSUPPORTED = 2;
TBBMALLOC_NO_MEMORY = 3;
TBBMALLOC_NO_EFFECT = 4;
// AllocationModeParam
TBBMALLOC_USE_HUGE_PAGES = 0; // value turns using huge pages on and off
// ScalableAllocationCmd
TBBMALLOC_CLEAN_ALL_BUFFERS = 0; // Clean internal allocator buffers for all threads.
TBBMALLOC_CLEAN_THREAD_BUFFERS = 1; // Clean internal allocator buffer for current thread only.
// Set allocator-specific allocation modes.
function scalable_allocation_mode(param: Integer; value: PtrInt): Integer; cdecl; external CTbbMallocDll;
// Call allocator-specific commands.
function scalable_allocation_command(cmd: Integer; param: Pointer): Integer; cdecl; external CTbbMallocDll;
implementation
function scalable_malloc (size: PtrInt): Pointer; cdecl; external CTbbMallocDll;
procedure scalable_free (ptr: Pointer); cdecl; external CTbbMallocDll;
function scalable_realloc (ptr: Pointer; size: PtrInt): Pointer; cdecl; external CTbbMallocDll;
function scalable_calloc (nobj: PtrInt; size: PtrInt): Pointer; cdecl; external CTbbMallocDll;
function scalable_msize (ptr: Pointer): PtrInt; cdecl; external CTbbMallocDll;
function FastGetLastError: Cardinal; assembler;
asm
{$IFDEF WIN32}
mov eax, fs:[$34]
{$ENDIF WIN32}
{$IFDEF WIN64}
mov rax, gs:[$68]
{$ENDIF WIN64}
end;
function TbbAllocMem(Size: PtrUInt): Pointer;
var
LastError: Cardinal;
begin
LastError := FastGetLastError;
Result := scalable_calloc(1, Size);
if LastError <> FastGetLastError then
SetLastError(LastError);
end;
function TbbGetMem(Size: PtrUInt): Pointer;
var
LastError: Cardinal;
begin
LastError := FastGetLastError;
Result := scalable_malloc(Size);
if LastError <> FastGetLastError then
SetLastError(LastError);
end;
function TbbReallocMem(var P: Pointer; Size: PtrUInt): Pointer;
var
LastError: Cardinal;
begin
LastError := FastGetLastError;
if Assigned(P) then
begin
P := scalable_realloc(P, Size);
end
else
begin
P := scalable_malloc(Size)
end;
Result := P;
if LastError <> FastGetLastError then
SetLastError(LastError);
end;
function TbbFreeMem(P: Pointer): PtrUInt;
var
LastError: Cardinal;
begin
Result := 0;
LastError := FastGetLastError;
if Assigned(P) then
scalable_free(P);
if LastError <> FastGetLastError then
SetLastError(LastError);
end;
function TbbFreeMemSize(P: Pointer; Size:ptruint): PtrUInt;
var
LastError: Cardinal;
begin
Result := 0;
LastError := FastGetLastError;
if Assigned(P) and (Size <> 0) then
scalable_free(P);
if LastError <> FastGetLastError then
SetLastError(LastError);
end;
function TbbMemSize(P: Pointer): PtrUInt;
var
LastError: Cardinal;
begin
Result := 0;
LastError := FastGetLastError;
Result := scalable_msize(P);
if LastError <> FastGetLastError then
SetLastError(LastError);
end;
function TbbGetHeapStatus:THeapStatus;
begin
fillchar(Result, sizeof(Result), 0);
end;
function TbbGetFPCHeapStatus:TFPCHeapStatus;
begin
fillchar(Result, sizeof(Result), 0);
end;
Const
TbbMemMgr : TMemoryManager = (
NeedLock : false;
GetMem : @TbbGetMem;
FreeMem : @TbbFreeMem;
FreememSize : @TbbFreeMemSize;
AllocMem : @TbbAllocMem;
ReallocMem : @TbbReallocMem;
MemSize : @TbbMemSize;
InitThread : nil;
DoneThread : nil;
RelocateHeap : nil;
GetHeapStatus : @TbbGetHeapStatus;
GetFPCHeapStatus: @TbbGetFPCHeapStatus;
);
Var
OldMemoryManager : TMemoryManager;
Initialization
GetMemoryManager(OldMemoryManager);
scalable_allocation_mode(TBBMALLOC_USE_HUGE_PAGES, 1);
SetMemoryManager(TbbMemMgr);
Finalization
SetMemoryManager(OldMemoryManager);
end.