|
| 1 | +(*=========== (C) Copyright 2019, Alexander B. All rights reserved. ===========*) |
| 2 | +(* *) |
| 3 | +(* Module: *) |
| 4 | +(* GoldSrc.Collections *) |
| 5 | +(* *) |
| 6 | +(* License: *) |
| 7 | +(* You may freely use this code provided you retain this copyright message. *) |
| 8 | +(* *) |
| 9 | +(* Description: *) |
| 10 | +(*=============================================================================*) |
| 11 | + |
| 12 | +unit GoldSrc.Collections; |
| 13 | + |
| 14 | +{$POINTERMATH ON} |
| 15 | + |
| 16 | +interface |
| 17 | + |
| 18 | +procedure CopyConstruct(var Memory: Pointer; const Src); inline; |
| 19 | +function UtlMemory_CalcNewAllocationCount(AllocationCount, GrowSize, NewSize, BytesItem: Integer): Integer; |
| 20 | + |
| 21 | +type |
| 22 | + TUtlMemoryAlloc = function(Size: Integer): Pointer; |
| 23 | + |
| 24 | +procedure UtlMemory_PushMemAlloc(Func: TUtlMemoryAlloc); |
| 25 | +procedure UtlMemory_PopMemAlloc; |
| 26 | + |
| 27 | +function malloc(Size: Integer): Pointer cdecl; external 'msvcrt'; |
| 28 | +function realloc(P: Pointer; Size: Integer): Pointer cdecl; external 'msvcrt'; |
| 29 | +procedure free(P: Pointer) cdecl; external 'msvcrt'; |
| 30 | + |
| 31 | +type |
| 32 | + CUtlMemory<T, I { = Integer}> = record |
| 33 | + public type PT = ^T; |
| 34 | + private |
| 35 | + function TI(const Value: Integer): Integer; inline; |
| 36 | + function GetElement(Index: Integer): PT; |
| 37 | + public |
| 38 | + FMemory: PT; |
| 39 | + FAllocationCount: Integer; |
| 40 | + FGrowSize: Integer; |
| 41 | + |
| 42 | + constructor Create(GrowSize: Integer; InitSize: Integer); overload; |
| 43 | + constructor Create(Memory: PT); overload; |
| 44 | + constructor Create(const Memory: PT; NumElements: Integer); overload; |
| 45 | + |
| 46 | + procedure Init(GrowSize: Integer = 0; InitSize: Integer = 0); |
| 47 | + procedure Grow(Number: Integer = 1); |
| 48 | + |
| 49 | + function IsExternallyAllocated: Boolean; |
| 50 | + |
| 51 | + property Element[Index: Integer]: PT read GetElement; default; |
| 52 | + |
| 53 | + property NumAllocated: Integer read FAllocationCount; |
| 54 | + end; |
| 55 | + |
| 56 | + CUtlVector<T, A { = Integer}> = record |
| 57 | + public type PT = ^T; |
| 58 | + public |
| 59 | + Base: CUtlMemory<T, Integer>; |
| 60 | + |
| 61 | + FSize: Integer; |
| 62 | + FElements: PT; |
| 63 | + |
| 64 | + procedure GrowVector(Number: Integer = 1); |
| 65 | + |
| 66 | + function IsValidIndex(Idx: Integer): Boolean; inline; |
| 67 | + |
| 68 | + function InsertBefore(Elem: Integer; const Src: T): Integer; |
| 69 | + procedure ShiftElementsRight(Elem: Integer; Number: Integer = 1); |
| 70 | + function AddToTail(const Src: T): Integer; |
| 71 | + end; |
| 72 | + |
| 73 | +type |
| 74 | + UtlSymId_t = Word; |
| 75 | + TUtlSymId = UtlSymId_t; |
| 76 | + |
| 77 | +const |
| 78 | + MAX_STRING_POOL_SIZE = 256; |
| 79 | + UTL_INVAL_SYMBOL: TUtlSymId = TUtlSymId(not 0); |
| 80 | + INVALID_STRING_INDEX: Cardinal = Cardinal(-1); |
| 81 | + |
| 82 | +type |
| 83 | + PCUtlSymbolTable = ^CUtlSymbolTable; |
| 84 | + CUtlSymbolTable = record |
| 85 | + |
| 86 | + end; |
| 87 | + |
| 88 | +type |
| 89 | + LessCtx_t = record |
| 90 | + UserString: PAnsiChar; |
| 91 | + Table: PCUtlSymbolTable; |
| 92 | + end; |
| 93 | + TLessCtx = LessCtx_t; |
| 94 | + PLessCtx = ^TLessCtx; |
| 95 | + |
| 96 | +type |
| 97 | + CUtlSymbol = record |
| 98 | + FID: TUtlSymId; |
| 99 | + |
| 100 | + constructor Create(ID: TUtlSymId); overload; |
| 101 | + constructor Create(Str: PAnsiChar); overload; |
| 102 | + constructor Create(const Sym: CUtlSymbol); overload; |
| 103 | + |
| 104 | + class operator Equal(const A: CUtlSymbol; B: PAnsiChar): Boolean; |
| 105 | + end; |
| 106 | + |
| 107 | +implementation |
| 108 | + |
| 109 | +uses |
| 110 | + System.Generics.Collections, Xander.ThisWrap; |
| 111 | + |
| 112 | +procedure CopyConstruct(var Memory: Pointer; const Src); inline; |
| 113 | +begin |
| 114 | + PPointer(Memory)^ := @Src; |
| 115 | +end; |
| 116 | + |
| 117 | +{ CUtlMemory<T, I> } |
| 118 | + |
| 119 | +function UtlMemory_CalcNewAllocationCount(AllocationCount, GrowSize, NewSize, BytesItem: Integer): Integer; |
| 120 | +begin |
| 121 | + if GrowSize <> 0 then |
| 122 | + begin |
| 123 | + AllocationCount := ((1 + ((NewSize - 1) div GrowSize)) * GrowSize); |
| 124 | + end |
| 125 | + else |
| 126 | + begin |
| 127 | + if AllocationCount = 0 then |
| 128 | + begin |
| 129 | + // Compute an allocation which is at least as big as a cache line... |
| 130 | + AllocationCount := (31 + BytesItem) div BytesItem; |
| 131 | + end; |
| 132 | + |
| 133 | + while AllocationCount < NewSize do |
| 134 | + begin |
| 135 | + AllocationCount := AllocationCount * 2; |
| 136 | + end; |
| 137 | + end; |
| 138 | + |
| 139 | + Result := AllocationCount; |
| 140 | +end; |
| 141 | + |
| 142 | +constructor CUtlMemory<T, I>.Create(GrowSize, InitSize: Integer); |
| 143 | +begin |
| 144 | + inherited; |
| 145 | +end; |
| 146 | + |
| 147 | +constructor CUtlMemory<T, I>.Create(Memory: PT); |
| 148 | +begin |
| 149 | + inherited; |
| 150 | +end; |
| 151 | + |
| 152 | +constructor CUtlMemory<T, I>.Create(const Memory: PT; NumElements: Integer); |
| 153 | +begin |
| 154 | + inherited; |
| 155 | +end; |
| 156 | + |
| 157 | +function CUtlMemory<T, I>.GetElement(Index: Integer): PT; |
| 158 | +begin |
| 159 | + Result := @FMemory[Index]; |
| 160 | +end; |
| 161 | + |
| 162 | +procedure CUtlMemory<T, I>.Grow(Number: Integer); |
| 163 | +var |
| 164 | + AllocationCount: Integer; |
| 165 | + AllocationRequested: Integer; |
| 166 | +begin |
| 167 | + if IsExternallyAllocated then |
| 168 | + begin |
| 169 | + // Can't grow a buffer whose memory was externally allocated |
| 170 | + Assert(False); |
| 171 | + Exit; |
| 172 | + end; |
| 173 | + |
| 174 | + // Make sure we have at least numallocated + num allocations. |
| 175 | + // Use the grow rules specified for this memory (in m_nGrowSize) |
| 176 | + AllocationRequested := FAllocationCount + Number; |
| 177 | + |
| 178 | + AllocationCount := UtlMemory_CalcNewAllocationCount(FAllocationCount, FGrowSize, AllocationRequested, SizeOf(T)); |
| 179 | + |
| 180 | + // if m_nAllocationRequested wraps index type I, recalculate |
| 181 | + if Integer(TI(FAllocationCount)) < AllocationRequested then |
| 182 | + begin |
| 183 | + if (Integer(TI(FAllocationCount)) = 0) and (Integer(TI(FAllocationCount - 1)) >= AllocationRequested) then |
| 184 | + begin |
| 185 | + Dec(AllocationRequested); |
| 186 | + end |
| 187 | + else |
| 188 | + begin |
| 189 | + if Integer(TI(AllocationRequested)) <> AllocationRequested then |
| 190 | + begin |
| 191 | + // we've been asked to grow memory to a size s.t. the index type can't address the requested amount of memory |
| 192 | + Assert(False); |
| 193 | + Exit; |
| 194 | + end; |
| 195 | + |
| 196 | + while Integer(TI(FAllocationCount)) < AllocationRequested do |
| 197 | + begin |
| 198 | + FAllocationCount := (FAllocationCount + AllocationRequested) div 2; |
| 199 | + end; |
| 200 | + end; |
| 201 | + end; |
| 202 | + |
| 203 | + if FMemory <> nil then |
| 204 | + begin |
| 205 | + FMemory := realloc(PT(FMemory), AllocationCount * SizeOf(T)); |
| 206 | + Assert(FMemory <> nil); |
| 207 | + end |
| 208 | + else |
| 209 | + begin |
| 210 | + FMemory := malloc(AllocationCount * SizeOf(T)); |
| 211 | + Assert(FMemory <> nil); |
| 212 | + end; |
| 213 | +end; |
| 214 | + |
| 215 | +//destructor CUtlMemory<T, I>.Destroy; |
| 216 | +//begin |
| 217 | +// |
| 218 | +// inherited Destroy; |
| 219 | +//end; |
| 220 | + |
| 221 | +procedure CUtlMemory<T, I>.Init(GrowSize, InitSize: Integer); |
| 222 | +begin |
| 223 | + |
| 224 | +end; |
| 225 | + |
| 226 | +function CUtlMemory<T, I>.IsExternallyAllocated: Boolean; |
| 227 | +begin |
| 228 | + Result := FGrowSize < 0; |
| 229 | +end; |
| 230 | + |
| 231 | +function CUtlMemory<T, I>.TI(const Value: Integer): Integer; |
| 232 | +var |
| 233 | + Mask: Integer; |
| 234 | +begin |
| 235 | + Mask := High(Integer) - 1; |
| 236 | + Result := Value and Mask; // TODO: High(I) - 1 |
| 237 | +end; |
| 238 | + |
| 239 | +{ CUtlVector<T, A> } |
| 240 | + |
| 241 | +function CUtlVector<T, A>.AddToTail(const Src: T): Integer; |
| 242 | +begin |
| 243 | + Result := InsertBefore(FSize, Src); |
| 244 | +end; |
| 245 | + |
| 246 | +procedure CUtlVector<T, A>.GrowVector(Number: Integer); |
| 247 | +begin |
| 248 | + if (FSize + Number > Base.NumAllocated) then |
| 249 | + begin |
| 250 | + Base.Grow(FSize + Number - Base.NumAllocated); |
| 251 | + end; |
| 252 | + |
| 253 | + Inc(FSize, Number); |
| 254 | + // TODO: ResetDbgInfo() |
| 255 | +end; |
| 256 | + |
| 257 | +function CUtlVector<T, A>.InsertBefore(Elem: Integer; const Src: T): Integer; |
| 258 | +var |
| 259 | + P: Pointer; |
| 260 | + Data: PT; |
| 261 | +begin |
| 262 | + P := Base.Element[Elem]; |
| 263 | + |
| 264 | + Data := malloc(SizeOf(Src)); |
| 265 | + Data^ := Src; |
| 266 | + |
| 267 | + GrowVector; |
| 268 | + ShiftElementsRight(Elem); |
| 269 | + Base.Element[Elem]^ := Data^; |
| 270 | + //CopyConstruct(P, Src); |
| 271 | + |
| 272 | + Exit(Elem); |
| 273 | +end; |
| 274 | + |
| 275 | +function CUtlVector<T, A>.IsValidIndex(Idx: Integer): Boolean; |
| 276 | +begin |
| 277 | + Result := (Idx >= 0) and (Idx < FSize); |
| 278 | +end; |
| 279 | + |
| 280 | +procedure CUtlVector<T, A>.ShiftElementsRight(Elem, Number: Integer); |
| 281 | +var |
| 282 | + NumToMove: Integer; |
| 283 | +begin |
| 284 | + Assert(IsValidIndex(Elem)); |
| 285 | + Assert(FSize <> 0); |
| 286 | + Assert(Number <> 0); |
| 287 | + |
| 288 | + NumToMove := FSize - Elem - Number; |
| 289 | + if (NumToMove > 0) and (Number > 0) then |
| 290 | + Move(Base.Element[Elem + Number]^, Base.Element[Elem]^, NumToMove * SizeOf(T)); |
| 291 | +end; |
| 292 | + |
| 293 | +var |
| 294 | + MemoryAllocatorStack: TList<TUtlMemoryAlloc>; |
| 295 | + |
| 296 | +procedure UtlMemory_PushMemAlloc(Func: TUtlMemoryAlloc); |
| 297 | +begin |
| 298 | + MemoryAllocatorStack.Add(Func); |
| 299 | +end; |
| 300 | + |
| 301 | +procedure UtlMemory_PopMemAlloc; |
| 302 | +begin |
| 303 | + MemoryAllocatorStack.Delete(MemoryAllocatorStack.Count - 1); |
| 304 | +end; |
| 305 | + |
| 306 | +{ CUtlSymbol } |
| 307 | + |
| 308 | +constructor CUtlSymbol.Create(ID: TUtlSymId); |
| 309 | +begin |
| 310 | + FID := ID; |
| 311 | +end; |
| 312 | + |
| 313 | +constructor CUtlSymbol.Create(Str: PAnsiChar); |
| 314 | +begin |
| 315 | + |
| 316 | +end; |
| 317 | + |
| 318 | +constructor CUtlSymbol.Create(const Sym: CUtlSymbol); |
| 319 | +begin |
| 320 | + |
| 321 | +end; |
| 322 | + |
| 323 | +class operator CUtlSymbol.Equal(const A: CUtlSymbol; B: PAnsiChar): Boolean; |
| 324 | +begin |
| 325 | + if A.FID = UTL_INVAL_SYMBOL then |
| 326 | + Exit(False); |
| 327 | + |
| 328 | + // ... |
| 329 | + |
| 330 | + Exit(True); |
| 331 | +end; |
| 332 | + |
| 333 | +initialization |
| 334 | + MemoryAllocatorStack := TList<TUtlMemoryAlloc>.Create; |
| 335 | + |
| 336 | +end. |
0 commit comments