forked from NTDLS/NSWFL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSWFL_MemoryPool.Cpp
914 lines (750 loc) · 26.5 KB
/
NSWFL_MemoryPool.Cpp
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © NetworkDLS 2023, All rights reserved
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _NSWFL_MEMORYPOOL_CPP_
#define _NSWFL_MEMORYPOOL_CPP_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "NSWFL.H"
#ifdef _USE_GLOBAL_MEMPOOL
extern NSWFL::Memory::MemoryPool *pMem; //pMem must be defined and initalized elsewhere.
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace NSWFL {
namespace Memory {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
#include <ConIO.H>
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
#ifdef UNICODE
#define MemoryPoolAssert(sExpression, sMsg) \
if(sExpression)\
{ \
this->MemoryPoolAssertEx(#sExpression, sMsg, __FUNCTION__, __FILE__, __DATE__, __TIME__, __LINE__); \
}
#else
#define MemoryPoolAssert(sExpression, sMsg) \
if(sExpression)\
{ \
this->MemoryPoolAssertEx(#sExpression, sMsg, __FUNCTION__, __FILE__, __DATE__, __TIME__, __LINE__); \
}
#endif
#else
#define MemoryPoolAssert(sExpression, sMsg)
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MemoryPool::~MemoryPool(void)
{
this->Destroy();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MemoryPool::MemoryPool(void)
{
this->Initialize();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
MemoryPool::MemoryPool(size_t iAllocateIncrement)
{
this->Initialize(iAllocateIncrement);
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
size_t MemoryPool::ReservedBytes(void)
{
this->Lock();
size_t iBytes = this->Items.iReservedBytes;
this->Unlock();
return iBytes;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MemoryPool::IsDebug(void)
{
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
return true;
#else
return false;
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
size_t MemoryPool::GetMemFreeCount(void)
{
return(this->Items.iAllocated - this->Items.iUsedCount);
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
size_t MemoryPool::GetMemAllocationCount(void)
{
return this->Items.iUsedCount;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
size_t MemoryPool::GetSlotAllocationCount(void)
{
return this->Items.iAllocated;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
bool MemoryPool::FreeAll(void)
{
bool bResult = true;
//Free all the memory stored in the [Array] structure.
for (register size_t iSlot = 0; iSlot < this->Items.iAllocated; iSlot++)
{
if (this->Items.Array[iSlot].iAddress)
{
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Memory leak detected while destroying a managed memory pool item at [0x%zX]."
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
"\r\n\r\nAllocated in \"%s\""
"\r\non line %zd for %zd bytes."
#endif
, this->Items.Array[iSlot].iAddress
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
, this->Items.Array[iSlot].sCodeFile,
this->Items.Array[iSlot].iLineOfCode,
this->Items.Array[iSlot].iSize
#endif
);
MemoryPoolAssert("FreeAll", this->sDebugText);
if (!this->Free((void *)this->Items.Array[iSlot].iAddress))
{
bResult = false;
}
}
}
return bResult;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
size_t MemoryPool::GetSlotByAddress(const size_t iAddress)
{
for (register size_t iSlot = 0; iSlot < this->Items.iAllocated; iSlot++)
{
if (this->Items.Array[iSlot].iAddress == iAddress)
{
return iSlot;
}
}
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Application attempted to access an unmanaged memory address at [0x%zX].", iAddress);
MemoryPoolAssert("GetSlotByAddress", this->sDebugText);
return -1; //Did not find the address specified.
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
size_t MemoryPool::FindFreeAllocSlot(void)
{
for (register size_t iSlot = 0; iSlot < this->Items.iAllocated; iSlot++)
{
if (this->Items.Array[iSlot].iAddress == 0)
{
return iSlot;
}
}
//Increase the allocation increment by 25%
this->Items.iAllocIncrement += (size_t)((float)this->Items.iAllocIncrement * 0.25f);
size_t iMemory = (sizeof(MEMITEM) * this->Items.iAllocated)
+ (sizeof(MEMITEM) * this->Items.iAllocIncrement);
if ((this->Items.Array = (MEMITEM *)realloc(this->Items.Array, iMemory)) == NULL)
{
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Failed to reallocate the managed Array structure for [%zd] bytes.", iMemory);
MemoryPoolAssert("FindFreeAllocSlot", this->sDebugText);
return -1;
}
size_t iPreviousCeleing = this->Items.iAllocated;
this->Items.iAllocated += this->Items.iAllocIncrement;
//Clean the new slots.
for (register size_t iSlot = iPreviousCeleing; iSlot < this->Items.iAllocated; iSlot++)
{
memset(&this->Items.Array[iSlot], 0, sizeof(MEMITEM));
}
return this->FindFreeAllocSlot();
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
bool MemoryPool::Initialize(size_t iAllocateIncrement)
{
InitializeCriticalSection(&this->csMemLock);
memset(&this->Items, 0, sizeof(Items));
this->Items.iAllocIncrement = iAllocateIncrement;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
this->hStdOut = NULL;
#endif
if ((this->Items.Array = (MEMITEM *)
calloc(sizeof(MEMITEM), this->Items.iAllocIncrement)) == NULL)
{
MemoryPoolAssert("Initialize", "Memory pool Failed to initialize.");
DeleteCriticalSection(&this->csMemLock);
return false;
}
this->Items.iAllocated = this->Items.iAllocIncrement;
for (register size_t iSlot = 0; iSlot < this->Items.iAllocated; iSlot++)
{
this->Items.Array[iSlot].iAddress = 0;
#if _MEMPOOL_DEBUG_LEVEL > _MEMPOOL_DEBUG_ADVANCED
this->Items.Array[iSlot].iSize = 0;
this->Items.Array[iSlot].iLineOfCode = -1;
this->Items.Array[iSlot].sCodeFile = NULL;
#endif
}
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
return this->OpenConsole();
#else
return true;
#endif
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MemoryPool::Initialize(void)
{
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
return this->Initialize(100);
#else
#if _MEMPOOL_SEQUENTIAL_WHEN_OPTIMIZED != 0
InitializeCriticalSectionAndSpinCount(&this->csMemLock, 4000);
#endif
return true;
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
bool MemoryPool::Destroy(void)
{
bool bResult = false;
this->Lock();
bResult = this->FreeAll();
free(this->Items.Array);
this->Unlock();
DeleteCriticalSection(&this->csMemLock);
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
bResult = bResult && this->CloseConsole();
#endif
return bResult;
}
#else
bool MemoryPool::Destroy(void)
{
#if _MEMPOOL_SEQUENTIAL_WHEN_OPTIMIZED != 0
DeleteCriticalSection(&this->csMemLock);
#elif _MEMPOOL_DEBUG_LEVEL > _MEMPOOL_DEBUG_DISABLED
DeleteCriticalSection(&this->csMemLock);
#endif
return true;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MemoryPool::Lock(void)
{
EnterCriticalSection(&this->csMemLock);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MemoryPool::Unlock(void)
{
LeaveCriticalSection(&this->csMemLock);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
bool MemoryPool::Free(void *lpMemory)
{
this->Lock();
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
sprintf_s(this->sDebugText, sizeof(this->sDebugText), "Freeing address 0x%X\n", lpMemory);
this->DbgWrite(this->sDebugText);
#endif
size_t iSlot = 0;
if (lpMemory == NULL)
{
MemoryPoolAssert("Free", "Application attempted to free a NULL pointer.");
this->Unlock();
return false;
}
if ((iSlot = this->GetSlotByAddress((size_t)lpMemory)) != (size_t)-1)
{
//Free the memory that the user allocated, also mark the [Array] slot as free.
free(lpMemory);
lpMemory = NULL;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
free(this->Items.Array[iSlot].sCodeFile);
this->Items.iReservedBytes -= this->Items.Array[iSlot].iSize;
this->Items.Array[iSlot].sCodeFile = NULL;
this->Items.Array[iSlot].iLineOfCode = 0;
this->Items.Array[iSlot].iSize = 0;
#endif
this->Items.Array[iSlot].iAddress = 0;
this->Items.iUsedCount--;
}
else {
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Address [0x%zX] not found in the managed memory pool.", (size_t)lpMemory);
MemoryPoolAssert("Free", this->sDebugText);
this->Unlock();
return false;
}
this->Unlock();
return true;
}
#else
bool MemoryPool::Free(void *lpMemory)
{
#if _MEMPOOL_SEQUENTIAL_WHEN_OPTIMIZED != 0
this->Lock();
free(lpMemory);
this->Unlock();
#else
free(lpMemory);
#endif
return true;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
void *MemoryPool::_dbg_ReAllocate(void *lpMemory, const size_t iSize, const size_t iCount,
const char *sCodeFile, const size_t iLineOfCode)
#else
void *MemoryPool::ReAllocate(void *lpMemory, const size_t iSize, const size_t iCount)
#endif
{
if (lpMemory)
{
this->Lock();
size_t iSlot = 0;
char *sMemory = NULL;
if ((iSlot = this->GetSlotByAddress((size_t)lpMemory)) != (size_t)-1)
{
if ((sMemory = (char *)realloc(lpMemory, iSize * iCount)) == NULL)
{
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Failed to reallocate managed user memory for [%zd] bytes.", iSize * iCount);
MemoryPoolAssert("ReAllocate", this->sDebugText);
this->Unlock();
return NULL;
}
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Realloc %d bytes. (0x%X -> 0x%X) @ %s:%d\n",
iSize * iCount, lpMemory, sMemory,
this->Items.Array[iSlot].sCodeFile,
this->Items.Array[iSlot].iLineOfCode);
this->DbgWrite(this->sDebugText);
#endif
this->Items.Array[iSlot].iAddress = (size_t)sMemory;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
this->Items.iReservedBytes -= this->Items.Array[iSlot].iSize;
this->Items.Array[iSlot].iSize = iSize * iCount;
this->Items.iReservedBytes += this->Items.Array[iSlot].iSize;
#endif
}
else {
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Address [0x%zX] not found in the managed memory pool.", (size_t)lpMemory);
MemoryPoolAssert("ReAllocate", this->sDebugText);
this->Unlock();
return NULL;
}
this->Unlock();
return sMemory;
}
else {
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
return this->_dbg_Allocate(iSize, iCount, sCodeFile, iLineOfCode);
#else
return this->Allocate(iSize, iCount);
#endif
}
}
#else
void *MemoryPool::ReAllocate(void *lpMemory, const size_t iSize, const size_t iCount)
{
#if _MEMPOOL_SEQUENTIAL_WHEN_OPTIMIZED != 0
this->Lock();
char *sMemory = (char *)realloc(lpMemory, iSize * iCount);
this->Unlock();
return sMemory;
#else
return (char *)realloc(lpMemory, iSize * iCount);
#endif
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
char *MemoryPool::_dbg_CloneStringNSafe(char *&sOutBuffer, const char *sString, size_t iLength, const char *sCodeFile, const size_t iLineOfCode)
#else
char *MemoryPool::CloneStringNSafe(char *&sOutBuffer, const char *sString, size_t iLength)
#endif
{
if (sOutBuffer != NULL)
{
this->Free(sOutBuffer);
}
size_t iCount = iLength + 1;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
sOutBuffer = (char *) this->_dbg_Allocate(sizeof(char), iCount, sCodeFile, iLineOfCode);
#else
sOutBuffer = (char *) this->Allocate(sizeof(char), iCount);
#endif
if (sOutBuffer)
{
strncpy_s(sOutBuffer, iCount, sString, iLength);
}
return sOutBuffer;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
char *MemoryPool::_dbg_CloneStringN(const char *sString, size_t iLength, const char *sCodeFile, const size_t iLineOfCode)
#else
char *MemoryPool::CloneStringN(const char *sString, size_t iLength)
#endif
{
size_t iCount = iLength + 1;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
char *sNewText = (char *) this->_dbg_Allocate(sizeof(char), iCount, sCodeFile, iLineOfCode);
#else
char *sNewText = (char *) this->Allocate(sizeof(char), iCount);
#endif
if (sNewText)
{
strncpy_s(sNewText, iCount, sString, iLength);
}
return sNewText;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
char *MemoryPool::_dbg_CloneStringSafe(char *&sOutBuffer, const char *sString, const char *sCodeFile, const size_t iLineOfCode)
#else
char *MemoryPool::CloneStringSafe(char *&sOutBuffer, const char *sString)
#endif
{
if (sOutBuffer != NULL)
{
this->Free(sOutBuffer);
}
size_t iCount = strlen(sString) + 1;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
sOutBuffer = (char *) this->_dbg_Allocate(sizeof(char), iCount, sCodeFile, iLineOfCode);
#else
sOutBuffer = (char *) this->Allocate(sizeof(char), iCount);
#endif
if (sOutBuffer)
{
strcpy_s(sOutBuffer, iCount, sString);
}
return sOutBuffer;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
char *MemoryPool::_dbg_CloneString(const char *sString, const char *sCodeFile, const size_t iLineOfCode)
#else
char *MemoryPool::CloneString(const char *sString)
#endif
{
size_t iCount = strlen(sString) + 1;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
char *sNewText = (char *) this->_dbg_Allocate(sizeof(char), iCount, sCodeFile, iLineOfCode);
#else
char *sNewText = (char *) this->Allocate(sizeof(char), iCount);
#endif
if (sNewText)
{
strcpy_s(sNewText, iCount, sString);
}
return sNewText;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
void *MemoryPool::_dbg_Allocate(const size_t iSize, const size_t iCount,
const char *sCodeFile, const size_t iLineOfCode)
#else
void *MemoryPool::Allocate(const size_t iSize, const size_t iCount)
#endif
{
this->Lock();
char *sMemory = NULL;
size_t iSlot = 0;
if ((sMemory = (char *)calloc(iSize, iCount)) == NULL)
{
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Failed to allocate managed user memory for [%zd] bytes.", iSize * iCount);
MemoryPoolAssert("Allocate", this->sDebugText);
this->Unlock();
return NULL;
}
if ((iSlot = this->FindFreeAllocSlot()) == -1)
{
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Failed to find qualified memory slot for [%zd] bytes.", iSize * iCount);
MemoryPoolAssert("Allocate", this->sDebugText);
this->Unlock();
return NULL;
}
this->Items.Array[iSlot].iAddress = (size_t)sMemory;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
this->Items.Array[iSlot].iSize = (iCount * iSize);
this->Items.Array[iSlot].iLineOfCode = iLineOfCode;
this->Items.iReservedBytes += this->Items.Array[iSlot].iSize;
size_t iCodeFileAlloc = this->GetFileName(sCodeFile, sTempFile) + 1;
this->Items.Array[iSlot].sCodeFile = (char *)calloc(iCodeFileAlloc, sizeof(char));
memcpy_s(this->Items.Array[iSlot].sCodeFile, iCodeFileAlloc, sTempFile, iCodeFileAlloc);
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Alloc %d bytes. (0x%X) @ %s:%d\n",
iSize * iCount, sMemory,
this->Items.Array[iSlot].sCodeFile,
this->Items.Array[iSlot].iLineOfCode);
this->DbgWrite(this->sDebugText);
#endif
#endif
this->Items.iUsedCount++;
this->Unlock();
return sMemory;
}
#else
void *MemoryPool::Allocate(const size_t iSize, const size_t iCount)
{
#if _MEMPOOL_SEQUENTIAL_WHEN_OPTIMIZED != 0
this->Lock();
char *sMemory = (char *)calloc(iCount, iSize);
this->Unlock();
return sMemory;
#else
return (char *)calloc(iCount, iSize);
#endif
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
BOOL WINAPI MemoryPool_ConsoleHandler(DWORD dwCtrlType)
{
if (dwCtrlType == CTRL_CLOSE_EVENT)
{
return true;
}
return FALSE;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
bool MemoryPool::OpenConsole(void)
{
if (this->hStdOut == NULL)
{
if (AllocConsole())
{
SetConsoleCtrlHandler(MemoryPool_ConsoleHandler, TRUE);
#ifdef UNICODE
SetConsoleTitle(L"MemoryPool");
#else
SetConsoleTitle("MemoryPool");
#endif
}
if ((this->hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)) != NULL)
{
return true;
}
}
return false;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
bool MemoryPool::CloseConsole(void)
{
if (this->hStdOut)
{
if (FreeConsole())
{
CloseHandle(this->hStdOut);
this->hStdOut = NULL;
return true;
}
}
return false;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
size_t MemoryPool::GetFileName(const char *sInPath, char *sOutFile)
{
size_t iWPos = 0;
for (size_t iPos = (int)strlen(sInPath) - 1;
iPos >= 0 && sInPath[iPos] != '\\' && sInPath[iPos] != '/'; iPos--)
{
sOutFile[iWPos++] = sInPath[iPos];
}
sOutFile[iWPos] = '\0';
char *String1 = NULL;
char *String2 = NULL;
for (String1 = sOutFile, String2 = sOutFile + iWPos - 1;
String2 > String1;
++String1, --String2)
{
*String1 ^= *String2;
*String2 ^= *String1;
*String1 ^= *String2;
}
return iWPos;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
void MemoryPool::DbgWrite(const size_t iValue)
{
char sVal[64];
_itoa_s(iValue, sVal, sizeof(sVal), 10);
this->DbgWrite(sVal);
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
void MemoryPool::DbgWrite(const char *sBuffer)
{
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
if (this->hStdOut)
{
DWORD dwWritten = 0;
WriteFile(this->hStdOut, sBuffer, (int)strlen(sBuffer), &dwWritten, 0);
}
#endif
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
void MemoryPool::MemoryPoolAssertEx(const char *sExpression, const char *sMsg,
const char *sFunction, const char *sFile,
const char *sDate, const char *sTime, size_t iLine)
{
char sAssert[2048];
sprintf_s(sAssert, sizeof(sAssert),
"%s\r\n\r\n"
"Procedure: %s\r\n"
"Source File: %s\r\n"
"Build Date/Time: %s (%s)\r\n"
"Expression: \"%s\"\r\n"
"Line: %zd\r\n\r\n",
sMsg, sFunction, sFile, sDate, sTime, sExpression, iLine);
this->DbgWrite(sAssert);
#ifdef UNICODE
size_t iConverted = 0;
WCHAR sWCS[2048];
mbstowcs_s(&iConverted, sWCS, sizeof(sWCS) / sizeof(WCHAR), sAssert, sizeof(sAssert));
MessageBox(NULL, sWCS, L"MemoryPool Exception", MB_ICONSTOP | MB_SYSTEMMODAL);
#else
MessageBox(NULL, sAssert, "MemoryPool Exception", MB_ICONSTOP | MB_SYSTEMMODAL);
#endif
if (IsDebuggerPresent())
{
__debugbreak();
}
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
void *MemoryPool::UnTrackMemory(void *lpMemory)
{
this->Lock();
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
sprintf_s(this->sDebugText, sizeof(this->sDebugText), "Untracking address 0x%X\n", lpMemory);
this->DbgWrite(this->sDebugText);
#endif
size_t iSlot = 0;
if (lpMemory == NULL)
{
MemoryPoolAssert("Free", "Application attempted to untrack a NULL pointer.");
this->Unlock();
return lpMemory;
}
if ((iSlot = this->GetSlotByAddress((size_t)lpMemory)) != (size_t)-1)
{
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
free(this->Items.Array[iSlot].sCodeFile);
this->Items.iReservedBytes -= this->Items.Array[iSlot].iSize;
this->Items.Array[iSlot].sCodeFile = NULL;
this->Items.Array[iSlot].iLineOfCode = 0;
this->Items.Array[iSlot].iSize = 0;
#endif
this->Items.Array[iSlot].iAddress = 0;
this->Items.iUsedCount--;
}
else {
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Address [0x%zX] not found in the managed memory pool.", (size_t)lpMemory);
MemoryPoolAssert("Free", this->sDebugText);
this->Unlock();
return lpMemory;
}
this->Unlock();
return lpMemory;
}
#else
void *MemoryPool::UnTrackMemory(void *lpMemory)
{
return lpMemory;
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_BASIC
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
void *MemoryPool::_dbg_TrackMemory(void *lpMemory, const char *sCodeFile, const size_t iLineOfCode)
#else
void *MemoryPool::TrackMemory(void *lpMemory)
#endif
{
this->Lock();
size_t iSlot = 0;
if ((iSlot = this->FindFreeAllocSlot()) == -1)
{
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Failed to find qualified memory slot for tracked memory.");
MemoryPoolAssert("Allocate", this->sDebugText);
this->Unlock();
return NULL;
}
this->Items.Array[iSlot].iAddress = (size_t)lpMemory;
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_ADVANCED
this->Items.Array[iSlot].iSize = 0; //We do not know how large this allocation is.
this->Items.Array[iSlot].iLineOfCode = iLineOfCode;
//this->Items.iReservedBytes += this->Items.Array[iSlot].iSize;
size_t iCodeFileAlloc = this->GetFileName(sCodeFile, sTempFile) + 1;
this->Items.Array[iSlot].sCodeFile = (char *)calloc(iCodeFileAlloc, sizeof(char));
memcpy_s(this->Items.Array[iSlot].sCodeFile, iCodeFileAlloc, sTempFile, iCodeFileAlloc);
#if _MEMPOOL_DEBUG_LEVEL >= _MEMPOOL_DEBUG_VERBOSE
sprintf_s(this->sDebugText, sizeof(this->sDebugText),
"Alloc unknow bytes. (0x%X) @ %s:%d\n",
lpMemory,
this->Items.Array[iSlot].sCodeFile,
this->Items.Array[iSlot].iLineOfCode);
this->DbgWrite(this->sDebugText);
#endif
#endif
this->Items.iUsedCount++;
this->Unlock();
return lpMemory;
}
#else
void *MemoryPool::TrackMemory(void *lpMemory)
{
return lpMemory;
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace::Memory
} //namespace::NSWFL
#endif