forked from JBontes/Life32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastObject6.pas
295 lines (247 loc) · 7.6 KB
/
FastObject6.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
unit FastObject6;
interface
uses Classes, LifeConst;
type
TFastObject = class(TPersistent)
public
class procedure InitStorageFacility(AClass: TClass);
class procedure FreeStorageFacility;
class function NewInstance: TObject; override;
procedure FreeInstance; override;
class function Create: pointer;
end;
var
BlockSize: integer = DefaultMemPerBlock;
implementation //##########################
uses SysUtils, Windows, LifeCel, Prefs;
const
CacheSize = 100;
type
//used to directly manipulate the properties of a list
//@@@@ Big hack alert.
THackList = class(TObject)
private
FList: PPointerList;
FCount: Integer;
end;
PPointer = ^Pointer;
PCardinal = ^Cardinal;
PBrick = ^TBrick;
TBrick = record
NextBrick: PBrick;
end;
PBlock = ^TBlock;
TBlock = record
NextBrick: PBrick;
FBricksInUse: Cardinal;
Next: PBlock;
Prev: PBlock;
CacheNext: PBlock;
CacheCount: integer;
end;
TStorageFacility = class(TObject)
private
//FUsedBlocks: TList; //list of TStorageBlocks in use
FFreeBlocks: TList; //List of available Blocks in virtual memory.
DataBuffer: PBlock;
BlockList: TBlock;
VirtualSize: Cardinal;
public
constructor Create(ObjectSize, ObjectCount: Cardinal);
destructor Destroy; override;
function NewBlock: PBlock;
function CreateBrick: TFastObject;
procedure FreeBrick(ABrick: TFastObject);
end;
(********************** TStorageFacility **************)
var
BrickSize: Cardinal; //StorageFacility only works with same sized blocks!
//BrickSize holds the InstanceSize of the Objects
//that will be stored in the block.
BricksPerBlock: Cardinal;
constructor TStorageFacility.Create(ObjectSize, ObjectCount: Cardinal);
var
i,b: cardinal;
MemStatus: TMemoryStatus;
begin
inherited Create;
BrickSize:= ObjectSize;
BricksPerBlock:= BlockSize div BrickSize;
//Reserve space in virtual memory, 2x physical mem, so the comp. will
//start trashing horrible before running out of mem.
//Instead of giving an error.
GlobalMemoryStatus(MemStatus);
VirtualSize:= MemStatus.dwTotalPhys * 2;
repeat
DataBuffer:=
PBlock(VirtualAlloc(nil,VirtualSize,MEM_RESERVE or MEM_COMMIT {or MEM_TOP_DOWN}, PAGE_READWRITE));
//Actual physical pages are not allocated unless/until the virtual addresses are actually accessed.
if (DataBuffer = nil) then VirtualSize:= VirtualSize div 2;
until (DataBuffer <> nil);
i:= VirtualSize div BlockSize;
//init the list of used blocks.
BlockList.Next:= @BlockList;
BlockList.Prev:= @BlockList;
//BlockList.CacheNext:= nil;
//Init the list of free blocks.
FFreeBlocks:= TList.Create;
FFreeBlocks.Capacity:= i;
while i > 0 do begin
Dec(i);
b:= cardinal(DataBuffer) + (i * BlockSize);
FFreeBlocks.Add(pointer(b));
end;
//Create one block to start with.
NewBlock;
end;
destructor TStorageFacility.Destroy;
begin
VirtualFree(DataBuffer,VirtualSize,MEM_DECOMMIT);
VirtualFree(DataBuffer,0,MEM_RELEASE);
//The data pointed to by blocklist is already freed.
BlockList.Next:= @BlockList;
BlockList.Prev:= @BlockList;
FFreeBlocks.Free;
inherited;
end;
function TStorageFacility.NewBlock: PBlock;
var
i, last: cardinal;
adres: cardinal;
ABrick: PBrick;
begin
//Are there any blocks in the cache?
if (BlockList.CacheCount > 0) then begin
Result:= BlockList.CacheNext;
BlockList.CacheNext:= Result^.CacheNext;
Dec(BlockList.CacheCount)
end
//no, create a new block
else begin
//Get a adres from the list of free blocks.
Last:= FFreeBlocks.count - 1;
pointer(Adres):= FFreeBlocks[Last];
Dec(THackList(FFreeBlocks).FCount); //FFreeBlocks.Delete(Last);
//get a ZERO-initialized block from our vitual storage.
//Result:= PBlock(VirtualAlloc(pointer(Adres),BlockSize,MEM_COMMIT,PAGE_READWRITE));
Result:= pointer(adres);
end; {else}
//(re)Initialize the block structure.
//Use counter is already 0.
//This speeds UP the action by improving the cache-characteristics of
//the Block/brick structure.
ABrick:= PBrick(Result);
Adres:= integer(ABrick);
for i:= 1 to BricksPerBlock - 1 do begin
adres:= adres + BrickSize;
ABrick^.NextBrick:= PBrick(adres);
cardinal(ABrick):= adres;
end;
ABrick^.NextBrick:= nil;
//Add the block at the end the list of used blocks.
//FUsedBlocks.Add(Result);
Result^.Next:= @BlockList;
Result^.Prev:= BlockList.Prev;
BlockList.Prev:= Result;
Result^.Prev^.Next:= Result;
end;
function TStorageFacility.CreateBrick: TFastObject;
var
ABlock: PBlock;
begin
//Get a block from the used list.
ABlock:= BlockList.Prev;
//used list is empty, create a new block.
if (ABlock = @BlockList) then ABlock:= NewBlock;
Result:= TFastObject(ABlock^.NextBrick);
//update the internal bookkeeping.
ABlock^.NextBrick:= ABlock^.NextBrick^.NextBrick;
Inc(ABlock^.FBricksInUse);
//if Block is full then remove it from the usedlist.
if ABlock^.NextBrick = nil then begin
ABlock^.Prev^.Next:= ABlock^.Next;
ABlock^.Next^.Prev:= ABlock^.Prev;
end;
end;
procedure TStorageFacility.FreeBrick(ABrick: TFastObject);
var
ABlock: PBlock;
const
Empty = 0;
begin
//How do we know which Block this object belongs to?
//Easy, it points towards its storageblock.
//each block starts on a 8k boundary.
//Just strip off the bottom bits, and we've got the starting address.
Cardinal(ABlock):= Cardinal(ABrick) and (-BlockSize);
Dec(ABlock^.FBricksInUse);
//Is the block empty?
if (ABlock^.FBricksInUse = Empty) then begin
//if so, remove the block from the Used list.
ABlock^.Prev^.Next:= ABlock^.Next;
ABlock^.Next^.Prev:= ABlock^.Prev;
//Can we add it to the cache?
if (BlockList.CacheCount < CacheSize) then begin
ABlock^.CacheNext:= BlockList.CacheNext;
BlockList.CacheNext:= ABlock;
Inc(BlockList.CacheCount);
//Add the brick to the list of free bricks.
PBrick(ABrick)^.NextBrick:= ABlock^.NextBrick;
ABlock^.NextBrick:= PBrick(ABrick);
end
else begin //no, free the block.
//update bookkeeping of freeblocks.
VirtualFree(ABlock,BlockSize,MEM_DECOMMIT);
THackList(FFreeBlocks).FList^[THackList(FFreeBlocks).FCount]:= ABlock;
Inc(THackList(FFreeBlocks).FCount); //FFreeBlocks.Add(ABlock);
end;
end {if}
//Not empty, lets go on.
else begin
//WAS the block fully used?, if so it need to be added,
//to the START of the used list.
if ABlock^.NextBrick = nil then begin
ABlock^.Prev:= @BlockList;
ABlock^.Next:= BlockList.Next;
BlockList.Next:= ABlock;
ABlock^.Next^.Prev:= ABlock;
end;
//Add the brick to the list of free bricks.
PBrick(ABrick)^.NextBrick:= ABlock^.NextBrick;
ABlock^.NextBrick:= PBrick(ABrick);
end;
end;
(********************** TStorageBlock *****************)
{ TFastTinyObject }
const
FStorage: TStorageFacility = nil;
class procedure TFastObject.InitStorageFacility(AClass: TClass);
begin
FStorage:= TStorageFacility.Create(AClass.InstanceSize, BricksPerBlock);
end;
class procedure TFastObject.FreeStorageFacility;
begin
FStorage.Free;
FStorage:= nil;
end;
class function TFastObject.NewInstance: TObject;
type
PClass = ^TClass;
begin
Result:= FStorage.CreateBrick;
PClass(Result)^:= Self; // Init VMT, don't init fields.
end;
class function TFastObject.Create: pointer;
begin
Result:= NewInstance;
end;
procedure TFastObject.FreeInstance;
begin
FStorage.FreeBrick(Self);
end;
initialization
BlockSize:= GetMemPerBlock;
TFastObject.InitStorageFacility(TLifeCel);
finalization
TFastObject.FreeStorageFacility;
end.