2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __HEAP_H__
|
|
|
|
#define __HEAP_H__
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
Memory Management
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// memory tag names are used to sort allocations for sys_dumpMemory and other reporting functions
|
2012-11-28 15:47:07 +00:00
|
|
|
enum memTag_t
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
#define MEM_TAG( x ) TAG_##x,
|
|
|
|
#include "sys/sys_alloc_tags.h"
|
|
|
|
TAG_NUM_TAGS,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int MAX_TAGS = 256;
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-17 11:24:31 +00:00
|
|
|
// RB: 64 bit fixes, changed int to size_t
|
|
|
|
void* Mem_Alloc16( const size_t size, const memTag_t tag );
|
2012-11-28 15:47:07 +00:00
|
|
|
void Mem_Free16( void* ptr );
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-12-17 11:24:31 +00:00
|
|
|
ID_INLINE void* Mem_Alloc( const size_t size, const memTag_t tag )
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
|
|
|
return Mem_Alloc16( size, tag );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void Mem_Free( void* ptr )
|
|
|
|
{
|
|
|
|
Mem_Free16( ptr );
|
|
|
|
}
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-12-17 11:24:31 +00:00
|
|
|
void* Mem_ClearedAlloc( const size_t size, const memTag_t tag );
|
2012-11-28 15:47:07 +00:00
|
|
|
char* Mem_CopyString( const char* in );
|
2012-12-17 11:24:31 +00:00
|
|
|
// RB end
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-12-24 11:34:18 +00:00
|
|
|
ID_INLINE void* operator new( size_t s )
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
throw( std::bad_alloc ) // DG: standard signature seems to include throw(..)
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Mem_Alloc( s, TAG_NEW );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
|
|
|
ID_INLINE void operator delete( void* p )
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
throw() // DG: delete musn't throw
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( p );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
ID_INLINE void* operator new[]( size_t s )
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
throw( std::bad_alloc ) // DG: standard signature seems to include throw(..)
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Mem_Alloc( s, TAG_NEW );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
|
|
|
ID_INLINE void operator delete[]( void* p )
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
throw() // DG: delete musn't throw
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( p );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void* operator new( size_t s, memTag_t tag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Mem_Alloc( s, tag );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
|
|
|
ID_INLINE void operator delete( void* p, memTag_t tag )
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
throw() // DG: delete musn't throw
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( p );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void* operator new[]( size_t s, memTag_t tag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Mem_Alloc( s, tag );
|
|
|
|
}
|
2012-12-24 11:34:18 +00:00
|
|
|
|
2012-12-22 05:15:50 +00:00
|
|
|
ID_INLINE void operator delete[]( void* p, memTag_t tag ) throw() // DG: delete musn't throw
|
2012-11-28 15:47:07 +00:00
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define replacements for the PS3 library's aligned new operator.
|
|
|
|
// Without these, allocations of objects with 32 byte or greater alignment
|
|
|
|
// may not go through our memory system.
|
|
|
|
|
|
|
|
/*
|
|
|
|
================================================
|
|
|
|
idTempArray is an array that is automatically free'd when it goes out of scope.
|
|
|
|
There is no "cast" operator because these are very unsafe.
|
|
|
|
|
|
|
|
The template parameter MUST BE POD!
|
|
|
|
|
|
|
|
Compile time asserting POD-ness of the template parameter is complicated due
|
|
|
|
to our vector classes that need a default constructor but are otherwise
|
|
|
|
considered POD.
|
|
|
|
================================================
|
|
|
|
*/
|
|
|
|
template < class T >
|
2012-11-28 15:47:07 +00:00
|
|
|
class idTempArray
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
2012-11-28 15:47:07 +00:00
|
|
|
idTempArray( idTempArray<T>& other );
|
2012-11-26 18:58:24 +00:00
|
|
|
idTempArray( unsigned int num );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
~idTempArray();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
T& operator []( unsigned int i )
|
|
|
|
{
|
|
|
|
assert( i < num );
|
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
const T& operator []( unsigned int i ) const
|
|
|
|
{
|
|
|
|
assert( i < num );
|
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
T* Ptr()
|
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
const T* Ptr() const
|
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Size( ) const
|
|
|
|
{
|
|
|
|
return num * sizeof( T );
|
|
|
|
}
|
|
|
|
unsigned int Num( ) const
|
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Zero()
|
|
|
|
{
|
|
|
|
memset( Ptr(), 0, Size() );
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
private:
|
2012-11-28 15:47:07 +00:00
|
|
|
T* buffer; // Ensure this buffer comes first, so this == &this->buffer
|
2012-11-26 18:58:24 +00:00
|
|
|
unsigned int num;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idTempArray::idTempArray
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template < class T >
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idTempArray<T>::idTempArray( idTempArray<T>& other )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
this->num = other.num;
|
|
|
|
this->buffer = other.buffer;
|
|
|
|
other.num = 0;
|
|
|
|
other.buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idTempArray::idTempArray
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template < class T >
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idTempArray<T>::idTempArray( unsigned int num )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
this->num = num;
|
2012-11-28 15:47:07 +00:00
|
|
|
buffer = ( T* )Mem_Alloc( num * sizeof( T ), TAG_TEMP );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idTempArray::~idTempArray
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template < class T >
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idTempArray<T>::~idTempArray()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============================================================================
|
|
|
|
|
|
|
|
Block based allocator for fixed size objects.
|
|
|
|
|
|
|
|
All objects of the 'type' are properly constructed and destructed when reused.
|
|
|
|
|
|
|
|
===============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BLOCK_ALLOC_ALIGNMENT 16
|
|
|
|
|
|
|
|
// Define this to force all block allocators to act like normal new/delete allocation
|
|
|
|
// for tool checking.
|
|
|
|
//#define FORCE_DISCRETE_BLOCK_ALLOCS
|
|
|
|
|
|
|
|
/*
|
|
|
|
================================================
|
|
|
|
idBlockAlloc is a block-based allocator for fixed-size objects.
|
|
|
|
|
|
|
|
All objects are properly constructed and destructed.
|
|
|
|
================================================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag = TAG_BLOCKALLOC>
|
2012-11-28 15:47:07 +00:00
|
|
|
class idBlockAlloc
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
|
|
|
ID_INLINE idBlockAlloc( bool clear = false );
|
|
|
|
ID_INLINE ~idBlockAlloc();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// returns total size of allocated memory
|
2012-11-28 15:47:07 +00:00
|
|
|
size_t Allocated() const
|
|
|
|
{
|
|
|
|
return total * sizeof( _type_ );
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// returns total size of allocated memory including size of (*this)
|
2012-11-28 15:47:07 +00:00
|
|
|
size_t Size() const
|
|
|
|
{
|
|
|
|
return sizeof( *this ) + Allocated();
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
ID_INLINE void Shutdown();
|
|
|
|
ID_INLINE void SetFixedBlocks( int numBlocks );
|
|
|
|
ID_INLINE void FreeEmptyBlocks();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
ID_INLINE _type_* Alloc();
|
2012-11-26 18:58:24 +00:00
|
|
|
ID_INLINE void Free( _type_ *element );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
int GetTotalCount() const
|
|
|
|
{
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
int GetAllocCount() const
|
|
|
|
{
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
int GetFreeCount() const
|
|
|
|
{
|
|
|
|
return total - active;
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
private:
|
2012-11-28 15:47:07 +00:00
|
|
|
union element_t
|
|
|
|
{
|
|
|
|
_type_* data; // this is a hack to make sure the save game system marks _type_ as saveable
|
|
|
|
element_t* next;
|
|
|
|
byte buffer[( CONST_MAX( sizeof( _type_ ), sizeof( element_t* ) ) + ( BLOCK_ALLOC_ALIGNMENT - 1 ) ) & ~( BLOCK_ALLOC_ALIGNMENT - 1 )];
|
2012-11-26 18:58:24 +00:00
|
|
|
};
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
class idBlock
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
|
|
|
element_t elements[_blockSize_];
|
2012-11-28 15:47:07 +00:00
|
|
|
idBlock* next;
|
|
|
|
element_t* free; // list with free elements in this block (temp used only by FreeEmptyBlocks)
|
2012-11-26 18:58:24 +00:00
|
|
|
int freeCount; // number of free elements in this block (temp used only by FreeEmptyBlocks)
|
|
|
|
};
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
idBlock* blocks;
|
|
|
|
element_t* free;
|
2012-11-26 18:58:24 +00:00
|
|
|
int total;
|
|
|
|
int active;
|
|
|
|
bool allowAllocs;
|
|
|
|
bool clearAllocs;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
ID_INLINE void AllocNewBlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::idBlockAlloc
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idBlockAlloc<_type_, _blockSize_, memTag>::idBlockAlloc( bool clear ) :
|
2012-11-26 18:58:24 +00:00
|
|
|
blocks( NULL ),
|
|
|
|
free( NULL ),
|
|
|
|
total( 0 ),
|
|
|
|
active( 0 ),
|
|
|
|
allowAllocs( true ),
|
|
|
|
clearAllocs( clear )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize__,align_t>::~idBlockAlloc
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE idBlockAlloc<_type_, _blockSize_, memTag>::~idBlockAlloc()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::Alloc
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE _type_* idBlockAlloc<_type_, _blockSize_, memTag>::Alloc()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef FORCE_DISCRETE_BLOCK_ALLOCS
|
|
|
|
// for debugging tools
|
|
|
|
return new _type_;
|
|
|
|
#else
|
2012-11-28 15:47:07 +00:00
|
|
|
if( free == NULL )
|
|
|
|
{
|
|
|
|
if( !allowAllocs )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
AllocNewBlock();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
active++;
|
2012-11-28 15:47:07 +00:00
|
|
|
element_t* element = free;
|
2012-11-26 18:58:24 +00:00
|
|
|
free = free->next;
|
|
|
|
element->next = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
_type_ * t = ( _type_* ) element->buffer;
|
|
|
|
if( clearAllocs )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
memset( t, 0, sizeof( _type_ ) );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
new( t ) _type_;
|
2012-11-26 18:58:24 +00:00
|
|
|
return t;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::Free
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idBlockAlloc<_type_, _blockSize_, memTag>::Free( _type_ * t )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef FORCE_DISCRETE_BLOCK_ALLOCS
|
|
|
|
// for debugging tools
|
|
|
|
delete t;
|
|
|
|
#else
|
2012-11-28 15:47:07 +00:00
|
|
|
if( t == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
t->~_type_();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
element_t* element = ( element_t* )( t );
|
2012-11-26 18:58:24 +00:00
|
|
|
element->next = free;
|
|
|
|
free = element;
|
|
|
|
active--;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::Shutdown
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idBlockAlloc<_type_, _blockSize_, memTag>::Shutdown()
|
|
|
|
{
|
|
|
|
while( blocks != NULL )
|
|
|
|
{
|
|
|
|
idBlock* block = blocks;
|
2012-11-26 18:58:24 +00:00
|
|
|
blocks = blocks->next;
|
|
|
|
Mem_Free( block );
|
|
|
|
}
|
|
|
|
blocks = NULL;
|
|
|
|
free = NULL;
|
|
|
|
total = active = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::SetFixedBlocks
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idBlockAlloc<_type_, _blockSize_, memTag>::SetFixedBlocks( int numBlocks )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int currentNumBlocks = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( idBlock* block = blocks; block != NULL; block = block->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
currentNumBlocks++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = currentNumBlocks; i < numBlocks; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
AllocNewBlock();
|
|
|
|
}
|
|
|
|
allowAllocs = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::AllocNewBlock
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idBlockAlloc<_type_, _blockSize_, memTag>::AllocNewBlock()
|
|
|
|
{
|
|
|
|
idBlock* block = ( idBlock* )Mem_Alloc( sizeof( idBlock ), memTag );
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next = blocks;
|
|
|
|
blocks = block;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < _blockSize_; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block->elements[i].next = free;
|
|
|
|
free = &block->elements[i];
|
2012-12-08 17:20:13 +00:00
|
|
|
|
2012-12-06 00:00:05 +00:00
|
|
|
// RB: changed UINT_PTR to uintptr_t
|
|
|
|
assert( ( ( ( uintptr_t )free ) & ( BLOCK_ALLOC_ALIGNMENT - 1 ) ) == 0 );
|
|
|
|
// RB end
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
total += _blockSize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idBlockAlloc<_type_,_blockSize_,align_t>::FreeEmptyBlocks
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
template<class _type_, int _blockSize_, memTag_t memTag>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idBlockAlloc<_type_, _blockSize_, memTag>::FreeEmptyBlocks()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// first count how many free elements are in each block
|
|
|
|
// and build up a free chain per block
|
2012-11-28 15:47:07 +00:00
|
|
|
for( idBlock* block = blocks; block != NULL; block = block->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block->free = NULL;
|
|
|
|
block->freeCount = 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( element_t* element = free; element != NULL; )
|
|
|
|
{
|
|
|
|
element_t* next = element->next;
|
|
|
|
for( idBlock* block = blocks; block != NULL; block = block->next )
|
|
|
|
{
|
|
|
|
if( element >= block->elements && element < block->elements + _blockSize_ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
element->next = block->free;
|
|
|
|
block->free = element;
|
|
|
|
block->freeCount++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if this assert fires, we couldn't find the element in any block
|
|
|
|
assert( element->next != next );
|
|
|
|
element = next;
|
|
|
|
}
|
|
|
|
// now free all blocks whose free count == _blockSize_
|
2012-11-28 15:47:07 +00:00
|
|
|
idBlock* prevBlock = NULL;
|
|
|
|
for( idBlock* block = blocks; block != NULL; )
|
|
|
|
{
|
|
|
|
idBlock* next = block->next;
|
|
|
|
if( block->freeCount == _blockSize_ )
|
|
|
|
{
|
|
|
|
if( prevBlock == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( blocks == block );
|
|
|
|
blocks = block->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( prevBlock->next == block );
|
|
|
|
prevBlock->next = block->next;
|
|
|
|
}
|
|
|
|
Mem_Free( block );
|
|
|
|
total -= _blockSize_;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
prevBlock = block;
|
|
|
|
}
|
|
|
|
block = next;
|
|
|
|
}
|
|
|
|
// now rebuild the free chain
|
|
|
|
free = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( idBlock* block = blocks; block != NULL; block = block->next )
|
|
|
|
{
|
|
|
|
for( element_t* element = block->free; element != NULL; )
|
|
|
|
{
|
|
|
|
element_t* next = element->next;
|
2012-11-26 18:58:24 +00:00
|
|
|
element->next = free;
|
|
|
|
free = element;
|
|
|
|
element = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============================================================================
|
|
|
|
|
|
|
|
Dynamic allocator, simple wrapper for normal allocations which can
|
|
|
|
be interchanged with idDynamicBlockAlloc.
|
|
|
|
|
|
|
|
No constructor is called for the 'type'.
|
|
|
|
Allocated blocks are always 16 byte aligned.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
class idDynamicAlloc
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicAlloc();
|
|
|
|
~idDynamicAlloc();
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
|
|
|
void SetFixedBlocks( int numBlocks ) {}
|
|
|
|
void SetLockMemory( bool lock ) {}
|
|
|
|
void FreeEmptyBaseBlocks() {}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
type* Alloc( const int num );
|
|
|
|
type* Resize( type* ptr, const int num );
|
|
|
|
void Free( type* ptr );
|
|
|
|
const char* CheckMemory( const type* ptr ) const;
|
|
|
|
|
|
|
|
int GetNumBaseBlocks() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int GetBaseBlockMemory() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int GetNumUsedBlocks() const
|
|
|
|
{
|
|
|
|
return numUsedBlocks;
|
|
|
|
}
|
|
|
|
int GetUsedBlockMemory() const
|
|
|
|
{
|
|
|
|
return usedBlockMemory;
|
|
|
|
}
|
|
|
|
int GetNumFreeBlocks() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int GetFreeBlockMemory() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int GetNumEmptyBaseBlocks() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
private:
|
|
|
|
int numUsedBlocks; // number of used blocks
|
|
|
|
int usedBlockMemory; // total memory in used blocks
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numAllocs;
|
|
|
|
int numResizes;
|
|
|
|
int numFrees;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicAlloc<type, baseBlockSize, minBlockSize>::idDynamicAlloc()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicAlloc<type, baseBlockSize, minBlockSize>::~idDynamicAlloc()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicAlloc<type, baseBlockSize, minBlockSize>::Init()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicAlloc<type, baseBlockSize, minBlockSize>::Shutdown()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
type* idDynamicAlloc<type, baseBlockSize, minBlockSize>::Alloc( const int num )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
numAllocs++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( num <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
numUsedBlocks++;
|
|
|
|
usedBlockMemory += num * sizeof( type );
|
|
|
|
return Mem_Alloc16( num * sizeof( type ), TAG_BLOCKALLOC );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
type* idDynamicAlloc<type, baseBlockSize, minBlockSize>::Resize( type* ptr, const int num )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
numResizes++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ptr == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Alloc( num );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( num <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Free( ptr );
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( 0 );
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicAlloc<type, baseBlockSize, minBlockSize>::Free( type* ptr )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
numFrees++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ptr == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Mem_Free16( ptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idDynamicAlloc<type, baseBlockSize, minBlockSize>::CheckMemory( const type* ptr ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicAlloc<type, baseBlockSize, minBlockSize>::Clear()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
numUsedBlocks = 0;
|
|
|
|
usedBlockMemory = 0;
|
|
|
|
numAllocs = 0;
|
|
|
|
numResizes = 0;
|
|
|
|
numFrees = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============================================================================
|
|
|
|
|
|
|
|
Fast dynamic block allocator.
|
|
|
|
|
|
|
|
No constructor is called for the 'type'.
|
|
|
|
Allocated blocks are always 16 byte aligned.
|
|
|
|
|
|
|
|
==============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "containers/BTree.h"
|
|
|
|
|
|
|
|
//#define DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
|
|
|
|
template<class type>
|
2012-11-28 15:47:07 +00:00
|
|
|
class idDynamicBlock
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
2012-11-28 15:47:07 +00:00
|
|
|
type* GetMemory() const
|
|
|
|
{
|
|
|
|
return ( type* )( ( ( byte* ) this ) + sizeof( idDynamicBlock<type> ) );
|
|
|
|
}
|
|
|
|
int GetSize() const
|
|
|
|
{
|
|
|
|
return abs( size );
|
|
|
|
}
|
|
|
|
void SetSize( int s, bool isBaseBlock )
|
|
|
|
{
|
|
|
|
size = isBaseBlock ? -s : s;
|
|
|
|
}
|
|
|
|
bool IsBaseBlock() const
|
|
|
|
{
|
|
|
|
return ( size < 0 );
|
|
|
|
}
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
int id[3];
|
2012-11-28 15:47:07 +00:00
|
|
|
void* allocator;
|
2012-11-26 18:58:24 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int size; // size in bytes of the block
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* prev; // previous memory block
|
|
|
|
idDynamicBlock<type>* next; // next memory block
|
|
|
|
idBTreeNode<idDynamicBlock<type>, int>* node; // node in the B-Tree with free blocks
|
2012-11-26 18:58:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_ = TAG_BLOCKALLOC>
|
2012-11-28 15:47:07 +00:00
|
|
|
class idDynamicBlockAlloc
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
public:
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlockAlloc();
|
|
|
|
~idDynamicBlockAlloc();
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
|
|
|
void SetFixedBlocks( int numBlocks );
|
|
|
|
void SetLockMemory( bool lock );
|
|
|
|
void FreeEmptyBaseBlocks();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
type* Alloc( const int num );
|
|
|
|
type* Resize( type* ptr, const int num );
|
|
|
|
void Free( type* ptr );
|
|
|
|
const char* CheckMemory( const type* ptr ) const;
|
|
|
|
|
|
|
|
int GetNumBaseBlocks() const
|
|
|
|
{
|
|
|
|
return numBaseBlocks;
|
|
|
|
}
|
|
|
|
int GetBaseBlockMemory() const
|
|
|
|
{
|
|
|
|
return baseBlockMemory;
|
|
|
|
}
|
|
|
|
int GetNumUsedBlocks() const
|
|
|
|
{
|
|
|
|
return numUsedBlocks;
|
|
|
|
}
|
|
|
|
int GetUsedBlockMemory() const
|
|
|
|
{
|
|
|
|
return usedBlockMemory;
|
|
|
|
}
|
|
|
|
int GetNumFreeBlocks() const
|
|
|
|
{
|
|
|
|
return numFreeBlocks;
|
|
|
|
}
|
|
|
|
int GetFreeBlockMemory() const
|
|
|
|
{
|
|
|
|
return freeBlockMemory;
|
|
|
|
}
|
2012-11-26 18:58:24 +00:00
|
|
|
int GetNumEmptyBaseBlocks() const;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
private:
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* firstBlock; // first block in list in order of increasing address
|
|
|
|
idDynamicBlock<type>* lastBlock; // last block in list in order of increasing address
|
|
|
|
idBTree<idDynamicBlock<type>, int, 4>freeTree; // B-Tree with free memory blocks
|
2012-11-26 18:58:24 +00:00
|
|
|
bool allowAllocs; // allow base block allocations
|
|
|
|
bool lockMemory; // lock memory so it cannot get swapped out
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
int blockId[3];
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numBaseBlocks; // number of base blocks
|
|
|
|
int baseBlockMemory; // total memory in base blocks
|
|
|
|
int numUsedBlocks; // number of used blocks
|
|
|
|
int usedBlockMemory; // total memory in used blocks
|
|
|
|
int numFreeBlocks; // number of free blocks
|
|
|
|
int freeBlockMemory; // total memory in free blocks
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
int numAllocs;
|
|
|
|
int numResizes;
|
|
|
|
int numFrees;
|
|
|
|
|
|
|
|
memTag_t tag;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
void Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* AllocInternal( const int num );
|
|
|
|
idDynamicBlock<type>* ResizeInternal( idDynamicBlock<type>* block, const int num );
|
|
|
|
void FreeInternal( idDynamicBlock<type>* block );
|
|
|
|
void LinkFreeInternal( idDynamicBlock<type>* block );
|
|
|
|
void UnlinkFreeInternal( idDynamicBlock<type>* block );
|
2012-11-26 18:58:24 +00:00
|
|
|
void CheckMemory() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::idDynamicBlockAlloc()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tag = _tag_;
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::~idDynamicBlockAlloc()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::Init()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
freeTree.Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::Shutdown()
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block;
|
|
|
|
|
|
|
|
for( block = firstBlock; block != NULL; block = block->next )
|
|
|
|
{
|
|
|
|
if( block->node == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeInternal( block );
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( block = firstBlock; block != NULL; block = firstBlock )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
firstBlock = block->next;
|
|
|
|
assert( block->IsBaseBlock() );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lockMemory )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//idLib::sys->UnlockMemory( block, block->GetSize() + (int)sizeof( idDynamicBlock<type> ) );
|
|
|
|
}
|
|
|
|
Mem_Free16( block );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
freeTree.Shutdown();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::SetFixedBlocks( int numBlocks )
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block;
|
|
|
|
|
|
|
|
for( int i = numBaseBlocks; i < numBlocks; i++ )
|
|
|
|
{
|
|
|
|
block = ( idDynamicBlock<type>* ) Mem_Alloc16( baseBlockSize, _tag_ );
|
|
|
|
if( lockMemory )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//idLib::sys->LockMemory( block, baseBlockSize );
|
|
|
|
}
|
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
memcpy( block->id, blockId, sizeof( block->id ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
block->allocator = ( void* )this;
|
2012-11-26 18:58:24 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
block->SetSize( baseBlockSize - ( int )sizeof( idDynamicBlock<type> ), true );
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next = NULL;
|
|
|
|
block->prev = lastBlock;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lastBlock )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock->next = block;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
firstBlock = block;
|
|
|
|
}
|
|
|
|
lastBlock = block;
|
|
|
|
block->node = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeInternal( block );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numBaseBlocks++;
|
|
|
|
baseBlockMemory += baseBlockSize;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
allowAllocs = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::SetLockMemory( bool lock )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lockMemory = lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::FreeEmptyBaseBlocks()
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block, *next;
|
|
|
|
|
|
|
|
for( block = firstBlock; block != NULL; block = next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
next = block->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( block->IsBaseBlock() && block->node != NULL && ( next == NULL || next->IsBaseBlock() ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnlinkFreeInternal( block );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->prev )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block->prev->next = block->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
firstBlock = block->next;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next->prev = block->prev;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock = block->prev;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lockMemory )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//idLib::sys->UnlockMemory( block, block->GetSize() + (int)sizeof( idDynamicBlock<type> ) );
|
|
|
|
}
|
|
|
|
numBaseBlocks--;
|
2012-11-28 15:47:07 +00:00
|
|
|
baseBlockMemory -= block->GetSize() + ( int )sizeof( idDynamicBlock<type> );
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free16( block );
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
CheckMemory();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
int idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::GetNumEmptyBaseBlocks() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int numEmptyBaseBlocks;
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* block;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numEmptyBaseBlocks = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( block = firstBlock; block != NULL; block = block->next )
|
|
|
|
{
|
|
|
|
if( block->IsBaseBlock() && block->node != NULL && ( block->next == NULL || block->next->IsBaseBlock() ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
numEmptyBaseBlocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numEmptyBaseBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
type* idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::Alloc( const int num )
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numAllocs++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( num <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
block = AllocInternal( num );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
block = ResizeInternal( block, num );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
CheckMemory();
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numUsedBlocks++;
|
|
|
|
usedBlockMemory += block->GetSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return block->GetMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
type* idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::Resize( type* ptr, const int num )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
numResizes++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ptr == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return Alloc( num );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( num <= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Free( ptr );
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
idDynamicBlock<type>* block = ( idDynamicBlock<type>* )( ( ( byte* ) ptr ) - ( int )sizeof( idDynamicBlock<type> ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
usedBlockMemory -= block->GetSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
block = ResizeInternal( block, num );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
CheckMemory();
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
usedBlockMemory += block->GetSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return block->GetMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::Free( type* ptr )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
numFrees++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( ptr == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
idDynamicBlock<type>* block = ( idDynamicBlock<type>* )( ( ( byte* ) ptr ) - ( int )sizeof( idDynamicBlock<type> ) );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numUsedBlocks--;
|
|
|
|
usedBlockMemory -= block->GetSize();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeInternal( block );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
CheckMemory();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::CheckMemory( const type* ptr ) const
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block;
|
|
|
|
|
|
|
|
if( ptr == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
block = ( idDynamicBlock<type>* )( ( ( byte* ) ptr ) - ( int )sizeof( idDynamicBlock<type> ) );
|
|
|
|
|
|
|
|
if( block->node != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return "memory has been freed";
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->id[0] != 0x11111111 || block->id[1] != 0x22222222 || block->id[2] != 0x33333333 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return "memory has invalid id";
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->allocator != ( void* )this )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return "memory was allocated with different allocator";
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
/* base blocks can be larger than baseBlockSize which can cause this code to fail
|
|
|
|
idDynamicBlock<type> *base;
|
|
|
|
for ( base = firstBlock; base != NULL; base = base->next ) {
|
|
|
|
if ( base->IsBaseBlock() ) {
|
|
|
|
if ( ((int)block) >= ((int)base) && ((int)block) < ((int)base) + baseBlockSize ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( base == NULL ) {
|
|
|
|
return "no base block found for memory";
|
|
|
|
}
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::Clear()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
firstBlock = lastBlock = NULL;
|
|
|
|
allowAllocs = true;
|
|
|
|
lockMemory = false;
|
|
|
|
numBaseBlocks = 0;
|
|
|
|
baseBlockMemory = 0;
|
|
|
|
numUsedBlocks = 0;
|
|
|
|
usedBlockMemory = 0;
|
|
|
|
numFreeBlocks = 0;
|
|
|
|
freeBlockMemory = 0;
|
|
|
|
numAllocs = 0;
|
|
|
|
numResizes = 0;
|
|
|
|
numFrees = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
blockId[0] = 0x11111111;
|
|
|
|
blockId[1] = 0x22222222;
|
|
|
|
blockId[2] = 0x33333333;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::AllocInternal( const int num )
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block;
|
2012-11-26 18:58:24 +00:00
|
|
|
int alignedBytes = ( num * sizeof( type ) + 15 ) & ~15;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
block = freeTree.FindSmallestLargerEqual( alignedBytes );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnlinkFreeInternal( block );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( allowAllocs )
|
|
|
|
{
|
|
|
|
int allocSize = Max( baseBlockSize, alignedBytes + ( int )sizeof( idDynamicBlock<type> ) );
|
|
|
|
block = ( idDynamicBlock<type>* ) Mem_Alloc16( allocSize, _tag_ );
|
|
|
|
if( lockMemory )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//idLib::sys->LockMemory( block, baseBlockSize );
|
|
|
|
}
|
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
memcpy( block->id, blockId, sizeof( block->id ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
block->allocator = ( void* )this;
|
2012-11-26 18:58:24 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
block->SetSize( allocSize - ( int )sizeof( idDynamicBlock<type> ), true );
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next = NULL;
|
|
|
|
block->prev = lastBlock;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lastBlock )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock->next = block;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
firstBlock = block;
|
|
|
|
}
|
|
|
|
lastBlock = block;
|
|
|
|
block->node = NULL;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
numBaseBlocks++;
|
|
|
|
baseBlockMemory += allocSize;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::ResizeInternal( idDynamicBlock<type>* block, const int num )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int alignedBytes = ( num * sizeof( type ) + 15 ) & ~15;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
2012-11-28 15:47:07 +00:00
|
|
|
assert( block->id[0] == 0x11111111 && block->id[1] == 0x22222222 && block->id[2] == 0x33333333 && block->allocator == ( void* )this );
|
2012-11-26 18:58:24 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the new size is larger
|
2012-11-28 15:47:07 +00:00
|
|
|
if( alignedBytes > block->GetSize() )
|
|
|
|
{
|
|
|
|
|
|
|
|
idDynamicBlock<type>* nextBlock = block->next;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// try to annexate the next block if it's free
|
2012-11-28 15:47:07 +00:00
|
|
|
if( nextBlock && !nextBlock->IsBaseBlock() && nextBlock->node != NULL &&
|
|
|
|
block->GetSize() + ( int )sizeof( idDynamicBlock<type> ) + nextBlock->GetSize() >= alignedBytes )
|
|
|
|
{
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
UnlinkFreeInternal( nextBlock );
|
2012-11-28 15:47:07 +00:00
|
|
|
block->SetSize( block->GetSize() + ( int )sizeof( idDynamicBlock<type> ) + nextBlock->GetSize(), block->IsBaseBlock() );
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next = nextBlock->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( nextBlock->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
nextBlock->next->prev = block;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock = block;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// allocate a new block and copy
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* oldBlock = block;
|
2012-11-26 18:58:24 +00:00
|
|
|
block = AllocInternal( num );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy( block->GetMemory(), oldBlock->GetMemory(), oldBlock->GetSize() );
|
|
|
|
FreeInternal( oldBlock );
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the unused space at the end of this block is large enough to hold a block with at least one element
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->GetSize() - alignedBytes - ( int )sizeof( idDynamicBlock<type> ) < Max( minBlockSize, ( int )sizeof( type ) ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return block;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
idDynamicBlock<type>* newBlock;
|
|
|
|
|
|
|
|
newBlock = ( idDynamicBlock<type>* )( ( ( byte* ) block ) + ( int )sizeof( idDynamicBlock<type> ) + alignedBytes );
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
|
|
|
memcpy( newBlock->id, blockId, sizeof( newBlock->id ) );
|
2012-11-28 15:47:07 +00:00
|
|
|
newBlock->allocator = ( void* )this;
|
2012-11-26 18:58:24 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
newBlock->SetSize( block->GetSize() - alignedBytes - ( int )sizeof( idDynamicBlock<type> ), false );
|
2012-11-26 18:58:24 +00:00
|
|
|
newBlock->next = block->next;
|
|
|
|
newBlock->prev = block;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( newBlock->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
newBlock->next->prev = newBlock;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock = newBlock;
|
|
|
|
}
|
|
|
|
newBlock->node = NULL;
|
|
|
|
block->next = newBlock;
|
|
|
|
block->SetSize( alignedBytes, block->IsBaseBlock() );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
FreeInternal( newBlock );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::FreeInternal( idDynamicBlock<type>* block )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
assert( block->node == NULL );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef DYNAMIC_BLOCK_ALLOC_CHECK
|
2012-11-28 15:47:07 +00:00
|
|
|
assert( block->id[0] == 0x11111111 && block->id[1] == 0x22222222 && block->id[2] == 0x33333333 && block->allocator == ( void* )this );
|
2012-11-26 18:58:24 +00:00
|
|
|
#endif
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// try to merge with a next free block
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* nextBlock = block->next;
|
|
|
|
if( nextBlock && !nextBlock->IsBaseBlock() && nextBlock->node != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnlinkFreeInternal( nextBlock );
|
2012-11-28 15:47:07 +00:00
|
|
|
block->SetSize( block->GetSize() + ( int )sizeof( idDynamicBlock<type> ) + nextBlock->GetSize(), block->IsBaseBlock() );
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next = nextBlock->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( nextBlock->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
nextBlock->next->prev = block;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock = block;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// try to merge with a previous free block
|
2012-11-28 15:47:07 +00:00
|
|
|
idDynamicBlock<type>* prevBlock = block->prev;
|
|
|
|
if( prevBlock && !block->IsBaseBlock() && prevBlock->node != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
UnlinkFreeInternal( prevBlock );
|
2012-11-28 15:47:07 +00:00
|
|
|
prevBlock->SetSize( prevBlock->GetSize() + ( int )sizeof( idDynamicBlock<type> ) + block->GetSize(), prevBlock->IsBaseBlock() );
|
2012-11-26 18:58:24 +00:00
|
|
|
prevBlock->next = block->next;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block->next->prev = prevBlock;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
lastBlock = prevBlock;
|
|
|
|
}
|
|
|
|
LinkFreeInternal( prevBlock );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
LinkFreeInternal( block );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::LinkFreeInternal( idDynamicBlock<type>* block )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
block->node = freeTree.Add( block, block->GetSize() );
|
|
|
|
numFreeBlocks++;
|
|
|
|
freeBlockMemory += block->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::UnlinkFreeInternal( idDynamicBlock<type>* block )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
freeTree.Remove( block->node );
|
|
|
|
block->node = NULL;
|
|
|
|
numFreeBlocks--;
|
|
|
|
freeBlockMemory -= block->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class type, int baseBlockSize, int minBlockSize, memTag_t _tag_>
|
2012-11-28 15:47:07 +00:00
|
|
|
void idDynamicBlockAlloc<type, baseBlockSize, minBlockSize, _tag_>::CheckMemory() const
|
|
|
|
{
|
|
|
|
idDynamicBlock<type>* block;
|
|
|
|
|
|
|
|
for( block = firstBlock; block != NULL; block = block->next )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// make sure the block is properly linked
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->prev == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( firstBlock == block );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( block->prev->next == block );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( block->next == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( lastBlock == block );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( block->next->prev == block );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !__HEAP_H__ */
|