Boost logo

Boost :

From: Tom Widmer (tom_usenet_at_[hidden])
Date: 2006-02-17 09:11:24


I noticed in the shmem implementation of the node based containers,
multiple inheritence of allocators is used to provide the allocators of
the three types required. This is going to add something like 8 extra
bytes of overhead to the container for a allocator type of size 4,
compared to having only a single allocator base class.

Is there any reason not to create copies of a single allocator on
demand? e.g. something like this:

template<class T, class A, bool convertible_construct>
struct shmem_list_alloc
    : public A
{
//...

void priv_init()
{
       //copy from A to make NodeAlloc
       m_node = NodeAlloc(static_cast<A&>(*this)).allocate(1);

       if(!boost::has_trivial_constructor<NodePtr>::value){
          scoped_ptr<Node, Deallocator>node_deallocator(m_node, *this);
          //Make sure destructors are called before memory is freed
          //if an exception is thrown
          {
             typedef typename PtrAlloc::pointer NodePtrPtr;
             //copy allocator
            PtrAlloc ptrAlloc(static_cast<NodeAlloc&>(*this));
             NodePtrPtr pnext(ptrAlloc.address(m_node->m_next)),
                pprev(ptrAlloc.address(m_node->m_prev));
                      ptrAlloc.construct(pnext, m_node);
             scoped_ptr<NodePtr, PtrDestructor> next_destroy(pnext, *this);
            ptrAlloc.construct(pprev, m_node);
             next_destroy.release();
          }
          node_deallocator.release();
       }
       else{
          m_node->m_next = m_node;
          m_node->m_prev = m_node;
       }
    }

That way you save 8 bytes in size, for presumably no measurable
performance penalty from the cheap copying of the allocator. Obviously,
any allocators used with this scheme must have the property that copies
don't independently stop being == to each other, but that's almost
required anyway for the multiple inheritence version.

Tom


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk