-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtmem.pas.mcp
245 lines (211 loc) · 6.96 KB
/
adtmem.pas.mcp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
(* This file is a part of the PascalAdt library, which provides
commonly used algorithms and data structures for the FPC and Delphi
compilers.
Copyright (C) 2004 by Lukasz Czajka
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA *)
unit adtmem;
{ This unit provides facilities for memory-management. }
interface
&include adtdefs.inc
type
PGCNode = ^TGCNode;
TGCNode = record
obj : TObject;
next : PGCNode;
end;
{ a handle to an object registered in TGrabageCollector }
TCollectorObjectHandle = PGCNode;
{ A quasi-(grabage collector). This is used in container classes to
keep track of their iterators. The idea is that each container
object creates its own TGrabageCollector and uses it to track its
iterators. When a container is destroyed or all iterators are
invalidated by some operation, it calls FreeObjects to destroy
all iterators. Each iterator calls UnregisterObject in its
destructor, so that it is also safe (and efficient) to destroy
them manually. This will work as long as you don't destroy (or
otherwise use) 'grabage-collected' objects in another
grabage-collected object's destructor. If you do this the
behaviour is undefined!!! }
TGrabageCollector = class
private
StartNode : PGCNode;
FinishNode : PGCNode;
FCapacity : Cardinal;
{ this field is set to true in a method that modifies the state
of TGrabageCollector object. It is also checked at the
beginning of such methods and if already set to true the
method is exitted. This is to allow the object being destroyed
to call these methods from its destructor. When the object is
destroyed by the user they will be normally executed. When it
is destroyed from within TGrabageCollector they are not
executed to avoid infinite recursion. }
IsInMethod : Boolean;
{$ifdef TEST_PASCAL_ADT }
FRegistered : Cardinal;
{$endif TEST_PASCAL_ADT }
procedure PreallocateNodes(howmany : Cardinal);
public
constructor Create;
destructor Destroy; override;
{ Registers obj in grabage collector. Returns handle which can
be later used to unregister the object. Do NOT call this
method when obj is already registered within any grabage
collector! }
function RegisterObject(const obj : TObject) : TCollectorObjectHandle;
{ unregisters the object associated with the given
handle. Returns the unregistered object. }
function UnregisterObject(handle : TCollectorObjectHandle) : TObject;
{ returns object corresponding to the given handle. }
function GetObject(handle : TCollectorObjectHandle) : TObject;
{ destroys all registered objects. All handles that had been
returned from RegisterObject are no longer valid }
procedure FreeObjects;
{ returns true if the piece of code which calls it was invoked
from inside this grabage collector; htis may be useful to
avoid destroying grabage-collected objects in a destructor of
another grabage-collected object, when the destructor is
invoked from a grabage collector, but to destroy them
otherwise }
property IsInGrabageCollector : Boolean read IsInMethod;
{$ifdef TEST_PASCAL_ADT }
property RegisteredObjects : Cardinal read FRegistered;
{$endif TEST_PASCAL_ADT }
end;
implementation
uses
adtmsg;
const
InitialGrabageNodes = 32;
{ ------------------------ TGrabageCollector ----------------------------- }
procedure TGrabageCollector.PreallocateNodes(howmany : Cardinal);
var
i : Integer;
node : PGCNode;
begin
Assert(FinishNode^.Next = nil, msgInternalError);
node := FinishNode;
for i := 1 to howmany do
begin
New(node^.Next); { may raise }
node := node^.Next;
node^.obj := nil;
node^.next := nil; { in case of an exception }
end;
end;
constructor TGrabageCollector.Create;
begin
New(StartNode);
StartNode^.obj := nil;
StartNode^.Next := nil;
FinishNode := StartNode;
FCapacity := InitialGrabageNodes + 1;
PreallocateNodes(FCapacity - 1);
{ IsInMethod := false;}
{FRegistered := 0;}
end;
destructor TGrabageCollector.Destroy;
var
nnode : PGCNode;
begin
if IsInMethod then
Exit;
IsInMethod := true;
try
while StartNode <> FinishNode do
begin
StartNode^.obj.Free; { to avoid destroying nil objs }
nnode := StartNode^.Next;
Dispose(StartNode);
StartNode := nnode;
end;
while StartNode <> nil do
begin
nnode := StartNode^.Next;
Dispose(StartNode);
StartNode := nnode;
end;
finally
IsInMethod := false;
end;
end;
function TGrabageCollector.RegisterObject(const obj :
TObject) : TCollectorObjectHandle;
begin
if IsInMethod then
begin
Result := nil;
Exit;
end;
if FinishNode^.Next = nil then
begin
PreallocateNodes(FCapacity);
FCapacity := FCapacity + FCapacity;
end;
FinishNode^.obj := obj;
Result := FinishNode;
FinishNode := FinishNode^.Next;
{$ifdef TEST_PASCAL_ADT }
Inc(FRegistered);
{$endif TEST_PASCAL_ADT }
end;
function TGrabageCollector.
UnregisterObject(handle : TCollectorObjectHandle) : TObject;
begin
if IsInMethod then
begin
Result := nil;
Exit;
end;
with PGCNode(handle)^ do
begin
Result := obj;
obj := nil;
end;
{$ifdef TEST_PASCAL_ADT }
Dec(FRegistered);
{$endif TEST_PASCAL_ADT }
end;
function TGrabageCollector.GetObject(handle : TCollectorObjectHandle) : TObject;
begin
Assert((handle <> nil) and (PGCNode(handle)^.Next <> nil));
Result := PGCNode(handle)^.obj;
end;
procedure TGrabageCollector.FreeObjects;
var
node : PGCNode;
begin
if IsInMethod then
Exit;
IsInMethod := true;
try
node := StartNode;
while node <> FinishNode do
begin
with node^ do
begin
obj.Free; { Free needed instead of Destroy to avoid
previously unregistered nil objs }
obj := nil;
{$ifdef TEST_PASCAL_ADT }
Dec(FRegistered);
{$endif TEST_PASCAL_ADT }
end;
node := node^.Next;
end;
FinishNode := StartNode;
finally
IsInMethod := false;
end;
end;
end.