Due to performance & memory issue present in the previous tinyxml version i have ported the new tinyxml2 for our purpose.
We got good improvement in performance level when compared with previous tinyxml. Also i need to measure the memory requirement for writing & parsing the XML data using the tinyxml2. because I have used tinyxml2 to write the large amount (more than 9MB) of data in xml format.
So I decided to overload the "new" operators in xml classes to find how much memory occupied for xml data's.
But i am unable to overload the "new" operator to share memory from own memory area instead of heap memory.
Please help me, how can i overload the "new" operator for this below case to share memory from own memory area.
Also, how can i found out memory leak incaseof memory from heap.
template< int SIZE >
class MemPoolT : public MemPool
{
public:
MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
~MemPoolT() {
// Delete the blocks.
for( int i=0; i<blockPtrs.Size(); ++i ) {
delete blockPtrs[i];
}
}
virtual void* Alloc() {
if ( !root ) {
// Need a new block.
Block* block = new Block();
blockPtrs.Push( block );
for( int i=0; i<COUNT-1; ++i ) {
block->chunk[i].next = &block->chunk[i+1];
}
block->chunk[COUNT-1].next = 0;
root = block->chunk;
}
void* result = root;
root = root->next;
++currentAllocs;
if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
nAllocs++;
return result;
}
virtual void Free( void* mem ) {
if ( !mem ) return;
--currentAllocs;
Chunk* chunk = (Chunk*)mem;
memset( chunk, 0xfe, sizeof(Chunk) );
chunk->next = root;
root = chunk;
}
private:
enum { COUNT = 1024/SIZE };
union Chunk {
Chunk* next;
char mem[SIZE];
};
struct Block {
Chunk chunk[COUNT];
};
DynArray< Block*, 10 > blockPtrs;
Chunk* root;
int currentAllocs;
int nAllocs;
int maxAllocs;
Due to performance & memory issue present in the previous tinyxml version i have ported the new tinyxml2 for our purpose.
We got good improvement in performance level when compared with previous tinyxml. Also i need to measure the memory requirement for writing & parsing the XML data using the tinyxml2. because I have used tinyxml2 to write the large amount (more than 9MB) of data in xml format.
So I decided to overload the "new" operators in xml classes to find how much memory occupied for xml data's.
But i am unable to overload the "new" operator to share memory from own memory area instead of heap memory.
Please help me, how can i overload the "new" operator for this below case to share memory from own memory area.
Also, how can i found out memory leak incaseof memory from heap.
template< int SIZE > class MemPoolT : public MemPool { public: MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {} ~MemPoolT() { // Delete the blocks. for( int i=0; i<blockPtrs.Size(); ++i ) { delete blockPtrs[i]; } }
};