leethomason / tinyxml2

TinyXML2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.
zlib License
5.05k stars 1.83k forks source link

Unable to Overload the new operator in the Block* block = new Block(); functions #54

Closed ssedbe closed 11 years ago

ssedbe commented 12 years ago

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;

};

leethomason commented 11 years ago

Inactive, closing.