There are several errors in this code. Here's one :
MemMgr::InsertBlock(void* block)
{
if(sizeof(block) > MAX)
throw runtime_error("Blocksize too big");
if(_blocks.size() > MAX)
throw runtime_error("Out of room");
memcpy(&_mempool[_idx], block, MAX);
_blocks.push_back(&_mempool[_idx++]);
}
block
is a pointer to a void type. In 64-bit code the size of
block
will
always be size of eight bytes. I think you want to compare the amount of data in block to MAX and the only way you can do that is to pass it to the function.
The memcpy statement is incorrect and the only reason the app does not fail is because only MAX bytes are copied. The destination, the first argument, is the problem. Look again at the definition of
mempool
:
void* _mempool[MAX];
It is declared an array of MAX void pointers. The memcpy statement copies into that array. What you really want is to copy
block
to one of the pointers in the array. In this case, the
_idx
'th one. That line should be written as :
memcpy( _mempool[_idx], block, MAX );
if you want copy MAX bytes. This assumes that _mempool[ _idx ] has a valid address in it.
The line following it, the push_back statement, has the same problem. It is using the address of the pointer in the array instead of its value. It should look like this :
_blocks.push_back( _mempool[_idx++] );
That is, if you want to add the actual pointer itself onto the list and not the address of the pointer. That is what I mean the value of the pointer.
One other thing - in my opinion the size of the data block and the number of data blocks should have two different names. They can certainly have the same values but I don't think the names should be the same because they refer to two entirely different things.