summaryrefslogtreecommitdiffstats
path: root/game/code/memory/createheap.cpp
blob: 745bf3573975d6c60d9fdc0507f1184384988b2c (plain) (blame)
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
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        createheap.cpp
//
// Description: this set of functions creates heaps
//
// History:     2003/04/13 + Created -- Ian Gipson
//
//=============================================================================

//========================================
// System Includes
//========================================

//========================================
// Project Includes
//========================================
#include <memory/createheap.h>
#include <radmemorymonitor.hpp>

//*****************************************************************************
//
// Global Data, Local Data, Local Classes
//
//*****************************************************************************
IRadMemoryHeap* g_HeapArray[ NUM_GAME_MEMORY_ALLOCATORS ];

struct HeapCreationData
{
    HeapType            type;
    GameMemoryAllocator parent;
    char                name[ 256 ];
};

HeapCreationData g_HeapCreationData[] = 
{
    { HEAP_TYPE_DOUG_LEA, GMA_DEFAULT, "Default"              },      
    { HEAP_TYPE_DOUG_LEA, GMA_DEFAULT, "Temp"                 },      
    { HEAP_TYPE_NONE,     GMA_DEFAULT, "Gamecube VMM"         },      
#ifdef RAD_WIN32
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Persistent"           },  // no static heap for pc
#else
    { HEAP_TYPE_STATIC,   GMA_DEFAULT, "Persistent"           },
#endif // RAD_WIN32
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level"                },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level Movie"          },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level FE"             },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level Zone"           },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level Other"          },
    { HEAP_TYPE_DOUG_LEA, GMA_DEFAULT, "Level Hud"            },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level Mission"        },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Level Audio"          },
    { HEAP_TYPE_NONE,     GMA_DEFAULT, "Debug"                },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Special"              },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Music"                },
    { HEAP_TYPE_DOUG_LEA, GMA_DEFAULT, "Audio Persistent"     },
    { HEAP_TYPE_DOUG_LEA, GMA_DEFAULT, "Small Alloc"          },
#ifdef RAD_XBOX
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "XBOX Sound"           },
#endif
#ifdef USE_CHAR_GAG_HEAP
    { HEAP_TYPE_DOUG_LEA, GMA_DEFAULT, "Characters and Gags"  },
#endif
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Anywhere in Level"    },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Anywhere in FE"       },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Either Other or Zone" },
    { HEAP_TYPE_TRACKING, GMA_DEFAULT, "Search"               },
    { HEAP_TYPE_NONE,     GMA_DEFAULT, "???"                  },
    { HEAP_TYPE_NONE,     GMA_DEFAULT, "???"                  },
};

//*****************************************************************************
//
// Public Member Functions
//
//*****************************************************************************

//=============================================================================
// AllocatorType
//=============================================================================
//
// Description: which type of heap is this?
//
// Parameters:  allocator - which heap are we concerned with
//
// Return:      N/A.
//
//=============================================================================
HeapType AllocatorType( GameMemoryAllocator allocator )
{
    return g_HeapCreationData[ allocator ].type;
}

//=============================================================================
// CreateHeap
//=============================================================================
//
// Description: Creates the specified heap
//
// Parameters:  allocator - which heap are we creating
//
// Return:      N/A.
//
//=============================================================================
void CreateHeap ( GameMemoryAllocator allocator, const unsigned int size )
{
    unsigned int index = static_cast< unsigned int >( allocator );
    rAssert( g_HeapArray[ index ] == NULL );

    HeapType type    = g_HeapCreationData[ index ].type;
    const char* name = g_HeapCreationData[ index ].name;
    GameMemoryAllocator parent = g_HeapCreationData[ index ].parent;

    rReleasePrintf ("Creating Heap: %s (%d)\n", name, size );

    #ifdef RAD_RELEASE
    if( type == HEAP_TYPE_TRACKING )
    {
        type = HEAP_TYPE_NONE;
    }
    #endif

    switch( type )
    {
        case HEAP_TYPE_STATIC :
        {
            HeapMgr()->PushHeap( GMA_DEBUG );
            g_HeapArray[ index ] = radMemoryCreateStaticHeap( size, RADMEMORY_ALLOC_DEFAULT, name );
            g_HeapArray[ index ]->AddRef();
            HeapMgr()->PopHeap( GMA_DEBUG );
            break;
        }
        case HEAP_TYPE_TRACKING :
        {
            HeapMgr()->PushHeap( GMA_DEBUG );
            g_HeapArray[ index ] = radMemoryCreateTrackingHeap( size, RADMEMORY_ALLOC_DEFAULT, name );
            HeapMgr()->PopHeap( GMA_DEBUG );
            break;
        }
        case HEAP_TYPE_DOUG_LEA :
        {
            HeapMgr()->PushHeap( GMA_DEBUG );
            g_HeapArray[ index ] = radMemoryCreateDougLeaHeap( size, RADMEMORY_ALLOC_DEFAULT, name );
            g_HeapArray[ index ]->AddRef();
            HeapMgr()->PopHeap( GMA_DEBUG );
            break;
        }
        case HEAP_TYPE_NONE :
        {
            //rAssert( false );
            return;
        }
        default:
        {
            rAssert( false );
            return;
        }
    }
    radMemoryRegisterAllocator( allocator, parent,g_HeapArray[ index ] );
}

//=============================================================================
// DestroyHeap
//=============================================================================
//
// Description: Destroys the specified heap
//
// Parameters:  allocator - which heap are we destroying
//
// Return:      N/A.
//
//=============================================================================
void DestroyHeapA( GameMemoryAllocator allocator )
{
    unsigned int index = static_cast< unsigned int >( allocator );
    if( g_HeapArray[ index ] == NULL )
    {
        return;
    }

    // Check for heap not empty.
    // If it's not empty this is a Bad Thing.  We will be leaving dangling pointers to all the contained memory.
    // This either means we have a leak and they will never get deleted or they will get deleted eventually but
    // the heap will no longer exist.
    //
    // If you assert here, DO NOT IGNORE IT.  If you need help diagnosing the cause of the assertion, see Joel.
    //
    unsigned int numAllocs;
    unsigned int totalFree;
    g_HeapArray[ index ]->GetStatus ( &totalFree, 0, &numAllocs, 0);

    //
    // Suspend memory monitor etc if we've leaked
    //
    if( numAllocs > 0 )
    {
        #ifndef FINAL
            unsigned int size = g_HeapArray[ index ]->GetSize();
            char s[ 256 ];
            const char* name = g_HeapCreationData[ index ].name;
            unsigned int leakSize = size - totalFree;
            sprintf( s, "MEMORY LEAK: '%s' %d blocks %d bytes ", name, numAllocs, leakSize );
            rTunePrintf( s );
            g_HeapArray[index]->ValidateHeap();
        #endif

        // Destroy the heap
        //
        g_HeapArray[ index ]->Release ();
        g_HeapArray[ index ] = 0;
        ::radMemoryUnregisterAllocator( allocator );

        if( CommandLineOptions::Get( CLO_MEMORY_MONITOR ) )
        {
            ::radMemoryMonitorSuspend ();
        }
        #ifndef RAD_RELEASE
        else
        {
//                rAssertMsg (0, s);
        }
        #endif // RAD_RELEASE
    }
    else
    {
        // Destroy the heap
        //
        g_HeapArray[ index ]->Release ();
        g_HeapArray[ index ] = 0;
        ::radMemoryUnregisterAllocator( allocator );
    }
}

//=============================================================================
// GetHeap
//=============================================================================
//
// Description: allows access to the heaps
//
// Parameters:  allocator - which heap are we getting
//
// Return:      N/A.
//
//=============================================================================
IRadMemoryAllocator* GetAllocator( GameMemoryAllocator allocator )
{
    IRadMemoryAllocator* allocatorPtr = radMemoryGetAllocator( static_cast< int >( allocator ) );
    return allocatorPtr;
}

//=============================================================================
// GetHeapReference
//=============================================================================
//
// Description: allows access to the heaps in the array
//
// Parameters:  allocator - which heap are we getting
//
// Return:      N/A.
//
//=============================================================================
IRadMemoryHeap** GetHeapReference( GameMemoryAllocator allocator )
{
    return &( g_HeapArray[ allocator ] );
}

//=============================================================================
// GetTotalMemoryFreeInAllHeaps
//=============================================================================
//
// Description: tells us how much free space there is in all the heaps
//
// Parameters:  none
//
// Return:      total free space
//
//=============================================================================
size_t GetTotalMemoryFreeInAllHeaps()
{
    unsigned int totalFreeMemory = 0;
    unsigned int i;
    unsigned int size = NUM_GAME_MEMORY_ALLOCATORS;
    for( i = 0; i < size; ++i )
    {
        IRadMemoryHeap* heap = g_HeapArray[ i ];
        unsigned int free;
        unsigned int largestBlock;
        unsigned int numberOfObjects;
        unsigned int highWaterMark;
        if( heap != NULL )
        {
            heap->GetStatus( &free, &largestBlock, &numberOfObjects, &highWaterMark );
            totalFreeMemory += free;
        }
    }
    return totalFreeMemory;
}

//=============================================================================
// AllocatorType
//=============================================================================
//
// Description: figure out what heap this is
//
// Parameters:  none
//
// Return:      the index of the heap
//
//=============================================================================
GameMemoryAllocator WhichAllocator( const IRadMemoryAllocator* heap )
{
    int i;
    for( i = 0; i < NUM_GAME_MEMORY_ALLOCATORS; ++i )
    {
        if( g_HeapArray[ i ] == heap )
        {
            return static_cast< GameMemoryAllocator >( i );
        }
    }
    return NUM_GAME_MEMORY_ALLOCATORS;
}