-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.cpp
2315 lines (2013 loc) · 53.1 KB
/
Heap.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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../idlib/precompiled.h"
#pragma hdrstop
// RAVEN BEGIN
// jsinger: attempt to eliminate cross-DLL allocation issues
#ifdef RV_UNIFIED_ALLOCATOR
void *(*Memory::mAllocator)(size_t size);
void (*Memory::mDeallocator)(void *ptr);
size_t (*Memory::mMSize)(void *ptr);
bool Memory::sOK = true;
void Memory::Error(const char *errStr)
{
// If you land here it's because you allocated dynamic memory in a DLL
// before the Memory system was initialized and this will lead to instability
// later. We can't assert here since that will cause the DLL to fail loading
// so set a break point on the BreakHere=0xdeadbeef; line in order to debug
int BreakHere;
BreakHere=0xdeadbeef;
if(common != NULL)
{
// this can't be an error or the game will fail to load the DLL and crash.
// While a crash is the desired behavior, a crash without any meaningful
// message to the user indicating what went wrong is not desired.
common->Warning(errStr);
}
else
{
// if common isn't initialized then we set a flag so that we can notify once it
// gets initialized
sOK = false;
}
}
#endif
// RAVEN END
#ifndef _RV_MEM_SYS_SUPPORT
#ifndef USE_LIBC_MALLOC
#ifdef _XBOX
#define USE_LIBC_MALLOC 1
#else
// RAVEN BEGIN
// scork: changed this to a 1, since 0 will crash Radiant if you BSP large maps ~3 times because of high watermark-mem that never gets freed.
#define USE_LIBC_MALLOC 1
// RAVEN END
#endif
#endif
#ifndef CRASH_ON_STATIC_ALLOCATION
// #define CRASH_ON_STATIC_ALLOCATION
#endif
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
inline
void *local_malloc(size_t size)
{
#ifndef _XENON
// RAVEN BEGIN
// jsinger: attempt to eliminate cross-DLL allocation issues
#ifdef RV_UNIFIED_ALLOCATOR
return Memory::Allocate(size);
#else
void *addr = malloc(size);
if( !addr && size ) {
common->FatalError( "Out of memory" );
}
return( addr );
#endif // RV_UNIFIED_ALLOCATOR
// RAVEN END
#else
#endif
}
inline
void local_free(void *ptr)
{
#ifndef _XENON
// RAVEN BEGIN
// jsinger: attempt to eliminate cross-DLL allocation issues
#ifdef RV_UNIFIED_ALLOCATOR
Memory::Free(ptr);
#else
return free(ptr);
#endif // RV_UNIFIED_ALLOCATOR
// RAVEN END
#else
#endif
}
// RAVEN END
//===============================================================
//
// idHeap
//
//===============================================================
// RAVEN BEGIN
// amccarthy: Added space in headers in debug for allocation tag storage
#ifdef _DEBUG
#define SMALL_HEADER_SIZE ( (int) ( sizeof( byte ) + sizeof( byte )+ sizeof( byte ) ) )
#define MEDIUM_HEADER_SIZE ( (int) ( sizeof( mediumHeapEntry_s ) + sizeof( byte )+ sizeof( byte ) ) )
#define LARGE_HEADER_SIZE ( (int) ( sizeof( dword * ) + sizeof( byte )+ sizeof( byte ) ) )
#define MALLOC_HEADER_SIZE ( (int) ( 11*sizeof( byte ) + sizeof( byte ) + sizeof( dword )))
#else
#define SMALL_HEADER_SIZE ( (int) ( sizeof( byte ) + sizeof( byte ) ) )
#define MEDIUM_HEADER_SIZE ( (int) ( sizeof( mediumHeapEntry_s ) + sizeof( byte ) ) )
#define LARGE_HEADER_SIZE ( (int) ( sizeof( dword * ) + sizeof( byte ) ) )
#endif
// RAVEN END
#define ALIGN_SIZE( bytes ) ( ( (bytes) + ALIGN - 1 ) & ~(ALIGN - 1) )
#define SMALL_ALIGN( bytes ) ( ALIGN_SIZE( (bytes) + SMALL_HEADER_SIZE ) - SMALL_HEADER_SIZE )
#define MEDIUM_SMALLEST_SIZE ( ALIGN_SIZE( 256 ) + ALIGN_SIZE( MEDIUM_HEADER_SIZE ) )
// RAVEN BEGIN
// amccarthy: Allocation tag tracking and reporting
#ifdef _DEBUG
int OutstandingXMallocSize=0;
int OutstandingXMallocTagSize[MA_MAX];
int PeakXMallocTagSize[MA_MAX];
int CurrNumAllocations[MA_MAX];
// Descriptions that go with each tag. When updating the tag enum in Heap.h please
// update this list as well.
// (also update the list in rvHeap.cpp)
char *TagNames[] = {
"none",
"New operation",
"default",
"Lexer",
"Parser",
"AAS routing",
"Class",
"Script program",
"Collision Model",
"CVar",
"Decl System",
"File System",
"Images",
"Materials",
"Models",
"Fonts",
"Main renderer",
"Vertex data",
"Sound",
"Window",
"Event loop",
"Math - Matrices and vectors",
"Animation",
"Dynamic Blocks",
"Strings",
"GUI",
"Effects",
"Entities",
"Physics",
"AI",
"Network",
"Not Used"
};
// jnewquist: Detect when the tag descriptions are out of sync with the enums.
template<int X>
class TagTableCheck
{
private:
TagTableCheck();
};
template<>
class TagTableCheck<1>
{
};
// An error here means you need to synchronize TagNames and Mem_Alloc_Types_t
TagTableCheck<sizeof(TagNames)/sizeof(char*) == MA_MAX> TagTableCheckedHere;
// An error here means there are too many tags. No more than 32!
TagTableCheck<MA_DO_NOT_USE<32> TagMaxCheckedHere;
void PrintOutstandingMemAlloc()
{
int i;
unsigned long totalOutstanding = 0;
for (i=0;i<MA_MAX;i++)
{
if (OutstandingXMallocTagSize[i] || PeakXMallocTagSize[i])
{
idLib::common->Printf("%-30s peak %9d curr %9d\n",TagNames[i],PeakXMallocTagSize[i],OutstandingXMallocTagSize[i]);
totalOutstanding += OutstandingXMallocTagSize[i];
}
}
idLib::common->Printf("Mem_Alloc Outstanding: %d\n",totalOutstanding);
}
const char *GetMemAllocStats(int tag, int &num, int &size, int &peak)
{
num = CurrNumAllocations[tag];
size = OutstandingXMallocTagSize[tag];
peak = PeakXMallocTagSize[tag];
return TagNames[tag];
}
#endif
/*
=================
Mem_ShowMemAlloc_f
=================
*/
// amccarthy: print out outstanding mem_allocs.
void Mem_ShowMemAlloc_f( const idCmdArgs &args ) {
#ifdef _DEBUG
PrintOutstandingMemAlloc();
#endif
}
// jnewquist: memory tag stack for new/delete
#if defined(_DEBUG) && !defined(ENABLE_INTEL_SMP)
MemScopedTag* MemScopedTag::mTop = NULL;
#endif
//RAVEN END
class idHeap {
public:
idHeap( void );
~idHeap( void ); // frees all associated data
void Init( void ); // initialize
//RAVEN BEGIN
//amccarthy: Added allocation tag
void * Allocate( const dword bytes, byte tag ); // allocate memory
//RAVEN END
void Free( void *p ); // free memory
//RAVEN BEGIN
//amccarthy: Added allocation tag
void * Allocate16( const dword bytes, byte tag );// allocate 16 byte aligned memory
//RAVEN END
void Free16( void *p ); // free 16 byte aligned memory
dword Msize( void *p ); // return size of data block
void Dump( void );
void AllocDefragBlock( void ); // hack for huge renderbumps
private:
enum {
ALIGN = 8 // memory alignment in bytes
};
enum {
INVALID_ALLOC = 0xdd,
SMALL_ALLOC = 0xaa, // small allocation
MEDIUM_ALLOC = 0xbb, // medium allocaction
LARGE_ALLOC = 0xcc // large allocaction
};
struct page_s { // allocation page
void * data; // data pointer to allocated memory
dword dataSize; // number of bytes of memory 'data' points to
page_s * next; // next free page in same page manager
page_s * prev; // used only when allocated
dword largestFree; // this data used by the medium-size heap manager
void * firstFree; // pointer to first free entry
};
struct mediumHeapEntry_s {
page_s * page; // pointer to page
dword size; // size of block
mediumHeapEntry_s * prev; // previous block
mediumHeapEntry_s * next; // next block
mediumHeapEntry_s * prevFree; // previous free block
mediumHeapEntry_s * nextFree; // next free block
dword freeBlock; // non-zero if free block
};
// variables
void * smallFirstFree[256/ALIGN+1]; // small heap allocator lists (for allocs of 1-255 bytes)
page_s * smallCurPage; // current page for small allocations
dword smallCurPageOffset; // byte offset in current page
page_s * smallFirstUsedPage; // first used page of the small heap manager
page_s * mediumFirstFreePage; // first partially free page
page_s * mediumLastFreePage; // last partially free page
page_s * mediumFirstUsedPage; // completely used page
page_s * largeFirstUsedPage; // first page used by the large heap manager
page_s * swapPage;
dword pagesAllocated; // number of pages currently allocated
dword pageSize; // size of one alloc page in bytes
dword pageRequests; // page requests
dword OSAllocs; // number of allocs made to the OS
int c_heapAllocRunningCount;
void *defragBlock; // a single huge block that can be allocated
// at startup, then freed when needed
// methods
page_s * AllocatePage( dword bytes ); // allocate page from the OS
void FreePage( idHeap::page_s *p ); // free an OS allocated page
//RAVEN BEGIN
//amccarthy: Added allocation tags
void * SmallAllocate( dword bytes, byte tag ); // allocate memory (1-255 bytes) from small heap manager
void SmallFree( void *ptr ); // free memory allocated by small heap manager
void * MediumAllocateFromPage( idHeap::page_s *p, dword sizeNeeded, byte tag );
void * MediumAllocate( dword bytes, byte tag ); // allocate memory (256-32768 bytes) from medium heap manager
void MediumFree( void *ptr ); // free memory allocated by medium heap manager
void * LargeAllocate( dword bytes, byte tag ); // allocate large block from OS directly
void LargeFree( void *ptr ); // free memory allocated by large heap manager
//RAVEN END
void ReleaseSwappedPages( void );
void FreePageReal( idHeap::page_s *p );
};
/*
================
idHeap::Init
================
*/
void idHeap::Init () {
OSAllocs = 0;
pageRequests = 0;
pageSize = 65536 - sizeof( idHeap::page_s );
pagesAllocated = 0; // reset page allocation counter
largeFirstUsedPage = NULL; // init large heap manager
swapPage = NULL;
memset( smallFirstFree, 0, sizeof(smallFirstFree) ); // init small heap manager
smallFirstUsedPage = NULL;
smallCurPage = AllocatePage( pageSize );
assert( smallCurPage );
smallCurPageOffset = SMALL_ALIGN( 0 );
defragBlock = NULL;
mediumFirstFreePage = NULL; // init medium heap manager
mediumLastFreePage = NULL;
mediumFirstUsedPage = NULL;
c_heapAllocRunningCount = 0;
//RAVEN BEGIN
//amccarthy: initalize allocation tracking
#ifdef _DEBUG
OutstandingXMallocSize=0;
int i;
for (i=0;i<MA_MAX;i++)
{
OutstandingXMallocTagSize[i] = 0;
PeakXMallocTagSize[i] = 0;
CurrNumAllocations[i] = 0;
}
#endif
//RAVEN END
}
/*
================
idHeap::idHeap
================
*/
idHeap::idHeap( void ) {
Init();
}
/*
================
idHeap::~idHeap
returns all allocated memory back to OS
================
*/
idHeap::~idHeap( void ) {
idHeap::page_s *p;
if ( smallCurPage ) {
FreePage( smallCurPage ); // free small-heap current allocation page
}
p = smallFirstUsedPage; // free small-heap allocated pages
while( p ) {
idHeap::page_s *next = p->next;
FreePage( p );
p= next;
}
p = largeFirstUsedPage; // free large-heap allocated pages
while( p ) {
idHeap::page_s *next = p->next;
FreePage( p );
p = next;
}
p = mediumFirstFreePage; // free medium-heap allocated pages
while( p ) {
idHeap::page_s *next = p->next;
FreePage( p );
p = next;
}
p = mediumFirstUsedPage; // free medium-heap allocated completely used pages
while( p ) {
idHeap::page_s *next = p->next;
FreePage( p );
p = next;
}
ReleaseSwappedPages();
if ( defragBlock ) {
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
local_free( defragBlock );
// RAVEN END
}
assert( pagesAllocated == 0 );
}
/*
================
idHeap::AllocDefragBlock
================
*/
void idHeap::AllocDefragBlock( void ) {
int size = 0x40000000;
if ( defragBlock ) {
return;
}
while( 1 ) {
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
defragBlock = local_malloc( size );
// RAVEN END
if ( defragBlock ) {
break;
}
size >>= 1;
}
idLib::common->Printf( "Allocated a %i mb defrag block\n", size / (1024*1024) );
}
/*
================
idHeap::Allocate
================
*/
//RAVEN BEGIN
//amccarthy: Added allocation tag
void *idHeap::Allocate( const dword bytes, byte tag ) {
//RAVEN END
if ( !bytes ) {
return NULL;
}
c_heapAllocRunningCount++;
#if USE_LIBC_MALLOC
//RAVEN BEGIN
//amccarthy: Added allocation tags for malloc
#ifdef _DEBUG
byte *p = (byte *)local_malloc( bytes + MALLOC_HEADER_SIZE );
OutstandingXMallocSize += bytes;
assert( tag > 0 && tag < MA_MAX);
OutstandingXMallocTagSize[tag] += bytes;
if (OutstandingXMallocTagSize[tag] > PeakXMallocTagSize[tag])
{
PeakXMallocTagSize[tag] = OutstandingXMallocTagSize[tag];
}
CurrNumAllocations[tag]++;
dword *d = (dword *)p;
d[0] = bytes;
byte *ret = p+MALLOC_HEADER_SIZE;
ret[-1] = tag;
return ret;
#else
return local_malloc( bytes);
#endif
//RAVEN END
#else
//RAVEN BEGIN
//amccarthy: Added allocation tag
if ( !(bytes & ~255) ) {
return SmallAllocate( bytes, tag );
}
if ( !(bytes & ~32767) ) {
return MediumAllocate( bytes, tag );
}
return LargeAllocate( bytes, tag );
//RAVEN END
#endif
}
/*
================
idHeap::Free
================
*/
void idHeap::Free( void *p ) {
if ( !p ) {
return;
}
c_heapAllocRunningCount--;
#if USE_LIBC_MALLOC
//RAVEN BEGIN
//amccarthy: allocation tracking
#ifdef _DEBUG
byte *ptr = ((byte *)p) - MALLOC_HEADER_SIZE;
dword size = ((dword*)(ptr))[0];
byte tag = ((byte *)(p))[-1];
OutstandingXMallocSize -= size;
assert( tag > 0 && tag < MA_MAX);
OutstandingXMallocTagSize[tag] -= size;
CurrNumAllocations[tag]--;
local_free( ptr );
#else
local_free( p );
#endif
//RAVEN END
#else
switch( ((byte *)(p))[-1] ) {
case SMALL_ALLOC: {
SmallFree( p );
break;
}
case MEDIUM_ALLOC: {
MediumFree( p );
break;
}
case LARGE_ALLOC: {
LargeFree( p );
break;
}
default: {
idLib::common->FatalError( "idHeap::Free: invalid memory block (%s)", idLib::sys->GetCallStackCurStr( 4 ) );
break;
}
}
#endif
}
/*
================
idHeap::Allocate16
================
*/
//RAVEN BEGIN
//amccarthy: Added allocation tag
void *idHeap::Allocate16( const dword bytes, byte tag ) {
//RAVEN END
byte *ptr, *alignedPtr;
//RAVEN BEGIN
//amccarthy: Added allocation tag
#ifdef _DEBUG
ptr = (byte *) Allocate(bytes+16, tag);
alignedPtr = (byte *) ( ( (int) ptr ) + 15 & ~15 );
int padSize = alignedPtr - ptr;
if ( padSize == 0 ) {
alignedPtr += 16;
padSize += 16;
}
*((byte *)(alignedPtr - 1)) = (byte) padSize;
assert( ( unsigned int )alignedPtr < 0xff000000 );
return (void *) alignedPtr;
#else
//RAVEN END
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
ptr = (byte *) local_malloc( bytes + 16 + 4 );
// RAVEN END
if ( !ptr ) {
if ( defragBlock ) {
idLib::common->Printf( "Freeing defragBlock on alloc of %i.\n", bytes );
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
local_free( defragBlock );
// RAVEN END
defragBlock = NULL;
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
ptr = (byte *) local_malloc( bytes + 16 + 4 );
// RAVEN END
AllocDefragBlock();
}
if ( !ptr ) {
common->FatalError( "malloc failure for %i", bytes );
}
}
alignedPtr = (byte *) ( ( (int) ptr ) + 15 & ~15 );
if ( alignedPtr - ptr < 4 ) {
alignedPtr += 16;
}
*((int *)(alignedPtr - 4)) = (int) ptr;
assert( ( unsigned int )alignedPtr < 0xff000000 );
return (void *) alignedPtr;
//RAVEN BEGIN
//amccarthy: allocation tracking added for debug xbox only.
#endif
//RAVEN END
}
/*
================
idHeap::Free16
================
*/
void idHeap::Free16( void *p ) {
//RAVEN BEGIN
//amccarthy: allocation tag
#ifdef _DEBUG
byte* ptr = (byte*)p;
int padSize = *(ptr-1);
ptr -= padSize;
Free( ptr );
#else
local_free( (void *) *((int *) (( (byte *) p ) - 4)) );
#endif
//RAVEN END
}
/*
================
idHeap::Msize
returns size of allocated memory block
p = pointer to memory block
Notes: size may not be the same as the size in the original
allocation request (due to block alignment reasons).
================
*/
dword idHeap::Msize( void *p ) {
if ( !p ) {
return 0;
}
#if USE_LIBC_MALLOC
#ifdef _WINDOWS
//RAVEN BEGIN
//amccarthy: allocation tracking
#ifdef _DEBUG
byte *ptr = ((byte *)p) - MALLOC_HEADER_SIZE;
// jsinger: attempt to eliminate cross-DLL allocation issues
#ifdef RV_UNIFIED_ALLOCATOR
return Memory::MSize(ptr);
#else
return _msize(ptr);
#endif // RV_UNIFIED_ALLOCATOR
#else
#ifdef RV_UNIFIED_ALLOCATOR
return Memory::MSize(p);
#else
return _msize(p);
#endif // RV_UNIFIED_ALLOCATOR
#endif
//RAVEN END
#else
return 0;
#endif
#else
switch( ((byte *)(p))[-1] ) {
case SMALL_ALLOC: {
return SMALL_ALIGN( ((byte *)(p))[-SMALL_HEADER_SIZE] * ALIGN );
}
case MEDIUM_ALLOC: {
return ((mediumHeapEntry_s *)(((byte *)(p)) - ALIGN_SIZE( MEDIUM_HEADER_SIZE )))->size - ALIGN_SIZE( MEDIUM_HEADER_SIZE );
}
case LARGE_ALLOC: {
return ((idHeap::page_s*)(*((dword *)(((byte *)p) - ALIGN_SIZE( LARGE_HEADER_SIZE )))))->dataSize - ALIGN_SIZE( LARGE_HEADER_SIZE );
}
default: {
idLib::common->FatalError( "idHeap::Msize: invalid memory block (%s)", idLib::sys->GetCallStackCurStr( 4 ) );
return 0;
}
}
#endif
}
/*
================
idHeap::Dump
dump contents of the heap
================
*/
void idHeap::Dump( void ) {
idHeap::page_s *pg;
for ( pg = smallFirstUsedPage; pg; pg = pg->next ) {
idLib::common->Printf( "%p bytes %-8d (in use by small heap)\n", pg->data, pg->dataSize);
}
if ( smallCurPage ) {
pg = smallCurPage;
idLib::common->Printf( "%p bytes %-8d (small heap active page)\n", pg->data, pg->dataSize );
}
for ( pg = mediumFirstUsedPage; pg; pg = pg->next ) {
idLib::common->Printf( "%p bytes %-8d (completely used by medium heap)\n", pg->data, pg->dataSize );
}
for ( pg = mediumFirstFreePage; pg; pg = pg->next ) {
idLib::common->Printf( "%p bytes %-8d (partially used by medium heap)\n", pg->data, pg->dataSize );
}
for ( pg = largeFirstUsedPage; pg; pg = pg->next ) {
idLib::common->Printf( "%p bytes %-8d (fully used by large heap)\n", pg->data, pg->dataSize );
}
idLib::common->Printf( "pages allocated : %d\n", pagesAllocated );
}
/*
================
idHeap::FreePageReal
frees page to be used by the OS
p = page to free
================
*/
void idHeap::FreePageReal( idHeap::page_s *p ) {
assert( p );
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
::local_free( p );
// RAVEN END
}
/*
================
idHeap::ReleaseSwappedPages
releases the swap page to OS
================
*/
void idHeap::ReleaseSwappedPages () {
if ( swapPage ) {
FreePageReal( swapPage );
}
swapPage = NULL;
}
/*
================
idHeap::AllocatePage
allocates memory from the OS
bytes = page size in bytes
returns pointer to page
================
*/
idHeap::page_s* idHeap::AllocatePage( dword bytes ) {
idHeap::page_s* p;
pageRequests++;
if ( swapPage && swapPage->dataSize == bytes ) { // if we've got a swap page somewhere
p = swapPage;
swapPage = NULL;
}
else {
dword size;
size = bytes + sizeof(idHeap::page_s);
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
p = (idHeap::page_s *) ::local_malloc( size + ALIGN - 1 );
// RAVEN END
if ( !p ) {
if ( defragBlock ) {
idLib::common->Printf( "Freeing defragBlock on alloc of %i.\n", size + ALIGN - 1 );
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
local_free( defragBlock );
// RAVEN END
defragBlock = NULL;
// RAVEN BEGIN
// jnewquist: send all allocations through one place on the Xenon
p = (idHeap::page_s *) ::local_malloc( size + ALIGN - 1 );
// RAVEN END
AllocDefragBlock();
}
if ( !p ) {
common->FatalError( "malloc failure for %i", bytes );
}
}
p->data = (void *) ALIGN_SIZE( (int)((byte *)(p)) + sizeof( idHeap::page_s ) );
p->dataSize = size - sizeof(idHeap::page_s);
p->firstFree = NULL;
p->largestFree = 0;
OSAllocs++;
}
p->prev = NULL;
p->next = NULL;
pagesAllocated++;
return p;
}
/*
================
idHeap::FreePage
frees a page back to the operating system
p = pointer to page
================
*/
void idHeap::FreePage( idHeap::page_s *p ) {
assert( p );
if ( p->dataSize == pageSize && !swapPage ) { // add to swap list?
swapPage = p;
}
else {
FreePageReal( p );
}
pagesAllocated--;
}
//===============================================================
//
// small heap code
//
//===============================================================
/*
================
idHeap::SmallAllocate
allocate memory (1-255 bytes) from the small heap manager
bytes = number of bytes to allocate
returns pointer to allocated memory
================
*/
//RAVEN BEGIN
//amccarthy: Added allocation tag
void *idHeap::SmallAllocate( dword bytes, byte tag ) {
//RAVEN END
// we need the at least sizeof( dword ) bytes for the free list
if ( bytes < sizeof( dword ) ) {
bytes = sizeof( dword );
}
// increase the number of bytes if necessary to make sure the next small allocation is aligned
bytes = SMALL_ALIGN( bytes );
byte *smallBlock = (byte *)(smallFirstFree[bytes / ALIGN]);
if ( smallBlock ) {
dword *link = (dword *)(smallBlock + SMALL_HEADER_SIZE);
//RAVEN BEGIN
//amccarthy: Added allocation tag
#ifdef _DEBUG
OutstandingXMallocSize += smallBlock[0];
assert( tag > 0 && tag < MA_MAX);
OutstandingXMallocTagSize[tag] += smallBlock[0];
if (OutstandingXMallocTagSize[tag] > PeakXMallocTagSize[tag])
{
PeakXMallocTagSize[tag] = OutstandingXMallocTagSize[tag];
}
CurrNumAllocations[tag]++;
smallBlock[1] = tag;
smallBlock[2] = SMALL_ALLOC; // allocation identifier
#else
smallBlock[1] = SMALL_ALLOC; // allocation identifier
#endif
//RAVEN END
smallFirstFree[bytes / ALIGN] = (void *)(*link);
return (void *)(link);
}
dword bytesLeft = (long)(pageSize) - smallCurPageOffset;
// if we need to allocate a new page
if ( bytes >= bytesLeft ) {
smallCurPage->next = smallFirstUsedPage;
smallFirstUsedPage = smallCurPage;
smallCurPage = AllocatePage( pageSize );
if ( !smallCurPage ) {
return NULL;
}
// make sure the first allocation is aligned
smallCurPageOffset = SMALL_ALIGN( 0 );
}
smallBlock = ((byte *)smallCurPage->data) + smallCurPageOffset;
smallBlock[0] = (byte)(bytes / ALIGN); // write # of bytes/ALIGN
//RAVEN BEGIN
//amccarthy: Added allocation tag
#ifdef _DEBUG
OutstandingXMallocSize += smallBlock[0];
assert( tag > 0 && tag < MA_MAX);
OutstandingXMallocTagSize[tag] += smallBlock[0];
if (OutstandingXMallocTagSize[tag] > PeakXMallocTagSize[tag])
{
PeakXMallocTagSize[tag] = OutstandingXMallocTagSize[tag];
}
CurrNumAllocations[tag]++;
smallBlock[1] = tag;
smallBlock[2] = SMALL_ALLOC; // allocation identifier
#else
smallBlock[1] = SMALL_ALLOC; // allocation identifier
#endif
//RAVEN END
smallCurPageOffset += bytes + SMALL_HEADER_SIZE; // increase the offset on the current page
return ( smallBlock + SMALL_HEADER_SIZE ); // skip the first two bytes
}
/*
================
idHeap::SmallFree
frees a block of memory allocated by SmallAllocate() call
data = pointer to block of memory
================
*/
void idHeap::SmallFree( void *ptr ) {
((byte *)(ptr))[-1] = INVALID_ALLOC;
//RAVEN BEGIN
//amccarthy: allocation tracking
#ifdef _DEBUG
byte tag = ((byte *)(ptr))[-2];
byte size = ((byte *)(ptr))[-3];
OutstandingXMallocSize -= size;
assert( tag > 0 && tag < MA_MAX);
OutstandingXMallocTagSize[tag] -= size;
CurrNumAllocations[tag]--;
#endif
//RAVEN END
byte *d = ( (byte *)ptr ) - SMALL_HEADER_SIZE;
dword *dt = (dword *)ptr;
// index into the table with free small memory blocks
dword ix = *d;
// check if the index is correct
if ( ix > (256 / ALIGN) ) {
idLib::common->FatalError( "SmallFree: invalid memory block" );
}
*dt = (dword)smallFirstFree[ix]; // write next index
smallFirstFree[ix] = (void *)d; // link
}
//===============================================================
//
// medium heap code
//
// Medium-heap allocated pages not returned to OS until heap destructor
// called (re-used instead on subsequent medium-size malloc requests).
//
//===============================================================
/*
================
idHeap::MediumAllocateFromPage
performs allocation using the medium heap manager from a given page
p = page
sizeNeeded = # of bytes needed