-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstdclass.h
683 lines (597 loc) · 10.6 KB
/
stdclass.h
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#pragma once
#include "types.h"
#include <stdlib.h>
#include "string.h"
#include <vector>
#define PAGE_SIZE 4096
#define PAGE_MASK (PAGE_SIZE-1)
#include "plugins/plugin_header.h"
#include "os_base.h"
extern u32 Array_T_id_count;
wchar* GetNullDCSoruceFileName(const char* full);
void GetPathFromFileName(wchar* full);
void GetFileNameFromPath(wchar* path,wchar* outp);
u32 fastrand();
//comonly used classes across the project
//Simple Array class for helping me out ;P
template<class T>
class Array
{
public:
T* data;
u32 Size;
u32 id;
Array(T* Source,u32 ellements)
{
//initialise array
Array_T_id_count+=1;
id=Array_T_id_count;
data=Source;
Size=ellements;
}
Array(u32 ellements)
{
//initialise array
Array_T_id_count+=1;
id=Array_T_id_count;
data=0;
Resize(ellements,false);
Size=ellements;
}
Array(u32 ellements,bool zero)
{
//initialise array
Array_T_id_count+=1;
id=Array_T_id_count;
data=0;
Resize(ellements,zero);
Size=ellements;
}
Array()
{
//initialise array
Array_T_id_count+=1;
id=Array_T_id_count;
data=0;
Size=0;
}
~Array()
{
if (data)
{
#ifdef MEM_ALLOC_TRACE
printf("WARNING : DESTRUCTOR WITH NON FREED ARRAY [arrayid:%d]\n",id);
#endif
Free();
}
}
void SetPtr(T* Source,u32 ellements)
{
//initialise array
Free();
data=Source;
Size=ellements;
}
T* Resize(u32 size,bool bZero)
{
if (size==0)
{
if (data)
{
#ifdef MEM_ALLOC_TRACE
printf("Freeing data -> resize to zero[Array:%d]\n",id);
#endif
Free();
}
}
if (!data)
data=(T*)malloc(size*sizeof(T));
else
data=(T*)realloc(data,size*sizeof(T));
//TODO : Optimise this
//if we allocated more , Zero it out
if (bZero)
{
if (size>Size)
{
for (u32 i=Size;i<size;i++)
{
u8*p =(u8*)&data[i];
for (u32 j=0;j<sizeof(T);j++)
{
p[j]=0;
}
}
}
}
Size=size;
return data;
}
void Zero()
{
memset(data,0,sizeof(T)*Size);
}
void Free()
{
if (Size!=0)
{
if (data)
free(data);
else
printf("Data allready freed [Array:%d]\n",id);
data=0;
}
else
{
if (data)
printf("Free : Size=0 , data ptr !=null [Array:%d]\n",id);
}
}
INLINE T& operator [](const u32 i)
{
#ifdef MEM_BOUND_CHECK
if (i>=Size)
{
printf("Error: Array %d , index out of range (%d>%d)\n",id,i,Size-1);
MEM_DO_BREAK;
}
#endif
return data[i];
}
INLINE T& operator [](const s32 i)
{
#ifdef MEM_BOUND_CHECK
if (!(i>=0 && i<(s32)Size))
{
printf("Error: Array %d , index out of range (%d > %d)\n",id,i,Size-1);
MEM_DO_BREAK;
}
#endif
return data[i];
}
};
//Simple and fast Stack
template<class T>
class Stack
{
public :
Array<T> items;
s32 top;
INLINE s32 FreeSpace()
{
return items.Size-top;
}
INLINE T& operator [](const u32 i)
{
#ifdef MEM_BOUND_CHECK
if (i>=top)
{
printf("Stack Error : Reading %d item , this item is not used \n",i);
MEM_DO_BREAK;
}
#endif
return items[i];
}
INLINE T& operator [](const s32 i)
{
#ifdef MEM_BOUND_CHECK
if (!(i>0 && i<top))
{
printf("Stack Error : Reading %d item , this item is not used \n",i);
MEM_DO_BREAK;
}
#endif
return items[i];
}
Stack()
{
top=0;
items.Resize(10,false);
}
Stack(s32 count)
{
top=0;
items.Resize(count,false);
}
~Stack()
{
items.Free();
}
/*void push(T &data)
{
if (top>=(s32)items.Size)
{
items.Resize(items.Size + items.Size*1/4+2,false);
}
items[top++]=data;
}*/
void push(T data)
{
if (top>=(s32)items.Size)
{
items.Resize(items.Size + items.Size*1/4+2,false);
}
items[top++]=data;
}
void push(T* data)
{
if (top>=(s32)items.Size)
{
items.Resize(items.Size + items.Size*1/4+2,false);
}
items[top++]=data;
}
void pop(T* data)
{
if (top<=0)
{
}
else
{
*data=items[--top];
if (top<(items.Size/5))
{
items.Resize(top*3/2);
}
}
}
T pop()
{
if (top<0)
{
//TODO : fix it ... it will work olny for pointers
return 0;
}
else
{
if ((u32)top<(items.Size/15))
{
items.Resize(top*3/2,false);
}
return items[--top];
}
}
};
//Fast and simple list
template<class T>
class GrowingList
{
private:
struct tripl{T item;bool used;};
public:
Array<tripl> items;
u32 itemcount;
INLINE T& operator [](const u32 i)
{
#ifdef MEM_BOUND_CHECK
if (items[i].used==false)
{
printf("GrowingList Error : Reading %d item , this item is not used \n",i);
MEM_DO_BREAK;
}
#endif
return items[i].item;
}
INLINE T& operator [](const s32 i)
{
#ifdef MEM_BOUND_CHECK
if (items[i].used==false)
{
printf("GrowingList Error : Reading %d item , this item is not used \n",i);
MEM_DO_BREAK;
}
#endif
return items[i].item;
}
GrowingList()
{
itemcount=0;
items.Resize(10,true);
}
GrowingList(u32 count)
{
itemcount=0;
items.Resize(count,true);
}
~GrowingList()
{
items.Free();
}
u32 Add(T& item)
{
if (itemcount==items.Size)
{
items.Resize(items.Size+items.Size*1/4+2,true);
}
itemcount++;
//needs to be optimised
//TODO: Add a psedo random algo
for(u32 i=0;i<items.Size;i++)
{
if (items[i].used==0)
{
items[i].used=1;
items[i].item=item;
return i;
}
}
return 0xFFFFFFFF;
}
u32 Add(T* item)
{
if (itemcount==items.Size)
{
items.Resize(items.Size+items.Size*1/4+2,true);
}
itemcount++;
for(u32 i=0;i<items.Size;i++)
{
if (items[i].used==0)
{
items[i].used=1;
items[i].item=*item;
return i;
}
}
return 0xFFFFFFFF;
}
bool Remove(u32 item)
{
bool rv=items[item].used;
if (rv)
items[item].used=0;
return rv;
}
};
//faster and simple list (olny for pointer types)
template<class T>
class GrowingListPtr
{
public:
Array<T*> items;
u32 itemcount;
INLINE T*& operator [](const u32 i)
{
#ifdef MEM_BOUND_CHECK
if (items[i]==0)
{
printf("GrowingListPtr Error : Reading %d item , this item is not used \n",i);
MEM_DO_BREAK;
}
#endif
return items[i];
}
INLINE T*& operator [](const s32 i)
{
#ifdef MEM_BOUND_CHECK
if (items[i]==0)
{
printf("GrowingListPtr Error : Reading %d item , this item is not used \n",i);
MEM_DO_BREAK;
}
#endif
return items[i];
}
GrowingListPtr()
{
itemcount=0;
items.Resize(10,true);
}
GrowingListPtr(u32 count)
{
itemcount=0;
items.Resize(count,true);
}
~GrowingListPtr()
{
items.Free();
}
u32 Add(T* item)
{
#ifdef MEM_BOUND_CHECK
if (item==0)
{
printf("GrowningListPtr Error : Add(T* item) , item ==0\n");
MEM_DO_BREAK;
}
#else
#ifdef MEM_ALLOC_CHECK
if (item==0)
{
printf("GrowningListPtr Error : Add(T* item) , item ==0\n");
MEM_DO_BREAK;
}
#else
#ifdef TRACE
if (item==0)
{
printf("GrowningListPtr Error : Add(T* item) , item ==0\n");
TRACE_DO_BREAK;
}
#endif
#endif
#endif
if (itemcount==items.Size)
{
items.Resize(items.Size+items.Size*1/4+2,true);
}
itemcount++;
//TODO: Add a psedo random algo
for(u32 i=0;i<items.Size;i++)
{
if (items[i]==0)
{
items[i]=item;
return i;
}
}
return 0xFFFFFFFF;
}
bool Remove(u32 item)
{
bool rv=items[item]!=0;
if (rv)
items[item]=0;
return rv;
}
};
//Fifo list xD
template<class T>
struct Fifo_List
{
Array<T> Item;
int ReadIndex;
int WriteIndex;
INLINE T GetItem()
{
if (!IsEmpty())
return Item[ReadIndex++];
else
WriteIndex=ReadIndex=0;
return 0;
}
INLINE void GetItems(T* items,int count)
{
for (int i=0;i<count;i++)
{
items[i]=GetItem();
}
}
INLINE void PutItem(T item)
{
if (!IsEmpty())
Item[WriteIndex++]=item;
else
{
if (Item.Size<=WriteIndex)
Item.Resize(WriteIndex*3/2,false);
WriteIndex=ReadIndex=0;
Item[WriteIndex++]=item;
}
}
INLINE void PutItems(T* items,int count)
{
for (int i=0;i<count;i++)
{
PutItem(items[i]);
}
}
INLINE bool IsEmpty()
{
return ReadIndex==WriteIndex;
}
};
template<class T>
class List : public std::vector<T>
{
public:
u32 itemcount;
List()
{
itemcount=0;
}
INLINE T* Add(T& item)
{
push_back(item);
itemcount++;
return &(*this)[this->size()-1];
}
};
//Are these really needed ?
typedef u32 ThreadEntryFP(void* param);
class cThread
{
private:
ThreadEntryFP* Entry;
void* param;
void* hThread;
public :
cThread(ThreadEntryFP* function,void* param);
~cThread();
//Simple thread functions
void Start();
void Suspend();
void WaitToEnd(u32 msec);
};
//Wait Events
class cResetEvent
{
private:
void* hEvent;
public :
cResetEvent(bool State,bool Auto);
~cResetEvent();
void Set(); //Set state to signaled
void Reset(); //Set state to non signaled
void Wait(u32 msec);//Wait for signal , then reset[if auto]
void Wait(); //Wait for signal , then reset[if auto]
};
//Dynamic library handler
class cDllHandler
{
private :
void* lib;
public:
cDllHandler();
~cDllHandler();
bool Load(wchar* dll);
bool IsLoaded();
void Unload();
void* GetProcAddress(char* name);
};
//Paths
void GetApplicationPath(wchar* path,u32 size);
wchar* GetEmuPath(const wchar* subpath);
class VArray
{
public:
u8* data;
u32 size;
void Init(u32 sz);
void Term();
void LockRegion(u32 offset,u32 size);
void UnLockRegion(u32 offset,u32 size);
void Zero()
{
UnLockRegion(0,size);
memset(data,0,size);
}
INLINE u8& operator [](const u32 i)
{
#ifdef MEM_BOUND_CHECK
if (i>=size)
{
printf("Error: VArray , index out of range (%d>%d)\n",i,size-1);
MEM_DO_BREAK;
}
#endif
return data[i];
}
};
class VArray2
{
public:
u8* data;
u32 size;
//void Init(void* data,u32 sz);
//void Term();
void LockRegion(u32 offset,u32 size);
void UnLockRegion(u32 offset,u32 size);
void Zero()
{
UnLockRegion(0,size);
memset(data,0,size);
}
INLINE u8& operator [](const u32 i)
{
#ifdef MEM_BOUND_CHECK
if (i>=size)
{
printf("Error: VArray2 , index out of range (%d>%d)\n",i,size-1);
MEM_DO_BREAK;
}
#endif
return data[i];
}
};
int msgboxf(const wchar* text,unsigned int type,...);