Menu

LOKI SmallObject Allocator copy live objects?

Developers
2007-12-18
2013-04-08
  • John Basrai

    John Basrai - 2007-12-18

    When the FixedAllocator swaps chunks around durning delete souldn't this be done with vector of pointers instead of by value?  In others words shouldn't FixedAllocator store a vector of pointers to Chunks inteads of vector of chunks?  My concern with using a by value vector is can it ever end up moving live objects or is only every moving deallocated memory?

    In FixedAllocator::trimEmptyChunk we have:

        if ( lastChunk != emptyChunk_ )
            std::swap( *emptyChunk_, *lastChunk );

        lastChunk->Release();
        chunks_.pop_back();

    This code is getting the empty chunk to the end of the vector to free it but isn't *lastChunk live?  So any objects that were in it were moved in memory without there their knowledge.  What am I missing here?
    -john

     
    • Richard Sposato

      Richard Sposato - 2008-04-16

      Hi John,

      The element at lastChunk is live, and yes the contents of *lastChunk get moved without their knowledge.  This is not a problem since the data members of Chunk are all primitive (1 pointer and 2 unsigned char's) and you can safely move primitive data.  If Chunk had instances of objects among its data, or if any object that had pointers or references to data members within Chunk, then moving would cause problems.

      If the vector stored pointers to Chunk's instead of storing Chunk's by value, then we would have to allocate extra memory to store each Chunk.  I'd consider that an unnecessary step that adds to the runtime cost of FixedAllocator.

      Cheers,

      Rich

       

Log in to post a comment.