summaryrefslogtreecommitdiffstats
path: root/game/code/memory/memorypool.cpp
blob: 72eb097ed7290ff6234967bbddb86861300645d8 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//==============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        memorypool.cpp
//
// Description: Implementation of the memory pool based on the one described
//              in "Effective C++".
//
// History:     + 2001/12/04 Created -- Darwin Chau
//              + 2002/06/19 Adapted form Simpsons2 -- Darwin Chau
//
//==============================================================================

//========================================
// System Includes
//========================================
// Foundation
#include <raddebug.hpp> 

//========================================
// Project Includes
//========================================
#include <memory/memorypool.h>

//******************************************************************************
//
// Global Data, Local Data, Local Classes
//
//******************************************************************************

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

//==============================================================================
// FBMemoryPool::FBMemoryPool
//==============================================================================
//
// Description: Constructor
//
// Parameters:  size
//                - size (in bytes) of the object being memory pooled
//
//              blockCapacity 
//                - controls the size of each memory block in the the pool by
//                  specifying the number of objects that will fit in each block
//                  (block size = size * blockCapacity)
//
//              allocator
//                - specifiy which heap to allocate memory from
//               
// Return:      N/A.      
//
//==============================================================================
FBMemoryPool::FBMemoryPool
(
    size_t size,
    int blockCapacity,
    GameMemoryAllocator allocator
)
:
    mSize( size ),  
    mBlockCapacity( blockCapacity ),
    mGMAllocator( allocator ),
    mpHeadOfFreeList( NULL ),
    mCurrentAllocs( 0 ),
    mCurrentBlock( 0 )
{
    int i;
    for( i = 0; i < MAX_BLOCKS; ++i )
    {
        mpBlockArray[mCurrentBlock] = NULL;
    }

#ifndef RAD_RELEASE
    mTotalAllocs = 0;
    mTotalFrees = 0;
    mPeakAllocs = 0;
#endif // !RAD_RELEASE
}


//==============================================================================
// FBMemoryPool::~FBMemoryPool
//==============================================================================
//
// Description: Destructor
//
// Parameters:  None.
//
// Return:      N/A.
//
//==============================================================================
FBMemoryPool::~FBMemoryPool()
{
    rAssertMsg( mCurrentAllocs == 0, "*** MEMORY LEAK ! ***\n" );
}


//==============================================================================
// FBMemoryPool::Allocate
//==============================================================================
//
// Description: Allocate memory from the pool.  The pool allocates memory in
//              large blocks which it then parcels out for subsequent allocation
//              requests.  All allocations from the pool must be the same size.
//
// Constraints: The size of the memory pool is aritificially capped by the
//              max number of blocks that can be allocated.  This was done
//              to avoid managing the memory required by an STL container class.
//
// Parameters:  size - amount of memory requested; this is only used to verify
//                     the size is the same as what the pool is configured for
//
// Return:      pointer to allocated memory on success
//              NULL on failure
//
//==============================================================================
void* FBMemoryPool::Allocate( size_t size )
{
    rAssert( size <= mSize );

    void* pMemory = NULL;
    
    //
    // Allocate memory from our pool.
    //
    if( mpHeadOfFreeList != NULL )
    {
        //
        // Allocate from the free list.
        //
        pMemory = static_cast<void*>( mpHeadOfFreeList );
        mpHeadOfFreeList = mpHeadOfFreeList->mpNext;
    }
    else
    {
        //
        // Need to allocate a new block of memory.
        //
        if( mCurrentBlock == MAX_BLOCKS )
        {
            //
            // Array to store blocks is full. 
            //
            rTuneString( "FBMemoryPool - Max blocks exceeded, tune pool config.\n");
            return( NULL );
        }

        void* pNewBlock = ::operator new( (mBlockCapacity * mSize), mGMAllocator );
        rAssert( pNewBlock != 0 );

        //
        // Store the pointer so we can delete the block later.
        //
        mpBlockArray[mCurrentBlock] = pNewBlock;
        ++mCurrentBlock;


        //
        // Make a linked list out of the block.
        //
        for( int i = 0; i < mBlockCapacity - 1; ++i )
        {
            char* pCurrent = static_cast<char*>(pNewBlock) + (i * mSize);
            MemoryPoolList* pList = reinterpret_cast<MemoryPoolList*>( pCurrent );
            char* pNext = static_cast<char*>(pNewBlock) + ( (i + 1) * mSize);
            pList->mpNext = reinterpret_cast<MemoryPoolList*>( pNext );
        }
        
        //
        // Mark the end of the list with a NULL.
        //
        char* pLast = static_cast<char*>(pNewBlock) + ( (mBlockCapacity - 1) * mSize);
        MemoryPoolList* pListEnd = reinterpret_cast<MemoryPoolList*>( pLast );
        pListEnd->mpNext = NULL;

        //
        // Return this piece of memory.
        //
        pMemory = pNewBlock;
        
        //
        // This is the new head of the free list.
        //
        mpHeadOfFreeList = reinterpret_cast<MemoryPoolList*>( pNewBlock )->mpNext;
    }

    ++mCurrentAllocs;


#ifndef RAD_RELEASE

    ++mTotalAllocs;
    
    if( mTotalAllocs > mPeakAllocs )
    {
        mPeakAllocs = mTotalAllocs;
    }

#endif // !RAD_RELEASE
    
    return( pMemory );
}


//==============================================================================
// FBMemoryPool::Free
//==============================================================================
//
// Description: Return memory to the pool.  When all of the memory has been
//              returned to the pool, all of the memory blocks are released.
//
// Parameters:  mem - pointer to memory being returned to the pool
//              size - size of memory being returned.
//
// Return:      None.
//
//==============================================================================
void FBMemoryPool::Free( void* mem, size_t size )
{
    //
    // Calling delete on a NULL pointer is "legal" so better handle it.
    //
    if( mem == NULL )
    {
        return;
    }

    rAssert( size <= mSize );

    //
    // Return the memory to the free list.
    //
    MemoryPoolList* pCarcass = static_cast<MemoryPoolList*>( mem );

    pCarcass->mpNext = mpHeadOfFreeList;
    mpHeadOfFreeList = pCarcass;

    --mCurrentAllocs;

    //
    // If there are no outstanding alloctions, free all the memory blocks.
    //
    if( mCurrentAllocs == 0 )
    {
        int i;
        for( i = 0; i < MAX_BLOCKS; ++i )
        {
            if( mpBlockArray[i] != NULL )
            {
                ::operator delete( mpBlockArray[i], mGMAllocator );
                mpBlockArray[i] = NULL;
            }
        }

        mpHeadOfFreeList = NULL;
        mCurrentBlock = 0;
    }

#ifndef RAD_RELEASE

    ++mTotalFrees;
    
    if( mCurrentAllocs == 0 )
    {
        this->DumpStats();
    }

#endif // !RAD_RELEASE
}



//==============================================================================
// FBMemoryPool::ChangeAllocator
//==============================================================================
//
// Description: Can only switch heaps if there are no outstanding allocations.
//
// Parameters:  allocator - switch to allocating from this heap
//
// Return:      true if successful
//              false otherwise
//
//==============================================================================
bool FBMemoryPool::ChangeAllocator( GameMemoryAllocator allocator )
{
    if( mCurrentAllocs == 0 )
    {
        mGMAllocator = allocator;
        return( true );
    }
    else
    {
        rAssertMsg( false, "Failed to change allocators" );
        return( false );
    }
}

bool FBMemoryPool::OneOfOurs(void* mem)
{
    for( int i = 0; i < MAX_BLOCKS; ++i )
    {
        if( mpBlockArray[i] != NULL )
        {
            if((mem >= mpBlockArray[i]) && (mem < (((char*)mpBlockArray[i]) + (mBlockCapacity * mSize))))
            {
                return true;
            }
        }
    }
    return false;
}


//==============================================================================
// FBMemoryPool::DumpStats
//==============================================================================
//
// Description: Debug spew.
//
// Parameters:  None.
//
// Return:      None.
//
//==============================================================================
void FBMemoryPool::DumpStats()
{
    rTunePrintf( "FBMemoryPool Stats: (Element Size: %d ; Block Capacity: %d)\n",
                 mSize, mBlockCapacity );
    rTunePrintf( "\tCurrent Allocs: %d\n", mCurrentAllocs );
#ifndef RAD_RELEASE
    rTunePrintf( "\tTotal Allocs: %d\n", mTotalAllocs );
    rTunePrintf( "\tTotal Frees: %d\n", mTotalFrees );
    rTunePrintf( "\tPeak Allocs: %d\n", mPeakAllocs );
#endif // !RAD_RELEASE
}