-
Notifications
You must be signed in to change notification settings - Fork 90
/
Grijjy.MemoryPool.pas
160 lines (142 loc) · 3.16 KB
/
Grijjy.MemoryPool.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
unit Grijjy.MemoryPool;
{ A reusable memory pooling class }
{$I Grijjy.inc}
interface
uses
System.Classes,
System.SysUtils,
System.SyncObjs,
System.Generics.Collections;
const
MAX_BLOCKS_QUEUED = 1024;
type
TgoMemoryPool = class(TObject)
private
FBlockSize: Integer;
FMaxBlocksQueued: Integer;
FBlocks: TQueue<Pointer>;
FLock: TCriticalSection;
function GetCount: Integer;
function GetSize: Integer;
procedure Clear;
public
constructor Create(const ABlockSize: Integer; const AMaxBlocksQueued: Integer = MAX_BLOCKS_QUEUED);
destructor Destroy; override;
public
function RequestMem: Pointer; overload;
function RequestMem(const AName: String): Pointer; overload;
procedure ReleaseMem(P: Pointer); overload;
procedure ReleaseMem(P: Pointer; const AName: String); overload;
public
property BlockSize: Integer read FBlockSize;
property Count: Integer read GetCount;
property Size: Integer read GetSize;
end;
implementation
{ TgoMemoryPool }
constructor TgoMemoryPool.Create(const ABlockSize: Integer; const AMaxBlocksQueued: Integer = MAX_BLOCKS_QUEUED);
begin
FBlockSize := ABlockSize;
FMaxBlocksQueued := AMaxBlocksQueued;
FBlocks := TQueue<Pointer>.Create;
FLock := TCriticalSection.Create;
end;
destructor TgoMemoryPool.Destroy;
begin
Clear;
FLock.Enter;
try
FBlocks.Free;
finally
FLock.Leave;
end;
FLock.Free;
inherited Destroy;
end;
function TgoMemoryPool.RequestMem: Pointer;
begin
Result := nil;
FLock.Enter;
try
if FBlocks.Count > 0 then
Result := FBlocks.Dequeue;
finally
FLock.Leave;
end;
if Result = nil then
begin
GetMem(Result, FBlockSize);
if Result <> nil then
FillChar(Result^, FBlockSize, 0);
end;
end;
function TgoMemoryPool.RequestMem(const AName: String): Pointer;
begin
Result := nil;
FLock.Enter;
try
if FBlocks.Count > 0 then
Result := FBlocks.Dequeue;
finally
FLock.Leave;
end;
if Result = nil then
begin
GetMem(Result, FBlockSize);
if Result <> nil then
FillChar(Result^, FBlockSize, 0);
end;
end;
procedure TgoMemoryPool.ReleaseMem(P: Pointer);
begin
if P <> nil then
begin
FLock.Enter;
try
if FBlocks.Count < FMaxBlocksQueued then
begin
FBlocks.Enqueue(P);
Exit;
end;
finally
FLock.Leave;
end;
FreeMem(P);
end;
end;
procedure TgoMemoryPool.ReleaseMem(P: Pointer; const AName: String);
begin
if P <> nil then
begin
FLock.Enter;
try
if FBlocks.Count < FMaxBlocksQueued then
begin
FBlocks.Enqueue(P);
Exit;
end;
finally
FLock.Leave;
end;
FreeMem(P);
end;
end;
procedure TgoMemoryPool.Clear;
begin
FLock.Enter;
try
while FBlocks.Count > 0 do
FreeMem(FBlocks.Dequeue);
finally
FLock.Leave;
end;
end;
function TgoMemoryPool.GetCount: Integer;
begin
Result := FBlocks.Count;
end;
function TgoMemoryPool.GetSize: Integer;
begin
Result := FBlocks.Count * FBlockSize;
end;
end.