64 #ifndef ID_DEBUG_MEMORY
73 #ifdef ID_REDIRECT_NEWDELETE
75 __inline
void *
operator new(
size_t s ) {
78 __inline
void operator delete(
void *
p ) {
81 __inline
void *
operator new[](
size_t s ) {
84 __inline
void operator delete[](
void *
p ) {
92 void *
Mem_Alloc(
const int size,
const char *fileName,
const int lineNumber );
94 void Mem_Free(
void *ptr,
const char *fileName,
const int lineNumber );
95 char *
Mem_CopyString(
const char *
in,
const char *fileName,
const int lineNumber );
96 void *
Mem_Alloc16(
const int size,
const char *fileName,
const int lineNumber );
97 void Mem_Free16(
void *ptr,
const char *fileName,
const int lineNumber );
99 #ifdef ID_REDIRECT_NEWDELETE
101 __inline
void *
operator new(
size_t s,
int t1,
int t2,
char *fileName,
int lineNumber ) {
104 __inline
void operator delete(
void *
p,
int t1,
int t2,
char *fileName,
int lineNumber ) {
107 __inline
void *
operator new[](
size_t s,
int t1,
int t2,
char *fileName,
int lineNumber ) {
110 __inline
void operator delete[](
void *
p,
int t1,
int t2,
char *fileName,
int lineNumber ) {
113 __inline
void *
operator new(
size_t s ) {
116 __inline
void operator delete(
void *
p ) {
119 __inline
void *
operator new[](
size_t s ) {
122 __inline
void operator delete[](
void *
p ) {
126 #define ID_DEBUG_NEW new( 0, 0, __FILE__, __LINE__ )
128 #define new ID_DEBUG_NEW
132 #define Mem_Alloc( size ) Mem_Alloc( size, __FILE__, __LINE__ )
133 #define Mem_ClearedAlloc( size ) Mem_ClearedAlloc( size, __FILE__, __LINE__ )
134 #define Mem_Free( ptr ) Mem_Free( ptr, __FILE__, __LINE__ )
135 #define Mem_CopyString( s ) Mem_CopyString( s, __FILE__, __LINE__ )
136 #define Mem_Alloc16( size ) Mem_Alloc16( size, __FILE__, __LINE__ )
137 #define Mem_Free16( ptr ) Mem_Free16( ptr, __FILE__, __LINE__ )
153 template<
class type,
int blockSize>
184 template<
class type,
int blockSize>
191 template<
class type,
int blockSize>
196 template<
class type,
int blockSize>
200 block->
next = blocks;
202 for (
int i = 0;
i < blockSize;
i++ ) {
215 template<
class type,
int blockSize>
218 element->
next = free;
223 template<
class type,
int blockSize>
227 blocks = blocks->
next;
247 template<
class type,
int baseBlockSize,
int minBlockSize>
283 template<
class type,
int baseBlockSize,
int minBlockSize>
288 template<
class type,
int baseBlockSize,
int minBlockSize>
293 template<
class type,
int baseBlockSize,
int minBlockSize>
297 template<
class type,
int baseBlockSize,
int minBlockSize>
302 template<
class type,
int baseBlockSize,
int minBlockSize>
309 usedBlockMemory += num *
sizeof(
type );
313 template<
class type,
int baseBlockSize,
int minBlockSize>
331 template<
class type,
int baseBlockSize,
int minBlockSize>
340 template<
class type,
int baseBlockSize,
int minBlockSize>
345 template<
class type,
int baseBlockSize,
int minBlockSize>
378 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
389 template<
class type,
int baseBlockSize,
int minBlockSize>
421 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
445 template<
class type,
int baseBlockSize,
int minBlockSize>
450 template<
class type,
int baseBlockSize,
int minBlockSize>
455 template<
class type,
int baseBlockSize,
int minBlockSize>
460 template<
class type,
int baseBlockSize,
int minBlockSize>
464 for ( block = firstBlock; block !=
NULL; block = block->
next ) {
466 FreeInternal( block );
470 for ( block = firstBlock; block !=
NULL; block = firstBlock ) {
471 firstBlock = block->
next;
484 template<
class type,
int baseBlockSize,
int minBlockSize>
488 for (
int i = numBaseBlocks;
i < numBlocks;
i++ ) {
493 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
494 memcpy( block->id, blockId,
sizeof( block->id ) );
495 block->allocator = (
void*)
this;
499 block->
prev = lastBlock;
501 lastBlock->next = block;
508 FreeInternal( block );
511 baseBlockMemory += baseBlockSize;
517 template<
class type,
int baseBlockSize,
int minBlockSize>
522 template<
class type,
int baseBlockSize,
int minBlockSize>
526 for ( block = firstBlock; block !=
NULL; block = next ) {
530 UnlinkFreeInternal( block );
534 firstBlock = block->
next;
539 lastBlock = block->
prev;
550 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
555 template<
class type,
int baseBlockSize,
int minBlockSize>
557 int numEmptyBaseBlocks;
560 numEmptyBaseBlocks = 0;
561 for ( block = firstBlock; block !=
NULL; block = block->
next ) {
563 numEmptyBaseBlocks++;
566 return numEmptyBaseBlocks;
569 template<
class type,
int baseBlockSize,
int minBlockSize>
579 block = AllocInternal( num );
580 if ( block ==
NULL ) {
583 block = ResizeInternal( block, num );
584 if ( block ==
NULL ) {
588 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
593 usedBlockMemory += block->
GetSize();
598 template<
class type,
int baseBlockSize,
int minBlockSize>
614 usedBlockMemory -= block->
GetSize();
616 block = ResizeInternal( block, num );
617 if ( block ==
NULL ) {
621 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
625 usedBlockMemory += block->
GetSize();
630 template<
class type,
int baseBlockSize,
int minBlockSize>
642 usedBlockMemory -= block->
GetSize();
644 FreeInternal( block );
646 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
651 template<
class type,
int baseBlockSize,
int minBlockSize>
662 return "memory has been freed";
665 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
666 if ( block->id[0] != 0x11111111 || block->id[1] != 0x22222222 || block->id[2] != 0x33333333 ) {
667 return "memory has invalid id";
669 if ( block->allocator != (
void*)this ) {
670 return "memory was allocated with different allocator";
691 template<
class type,
int baseBlockSize,
int minBlockSize>
693 firstBlock = lastBlock =
NULL;
706 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
707 blockId[0] = 0x11111111;
708 blockId[1] = 0x22222222;
709 blockId[2] = 0x33333333;
713 template<
class type,
int baseBlockSize,
int minBlockSize>
716 int alignedBytes = ( num *
sizeof(
type ) + 15 ) & ~15;
718 block = freeTree.FindSmallestLargerEqual( alignedBytes );
719 if ( block !=
NULL ) {
720 UnlinkFreeInternal( block );
721 }
else if ( allowAllocs ) {
727 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
728 memcpy( block->id, blockId,
sizeof( block->id ) );
729 block->allocator = (
void*)
this;
733 block->
prev = lastBlock;
735 lastBlock->next = block;
743 baseBlockMemory += allocSize;
749 template<
class type,
int baseBlockSize,
int minBlockSize>
751 int alignedBytes = ( num *
sizeof(
type ) + 15 ) & ~15;
753 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
754 assert( block->id[0] == 0x11111111 && block->id[1] == 0x22222222 && block->id[2] == 0x33333333 && block->allocator == (
void*)
this );
758 if ( alignedBytes > block->
GetSize() ) {
766 UnlinkFreeInternal( nextBlock );
769 if ( nextBlock->
next ) {
770 nextBlock->
next->prev = block;
777 block = AllocInternal( num );
778 if ( block ==
NULL ) {
782 FreeInternal( oldBlock );
794 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
795 memcpy( newBlock->id, blockId,
sizeof( newBlock->id ) );
796 newBlock->allocator = (
void*)
this;
800 newBlock->
prev = block;
801 if ( newBlock->
next ) {
802 newBlock->
next->prev = newBlock;
804 lastBlock = newBlock;
807 block->
next = newBlock;
810 FreeInternal( newBlock );
815 template<
class type,
int baseBlockSize,
int minBlockSize>
820 #ifdef DYNAMIC_BLOCK_ALLOC_CHECK
821 assert( block->id[0] == 0x11111111 && block->id[1] == 0x22222222 && block->id[2] == 0x33333333 && block->allocator == (
void*)
this );
827 UnlinkFreeInternal( nextBlock );
830 if ( nextBlock->
next ) {
831 nextBlock->
next->prev = block;
840 UnlinkFreeInternal( prevBlock );
844 block->
next->prev = prevBlock;
846 lastBlock = prevBlock;
848 LinkFreeInternal( prevBlock );
850 LinkFreeInternal( block );
854 template<
class type,
int baseBlockSize,
int minBlockSize>
856 block->
node = freeTree.Add( block, block->
GetSize() );
858 freeBlockMemory += block->
GetSize();
861 template<
class type,
int baseBlockSize,
int minBlockSize>
863 freeTree.Remove( block->
node );
866 freeBlockMemory -= block->
GetSize();
869 template<
class type,
int baseBlockSize,
int minBlockSize>
873 for ( block = firstBlock; block !=
NULL; block = block->
next ) {
876 assert( firstBlock == block );
881 assert( lastBlock == block );
idDynamicBlockAlloc(void)
int GetNumFreeBlocks(void) const
void FreeEmptyBaseBlocks(void)
assert(prefInfo.fullscreenBtn)
idDynamicBlock< type > * next
int GetNumUsedBlocks(void) const
struct idBlockAlloc::block_s block_t
void UnlinkFreeInternal(idDynamicBlock< type > *block)
void lock(CURL *handle, curl_lock_data data, curl_lock_access access, void *useptr)
void LinkFreeInternal(idDynamicBlock< type > *block)
idBTree< idDynamicBlock< type >, int, 4 > freeTree
type * Alloc(const int num)
idDynamicBlock< type > * ResizeInternal(idDynamicBlock< type > *block, const int num)
ID_INLINE T Max(T x, T y)
void Mem_DumpCompressed_f(const class idCmdArgs &args)
bool IsBaseBlock(void) const
void FreeInternal(idDynamicBlock< type > *block)
int GetNumUsedBlocks(void) const
void SetSize(int s, bool isBaseBlock)
char * Mem_CopyString(const char *in)
void SetFixedBlocks(int numBlocks)
int GetUsedBlockMemory(void) const
int GetFreeBlockMemory(void) const
int GetBaseBlockMemory(void) const
void CheckMemory(void) const
virtual bool LockMemory(void *ptr, int bytes)=0
type * Resize(type *ptr, const int num)
idBTreeNode< idDynamicBlock< type >, int > * node
GLuint GLuint GLsizei GLenum type
type * Alloc(const int num)
void SetLockMemory(bool lock)
int GetNumEmptyBaseBlocks(void) const
void Mem_GetStats(memoryStats_t &stats)
void Mem_Dump_f(const class idCmdArgs &args)
element_t elements[blockSize]
idDynamicBlock< type > * AllocInternal(const int num)
virtual bool UnlockMemory(void *ptr, int bytes)=0
type * Resize(type *ptr, const int num)
type * GetMemory(void) const
int GetNumEmptyBaseBlocks(void) const
void * Mem_Alloc(const int size)
const char * CheckMemory(const type *ptr) const
void * Mem_Alloc16(const int size)
~idDynamicBlockAlloc(void)
int GetAllocCount(void) const
int GetBaseBlockMemory(void) const
int GetNumBaseBlocks(void) const
int GetNumBaseBlocks(void) const
void * Mem_ClearedAlloc(const int size)
idDynamicBlock< type > * prev
idDynamicBlock< type > * firstBlock
void Mem_AllocDefragBlock(void)
void FreeEmptyBaseBlocks(void)
struct idBlockAlloc::element_s element_t
int GetNumFreeBlocks(void) const
void Mem_ClearFrameStats(void)
int GetUsedBlockMemory(void) const
void Mem_EnableLeakTest(const char *name)
void SetLockMemory(bool lock)
int GetFreeBlockMemory(void) const
void Mem_Free16(void *ptr)
idDynamicBlock< type > * lastBlock
void Mem_GetFrameStats(memoryStats_t &allocs, memoryStats_t &frees)
void SetFixedBlocks(int numBlocks)
int GetFreeCount(void) const
int GetTotalCount(void) const