-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCCPMemory.cpp
More file actions
808 lines (679 loc) · 15.8 KB
/
CCPMemory.cpp
File metadata and controls
808 lines (679 loc) · 15.8 KB
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
// Copyright © 2025 CCP ehf.
#include "include/CCPMemory.h"
#include "include/CCPMemoryTracker.h"
#include "include/CCPAssert.h"
#include "include/CcpTelemetry.h"
#include "CcpMemoryTrackerMutex.h"
#ifdef __APPLE__
#include <malloc/malloc.h>
#include <mach/mach.h>
#include <mach/task_info.h>
#elif defined(_WIN32)
#include <psapi.h>
#else
#include <malloc.h>
#endif
// We need to initialize the memory system before any other static initializers are executed.
#ifdef _WIN32
#pragma warning(suppress:4073)
#pragma init_seg(lib)
#endif
#ifdef _WIN32
HANDLE s_heap = INVALID_HANDLE_VALUE;
#endif
// Should allocations be guarded? Note that this cannot be changed at runtime - the only
// way to enable is via a command line switch that is processed in a static initializer.
// Once any allocations have been made this cannot be changed - things will crash and burn.
bool s_guardAllocations = false;
CcpMemoryTrackerMutex& GetTrackingMutex()
{
static CcpMemoryTrackerMutex s_trackingGuard;
return s_trackingGuard;
}
#define IS_2_POW_N(X) (((X)&(X-1)) == 0)
#define PTR_SZ sizeof(void *)
// Note that the guards must maintain the alignment of the allocated block
const int CCP_MEMORY_GUARD_SIZE_FRONT = 64 - sizeof( size_t );
const int CCP_MEMORY_GUARD_SIZE_BACK = 64;
const int CCP_MEMORY_GUARD_SIZE = CCP_MEMORY_GUARD_SIZE_FRONT + CCP_MEMORY_GUARD_SIZE_BACK + sizeof( size_t );
const uint8_t CCP_MEMORY_GUARD_VALUE_FRONT = 0xee;
const uint8_t CCP_MEMORY_GUARD_VALUE_BACK = 0xef;
// Should memory tracking in Telemetry be enabled? Defaults to yes. This can skew results of
// timing analysis of functions that do a lot of allocations, such as the yaml parser.
// Set this to 0 to disable it - please don't check it in like that!
#define ENABLE_TELEMETRY_MEMORY_TRACKING CCP_TELEMETRY_ENABLED
#ifdef _WIN32
static bool ValidateHeap()
{
if( !HeapLock( s_heap ) )
{
return false;
}
PROCESS_HEAP_ENTRY entry;
entry.lpData = NULL;
while( HeapWalk( s_heap, &entry ) )
{
if( entry.wFlags & PROCESS_HEAP_ENTRY_BUSY )
{
if( !HeapValidate( s_heap, 0, entry.lpData ) )
{
return false;
}
}
}
HeapUnlock( s_heap );
return true;
}
#endif
void* CCPMallocWithTracking( size_t size, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
void* p = CCPMalloc( size );
if( IsMemoryTrackingEnabled() && p )
{
MemoryTrackerAdd( p, size, name, file, line );
}
return p;
}
void* CCPCallocWithTracking( size_t nitems, size_t size, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
void* p = CCPCalloc( nitems, size );
if( IsMemoryTrackingEnabled() && p )
{
MemoryTrackerAdd( p, size, name, file, line );
}
return p;
}
void* CCPAlignedMallocWithTracking( size_t size, size_t alignment, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
void* p = CCPAlignedMalloc( size, alignment );
if( IsMemoryTrackingEnabled() && p )
{
MemoryTrackerAdd( p, size, name, file, line );
}
return p;
}
void CCPFreeWithTracking( void* p )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( p )
{
if( !MemoryTrackerIsValid( p ) )
{
CCP_LOGERR( "CCPFreeWithTracking: Pointer 0x%p is not valid for the heap", p );
CCP_DEBUG_BREAK();
}
MemoryTrackerRemove( p );
CCPFree( p );
}
}
void CCPAlignedFreeWithTracking( void* p )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( p )
{
if( IsMemoryTrackingEnabled() )
{
MemoryTrackerRemove( p );
}
CCPAlignedFree( p );
}
}
void* CCPReallocWithTracking( void* p, size_t size, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( p && !MemoryTrackerIsValid( p ) )
{
CCP_DEBUG_BREAK();
}
void* pNew = CCPRealloc( p, size );
if( IsMemoryTrackingEnabled() )
{
if( p )
{
MemoryTrackerRemove( p );
}
if( pNew )
{
MemoryTrackerAdd( pNew, size, name, file, line );
}
}
return pNew;
}
void* CCPAlignedReallocWithTracking( void* p, size_t size, size_t alignment, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( p && !MemoryTrackerIsValid( p ) )
{
CCP_DEBUG_BREAK();
}
void* pNew = CCPAlignedRealloc( p, size, alignment );
if( IsMemoryTrackingEnabled() )
{
if( p )
{
MemoryTrackerRemove( p );
}
if( pNew )
{
MemoryTrackerAdd( pNew, size, name, file, line );
}
}
return pNew;
}
size_t CCPMSizeWithTracking( void *p )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( !MemoryTrackerIsValid( p ) )
{
CCP_LOGERR( "CCPMSizeWithTracking: Pointer 0x%p is not valid for the heap", p );
CCP_DEBUG_BREAK();
}
return CCPMSize( p );
}
char* CCPStrDupWithTracking( const char* s, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( !s )
{
return NULL;
}
size_t size = strlen(s) + 1;
char* s2 = (char*)CCPMallocWithTracking( size, name, file, line );
strcpy_s( s2, size, s );
return s2;
}
wchar_t* CCPWStrDupWithTracking( const wchar_t* s, const char* name, const char* file, int line )
{
CcpMemoryTrackerAutoMutex guard( GetTrackingMutex() );
if( !s )
{
return NULL;
}
size_t size = wcslen( s ) + 1;
wchar_t* s2 = (wchar_t*)CCPMallocWithTracking( size * sizeof(wchar_t), name, file, line );
if (s2)
wcscpy_s( s2, size, s );
return s2;
}
static inline size_t CalcSizeHelper( size_t items, size_t size )
{
auto ret = items * size;
if ( ret == 0 || ( items != ret / size ) )
{
return 0;
}
return ret;
}
#ifdef _WIN32
//memory usage statistic
#ifdef _WIN64
LONGLONG s_memuse = 0;
static inline void UpdateCount(SSIZE_T s)
{
InterlockedExchangeAdd64(&s_memuse, (LONGLONG)s);
}
#else
LONG s_memuse = 0;
static inline void UpdateCount(SSIZE_T s)
{
InterlockedExchangeAdd(&s_memuse, (LONG)s);
}
#endif
static inline void UpdateCount(void *p, bool inc)
{
if (!p)
return;
SSIZE_T s = HeapSize(s_heap, 0, p);
if (!inc)
s = -s;
UpdateCount(s);
}
static inline void* CcpPlatformMalloc( size_t size )
{
void* p = HeapAlloc( s_heap, 0, size );
UpdateCount( size );
#if ENABLE_TELEMETRY_MEMORY_TRACKING
if ( p && CcpTelemetryIsConnected() )
{
// TracySecureAlloc( p, size );
}
#endif
return p;
}
static inline void* CcpPlatformCalloc( size_t items, size_t size )
{
auto bytes = CalcSizeHelper( items, size );
if (bytes == 0)
{
return nullptr;
}
void* p = HeapAlloc( s_heap, HEAP_ZERO_MEMORY, bytes );
UpdateCount( bytes );
#if ENABLE_TELEMETRY_MEMORY_TRACKING
if ( p && CcpTelemetryIsConnected() )
{
// TracySecureAlloc( p, size );
}
#endif
return p;
}
static inline void CcpPlatformFree( void* p )
{
UpdateCount( p, false );
HeapFree( s_heap, 0, p );
}
#else
std::atomic<size_t> s_memuse( 0 );
static inline void* CcpPlatformMalloc( size_t size )
{
#if defined(__ANDROID__)
size += sizeof( size_t );
#endif
void* p = malloc( size );
if( p )
{
auto realSize = CCPMSize( p );
s_memuse += realSize;
#if ENABLE_TELEMETRY_MEMORY_TRACKING
if ( CcpTelemetryIsConnected() ) {
// TracySecureAlloc( p, realSize );
}
#endif
}
#if defined(__ANDROID__)
if( p )
{
*reinterpret_cast<size_t*>( p ) = size - sizeof( size_t );
p = reinterpret_cast<size_t*>( p ) + 1;
}
#endif
return p;
}
static inline void* CcpPlatformCalloc( size_t items, size_t size )
{
void* p = calloc( items, size );
if ( p )
{
auto realSize = CCPMSize( p );
s_memuse += realSize;
#if ENABLE_TELEMETRY_MEMORY_TRACKING
if ( CcpTelemetryIsConnected() )
{
// TracySecureAlloc( p, realSize );
}
#endif
}
return p;
}
static inline void CcpPlatformFree( void* p )
{
#if defined(__ANDROID__)
p = reinterpret_cast<size_t*>( p ) - 1;
#endif
s_memuse -= CCPMSize( p );
free( p );
}
#endif
void *WriteMemoryGuard(size_t orgSize, void *p) {
uintptr_t p0 = (uintptr_t)p;
// Write original size requested
*(size_t*)p0 = orgSize;
uintptr_t pg = p0 + sizeof( size_t );
// Write guard bytes in front
memset( (void*)pg, CCP_MEMORY_GUARD_VALUE_FRONT, CCP_MEMORY_GUARD_SIZE_FRONT );
uintptr_t p1 = (uintptr_t)p0 + CCP_MEMORY_GUARD_SIZE_FRONT + sizeof( size_t );
uintptr_t p2 = p1 + orgSize;
// Write guard bytes in back
memset( (void*)p2, CCP_MEMORY_GUARD_VALUE_BACK, CCP_MEMORY_GUARD_SIZE_BACK );
p = (void*)p1;
return p;
}
void* CCPMallocWithGuard( size_t size )
{
size_t orgSize = size;
size += CCP_MEMORY_GUARD_SIZE;
void* p = CcpPlatformMalloc( size );
if( !p )
{
CCP_LOGERR( "Failed to allocate %zd bytes of memory", size );
}
if( p )
{
p = WriteMemoryGuard(orgSize, p);
}
return p;
}
void* CCPMalloc( size_t size )
{
if( s_guardAllocations )
{
return CCPMallocWithGuard( size );
}
else
{
void* p = CcpPlatformMalloc( size );
if( !p )
{
CCP_LOGERR( "Failed to allocate %zd bytes of memory", size );
}
return p;
}
}
void* CCPCallocWithGuard(size_t items, size_t size )
{
size_t orgSize = CalcSizeHelper( items, size );
if ( orgSize == 0 ) {
return nullptr;
}
size_t sizeWithGuard = orgSize + CCP_MEMORY_GUARD_SIZE;
// Simply pretend it's one large item we allocate here, otherwise
// calculating the guard size / offsets gets really complicated.
void* p = CcpPlatformCalloc( 1, sizeWithGuard );
if( !p )
{
CCP_LOGERR( "Failed to allocate %zd bytes of memory", size );
}
if( p )
{
p = WriteMemoryGuard(orgSize, p);
}
return p;
}
void* CCPCalloc( size_t items, size_t size )
{
if( s_guardAllocations )
{
return CCPCallocWithGuard( items, size );
}
else
{
void* p = CcpPlatformCalloc( items, size );
if( !p )
{
CCP_LOGERR( "Failed to allocate %zd bytes of memory", size );
}
return p;
}
}
void CCPFreeWithGuard( void* p )
{
if( !p )
{
return;
}
// Calculate the pointer that was originally allocated
uintptr_t p0 = (uintptr_t)p - CCP_MEMORY_GUARD_SIZE_FRONT - sizeof( size_t );
// Get the size
size_t orgSize = *(size_t*)p0;
if( orgSize == 0xdddddddd )
{
CCP_LOGERR( "Freeing a block that was already freed at 0x%p", p );
CCP_DEBUG_BREAK();
}
uintptr_t pg = p0 + sizeof( size_t );
uintptr_t p2 = (uintptr_t)p + orgSize;
uint8_t* test = (uint8_t*)pg;
for( int i = 0; i < CCP_MEMORY_GUARD_SIZE_FRONT; ++i )
{
if( *test != CCP_MEMORY_GUARD_VALUE_FRONT )
{
CCP_LOGERR( "Write outside of allocated memory detected in front of block at 0x%p", p );
CCP_DEBUG_BREAK();
break;
}
++test;
}
test = (uint8_t*)p2;
for( int i = 0; i < CCP_MEMORY_GUARD_SIZE_BACK; ++i )
{
if( *test != CCP_MEMORY_GUARD_VALUE_BACK )
{
CCP_LOGERR( "Write outside of allocated memory detected after block at 0x%p", p );
CCP_DEBUG_BREAK();
break;
}
++test;
}
memset( (void*)p0, 0xdd, orgSize + CCP_MEMORY_GUARD_SIZE );
// Set 'p' to the pointer originally allocated before the call to HeapFree below
p = (void*)p0;
CcpPlatformFree( p );
}
void CCPFree( void* p )
{
if( p )
{
#if ENABLE_TELEMETRY_MEMORY_TRACKING
if ( CcpTelemetryIsConnected() )
{
// TracySecureFree( p );
}
#endif
if( s_guardAllocations )
{
CCPFreeWithGuard( p );
}
else
{
CcpPlatformFree( p );
}
}
}
void* CCPAlignedMalloc( size_t size, size_t alignment )
{
// Aligned allocation is done by simply allocating a larger block to allow for potentially
// misaligned memory coming from the underlying allocation.
// |s|___w___|p|_________data block__________|_w_|
//
// s -> Pointer to start of the block allocated by malloc.
// p -> Value of s.
// w -> Wasted memory.
CCP_ASSERT( IS_2_POW_N( alignment ) );
alignment = (alignment > PTR_SZ ? alignment : PTR_SZ) -1;
uintptr_t ptr = (uintptr_t)CCPMalloc( PTR_SZ + alignment + size );
if( !ptr )
{
return NULL;
}
uintptr_t retPtr = ((ptr + PTR_SZ + alignment) & ~alignment);
((uintptr_t *)retPtr)[-1] = ptr;
return (void *)retPtr;
}
void CCPAlignedFree( void* p )
{
if( p )
{
uintptr_t* ptr = (uintptr_t*)p;
void* allocatedPtr = (void*)ptr[-1];
if( s_guardAllocations )
{
if( (uintptr_t)allocatedPtr == 0xdddddddd )
{
CCP_LOGERR( "Freeing an aligned block that was already freed at 0x%p", p );
CCP_DEBUG_BREAK();
return;
}
}
CCPFree( allocatedPtr );
}
}
char* CCPStrDup( const char* s )
{
if( !s )
{
return NULL;
}
size_t size = strlen(s) + 1;
char* s2 = (char*)CCPMalloc( size );
if( s2 )
{
strcpy_s( s2, size, s );
}
return s2;
}
wchar_t* CCPWStrDup( const wchar_t* s)
{
if( !s )
{
return NULL;
}
size_t size = wcslen( s ) + 1;
wchar_t* s2 = (wchar_t*)CCPMalloc( size*sizeof(wchar_t) );
if( s2 )
{
wcscpy_s( s2, size, s );
}
return s2;
}
void* CCPRealloc( void* p, size_t size )
{
if( !p )
{
p = CCPMalloc( size );
}
else
{
void* pNew = CCPMalloc( size );
if( pNew )
{
size_t oldSize = CCPMSize( p );
size_t toCopy = oldSize;
if( size < oldSize )
{
toCopy = size;
}
memcpy( pNew, p, toCopy );
CCPFree( p );
p = pNew;
}
else
{
return nullptr;
}
}
return p;
}
void* CCPAlignedRealloc( void* p, size_t size, size_t alignment )
{
if( !p )
{
p = CCPAlignedMalloc( size, alignment );
}
else
{
uintptr_t* ptr = (uintptr_t*)p;
void* allocatedPtr = (void*)ptr[-1];
size_t oldSize = CCPMSize(allocatedPtr);
auto offset = static_cast<uint8_t*>( p ) - static_cast<uint8_t*>( allocatedPtr );
oldSize -= offset;
void* pNew = CCPAlignedMalloc( size, alignment );
if( pNew )
{
size_t toCopy = oldSize;
if( size < oldSize )
{
toCopy = size;
}
memcpy( pNew, p, toCopy );
CCPAlignedFree( p );
p = pNew;
}
else
{
return nullptr;
}
}
return p;
}
size_t CCPMSize( void *p )
{
if( s_guardAllocations )
{
// Calculate the pointer that was originally allocated
uintptr_t p0 = (uintptr_t)p - CCP_MEMORY_GUARD_SIZE_FRONT - sizeof( size_t );
// Get the size
size_t orgSize = *(size_t*)p0;
return orgSize;
}
else
{
#ifdef _WIN32
return HeapSize( s_heap, 0, p );
#elif defined(__APPLE__)
return malloc_size( p );
#elif defined(__ANDROID__)
return reinterpret_cast<size_t*>( p )[-1];
#else
return malloc_usable_size( p );
#endif
}
}
size_t CCPMallocUsage()
{
return size_t( s_memuse );
}
#ifdef _WIN32
class CcpMemoryInitializer
{
public:
CcpMemoryInitializer()
{
#ifdef _MSC_VER
// sigh. simply use the crt heap - we occasionally run into issues where
// things are allocated in one heap, freed on another, usually in
// std::string.
s_heap = (HANDLE)_get_heap_handle();
#else
s_heap = HeapCreate( 0, 0, 0 );
#endif
#ifdef _MSC_VER
ULONG heapFragValue = 2;
HeapSetInformation( s_heap, HeapCompatibilityInformation, &heapFragValue, sizeof( heapFragValue ) );
#endif
const wchar_t* cmdLine = GetCommandLineW();
const wchar_t* found = wcsstr( cmdLine, L"/memoryGuards" );
if( found )
{
s_guardAllocations = true;
}
found = wcsstr( cmdLine, L"/memoryTracking" );
if( found )
{
MemoryTrackerInitialize();
}
}
};
static CcpMemoryInitializer initializeCcpMemory;
#endif
bool CcpGetProcessMemoryInfo( CcpProcessMemoryInfo& result )
{
#ifdef _WIN32
PROCESS_MEMORY_COUNTERS mc;
if( GetProcessMemoryInfo( GetCurrentProcess(), &mc, sizeof(mc)) )
{
result.pageFaultCount = mc.PageFaultCount;
result.workingSetSize = mc.WorkingSetSize;
result.pageFileUsage = mc.PagefileUsage;
return true;
}
#elif defined(__APPLE__)
task_vm_info_data_t vmInfo;
mach_msg_type_number_t infoCount = TASK_VM_INFO_COUNT;
if( task_info( mach_task_self(), TASK_VM_INFO, (task_info_t)&vmInfo, &infoCount ) == KERN_SUCCESS )
{
result.workingSetSize = size_t( vmInfo.resident_size );
result.pageFileUsage = size_t( vmInfo.phys_footprint );
}
task_events_info_data_t vmEvents;
infoCount = TASK_EVENTS_INFO_COUNT;
if ( task_info( mach_task_self(), TASK_EVENTS_INFO, (task_info_t)&vmEvents, &infoCount ) == KERN_SUCCESS )
{
result.pageFaultCount = size_t( vmEvents.faults );
return true;
}
#endif
return false;
}