Clarify "block" and "chunk"

This commit is contained in:
Dmitry-Me 2016-08-09 17:59:31 +03:00
parent 75c8f40640
commit 88145b8ae6
1 changed files with 23 additions and 18 deletions

View File

@ -315,7 +315,7 @@ public:
/* /*
Template child class to create pools of the correct type. Template child class to create pools of the correct type.
*/ */
template< int SIZE > template< int ITEM_SIZE >
class MemPoolT : public MemPool class MemPoolT : public MemPool
{ {
public: public:
@ -338,7 +338,7 @@ public:
} }
virtual int ItemSize() const { virtual int ItemSize() const {
return SIZE; return ITEM_SIZE;
} }
int CurrentAllocs() const { int CurrentAllocs() const {
return _currentAllocs; return _currentAllocs;
@ -350,13 +350,15 @@ public:
Block* block = new Block(); Block* block = new Block();
_blockPtrs.Push( block ); _blockPtrs.Push( block );
for( int i=0; i<COUNT-1; ++i ) { Item* blockItems = block->items;
block->chunk[i].next = &block->chunk[i+1]; for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
blockItems[i].next = &(blockItems[i + 1]);
} }
block->chunk[COUNT-1].next = 0; blockItems[ITEMS_PER_BLOCK - 1].next = 0;
_root = block->chunk; _root = blockItems;
} }
void* result = _root; Item* const result = _root;
TIXMLASSERT( result != 0 );
_root = _root->next; _root = _root->next;
++_currentAllocs; ++_currentAllocs;
@ -373,16 +375,17 @@ public:
return; return;
} }
--_currentAllocs; --_currentAllocs;
Chunk* chunk = static_cast<Chunk*>( mem ); Item* item = static_cast<Item*>( mem );
#ifdef DEBUG #ifdef DEBUG
memset( chunk, 0xfe, sizeof(Chunk) ); memset( item, 0xfe, sizeof( *item ) );
#endif #endif
chunk->next = _root; item->next = _root;
_root = chunk; _root = item;
} }
void Trace( const char* name ) { void Trace( const char* name ) {
printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n", printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() ); name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
} }
void SetTracked() { void SetTracked() {
@ -402,21 +405,23 @@ public:
// 16k: 5200 // 16k: 5200
// 32k: 4300 // 32k: 4300
// 64k: 4000 21000 // 64k: 4000 21000
enum { COUNT = (4*1024)/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private // Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
// in private part if ITEMS_PER_BLOCK is private
enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
private: private:
MemPoolT( const MemPoolT& ); // not supported MemPoolT( const MemPoolT& ); // not supported
void operator=( const MemPoolT& ); // not supported void operator=( const MemPoolT& ); // not supported
union Chunk { union Item {
Chunk* next; Item* next;
char mem[SIZE]; char itemData[ITEM_SIZE];
}; };
struct Block { struct Block {
Chunk chunk[COUNT]; Item items[ITEMS_PER_BLOCK];
}; };
DynArray< Block*, 10 > _blockPtrs; DynArray< Block*, 10 > _blockPtrs;
Chunk* _root; Item* _root;
int _currentAllocs; int _currentAllocs;
int _nAllocs; int _nAllocs;