-
Notifications
You must be signed in to change notification settings - Fork 4
/
GoldSrc.Collections.pas
336 lines (263 loc) · 8.05 KB
/
GoldSrc.Collections.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
(*=========== (C) Copyright 2019, Alexander B. All rights reserved. ===========*)
(* *)
(* Module: *)
(* GoldSrc.Collections *)
(* *)
(* License: *)
(* You may freely use this code provided you retain this copyright message. *)
(* *)
(* Description: *)
(*=============================================================================*)
unit GoldSrc.Collections;
{$POINTERMATH ON}
interface
procedure CopyConstruct(var Memory: Pointer; const Src); inline;
function UtlMemory_CalcNewAllocationCount(AllocationCount, GrowSize, NewSize, BytesItem: Integer): Integer;
type
TUtlMemoryAlloc = function(Size: Integer): Pointer;
procedure UtlMemory_PushMemAlloc(Func: TUtlMemoryAlloc);
procedure UtlMemory_PopMemAlloc;
function malloc(Size: Integer): Pointer cdecl; external 'msvcrt';
function realloc(P: Pointer; Size: Integer): Pointer cdecl; external 'msvcrt';
procedure free(P: Pointer) cdecl; external 'msvcrt';
type
CUtlMemory<T, I { = Integer}> = record
public type PT = ^T;
private
function TI(const Value: Integer): Integer; inline;
function GetElement(Index: Integer): PT;
public
FMemory: PT;
FAllocationCount: Integer;
FGrowSize: Integer;
constructor Create(GrowSize: Integer; InitSize: Integer); overload;
constructor Create(Memory: PT); overload;
constructor Create(const Memory: PT; NumElements: Integer); overload;
procedure Init(GrowSize: Integer = 0; InitSize: Integer = 0);
procedure Grow(Number: Integer = 1);
function IsExternallyAllocated: Boolean;
property Element[Index: Integer]: PT read GetElement; default;
property NumAllocated: Integer read FAllocationCount;
end;
CUtlVector<T, A { = Integer}> = record
public type PT = ^T;
public
Base: CUtlMemory<T, Integer>;
FSize: Integer;
FElements: PT;
procedure GrowVector(Number: Integer = 1);
function IsValidIndex(Idx: Integer): Boolean; inline;
function InsertBefore(Elem: Integer; const Src: T): Integer;
procedure ShiftElementsRight(Elem: Integer; Number: Integer = 1);
function AddToTail(const Src: T): Integer;
end;
type
UtlSymId_t = Word;
TUtlSymId = UtlSymId_t;
const
MAX_STRING_POOL_SIZE = 256;
UTL_INVAL_SYMBOL: TUtlSymId = TUtlSymId(not 0);
INVALID_STRING_INDEX: Cardinal = Cardinal(-1);
type
PCUtlSymbolTable = ^CUtlSymbolTable;
CUtlSymbolTable = record
end;
type
LessCtx_t = record
UserString: PAnsiChar;
Table: PCUtlSymbolTable;
end;
TLessCtx = LessCtx_t;
PLessCtx = ^TLessCtx;
type
CUtlSymbol = record
FID: TUtlSymId;
constructor Create(ID: TUtlSymId); overload;
constructor Create(Str: PAnsiChar); overload;
constructor Create(const Sym: CUtlSymbol); overload;
class operator Equal(const A: CUtlSymbol; B: PAnsiChar): Boolean;
end;
implementation
uses
System.Generics.Collections, Xander.ThisWrap;
procedure CopyConstruct(var Memory: Pointer; const Src); inline;
begin
PPointer(Memory)^ := @Src;
end;
{ CUtlMemory<T, I> }
function UtlMemory_CalcNewAllocationCount(AllocationCount, GrowSize, NewSize, BytesItem: Integer): Integer;
begin
if GrowSize <> 0 then
begin
AllocationCount := ((1 + ((NewSize - 1) div GrowSize)) * GrowSize);
end
else
begin
if AllocationCount = 0 then
begin
// Compute an allocation which is at least as big as a cache line...
AllocationCount := (31 + BytesItem) div BytesItem;
end;
while AllocationCount < NewSize do
begin
AllocationCount := AllocationCount * 2;
end;
end;
Result := AllocationCount;
end;
constructor CUtlMemory<T, I>.Create(GrowSize, InitSize: Integer);
begin
inherited;
end;
constructor CUtlMemory<T, I>.Create(Memory: PT);
begin
inherited;
end;
constructor CUtlMemory<T, I>.Create(const Memory: PT; NumElements: Integer);
begin
inherited;
end;
function CUtlMemory<T, I>.GetElement(Index: Integer): PT;
begin
Result := @FMemory[Index];
end;
procedure CUtlMemory<T, I>.Grow(Number: Integer);
var
AllocationCount: Integer;
AllocationRequested: Integer;
begin
if IsExternallyAllocated then
begin
// Can't grow a buffer whose memory was externally allocated
Assert(False);
Exit;
end;
// Make sure we have at least numallocated + num allocations.
// Use the grow rules specified for this memory (in m_nGrowSize)
AllocationRequested := FAllocationCount + Number;
AllocationCount := UtlMemory_CalcNewAllocationCount(FAllocationCount, FGrowSize, AllocationRequested, SizeOf(T));
// if m_nAllocationRequested wraps index type I, recalculate
if Integer(TI(FAllocationCount)) < AllocationRequested then
begin
if (Integer(TI(FAllocationCount)) = 0) and (Integer(TI(FAllocationCount - 1)) >= AllocationRequested) then
begin
Dec(AllocationRequested);
end
else
begin
if Integer(TI(AllocationRequested)) <> AllocationRequested then
begin
// we've been asked to grow memory to a size s.t. the index type can't address the requested amount of memory
Assert(False);
Exit;
end;
while Integer(TI(FAllocationCount)) < AllocationRequested do
begin
FAllocationCount := (FAllocationCount + AllocationRequested) div 2;
end;
end;
end;
if FMemory <> nil then
begin
FMemory := realloc(PT(FMemory), AllocationCount * SizeOf(T));
Assert(FMemory <> nil);
end
else
begin
FMemory := malloc(AllocationCount * SizeOf(T));
Assert(FMemory <> nil);
end;
end;
//destructor CUtlMemory<T, I>.Destroy;
//begin
//
// inherited Destroy;
//end;
procedure CUtlMemory<T, I>.Init(GrowSize, InitSize: Integer);
begin
end;
function CUtlMemory<T, I>.IsExternallyAllocated: Boolean;
begin
Result := FGrowSize < 0;
end;
function CUtlMemory<T, I>.TI(const Value: Integer): Integer;
var
Mask: Integer;
begin
Mask := High(Integer) - 1;
Result := Value and Mask; // TODO: High(I) - 1
end;
{ CUtlVector<T, A> }
function CUtlVector<T, A>.AddToTail(const Src: T): Integer;
begin
Result := InsertBefore(FSize, Src);
end;
procedure CUtlVector<T, A>.GrowVector(Number: Integer);
begin
if (FSize + Number > Base.NumAllocated) then
begin
Base.Grow(FSize + Number - Base.NumAllocated);
end;
Inc(FSize, Number);
// TODO: ResetDbgInfo()
end;
function CUtlVector<T, A>.InsertBefore(Elem: Integer; const Src: T): Integer;
var
P: Pointer;
Data: PT;
begin
P := Base.Element[Elem];
Data := malloc(SizeOf(Src));
Data^ := Src;
GrowVector;
ShiftElementsRight(Elem);
Base.Element[Elem]^ := Data^;
//CopyConstruct(P, Src);
Exit(Elem);
end;
function CUtlVector<T, A>.IsValidIndex(Idx: Integer): Boolean;
begin
Result := (Idx >= 0) and (Idx < FSize);
end;
procedure CUtlVector<T, A>.ShiftElementsRight(Elem, Number: Integer);
var
NumToMove: Integer;
begin
Assert(IsValidIndex(Elem));
Assert(FSize <> 0);
Assert(Number <> 0);
NumToMove := FSize - Elem - Number;
if (NumToMove > 0) and (Number > 0) then
Move(Base.Element[Elem + Number]^, Base.Element[Elem]^, NumToMove * SizeOf(T));
end;
var
MemoryAllocatorStack: TList<TUtlMemoryAlloc>;
procedure UtlMemory_PushMemAlloc(Func: TUtlMemoryAlloc);
begin
MemoryAllocatorStack.Add(Func);
end;
procedure UtlMemory_PopMemAlloc;
begin
MemoryAllocatorStack.Delete(MemoryAllocatorStack.Count - 1);
end;
{ CUtlSymbol }
constructor CUtlSymbol.Create(ID: TUtlSymId);
begin
FID := ID;
end;
constructor CUtlSymbol.Create(Str: PAnsiChar);
begin
end;
constructor CUtlSymbol.Create(const Sym: CUtlSymbol);
begin
end;
class operator CUtlSymbol.Equal(const A: CUtlSymbol; B: PAnsiChar): Boolean;
begin
if A.FID = UTL_INVAL_SYMBOL then
Exit(False);
// ...
Exit(True);
end;
initialization
MemoryAllocatorStack := TList<TUtlMemoryAlloc>.Create;
end.