Boost logo

Boost :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2006-02-08 12:27:52


Hi Pavel,

> Rather than yet another underdocumented shared pointer with
> subtly different name it should be:
>
> boost::shared_ptr<void, shmem::deleter> p = segment.allocate(1024);

I don't know if in CVS this has changed but in my boost version
boost::shared_ptr has only one template parameter and the deleter is
passed in the constructor and dynamically (and polymorphically) created.

> void* p = segment_allocate(1024, shmem::manual_lifetime);

What you want to express with this?

>> Where the shared_ptr has a deleter calling
>> named_shared_object::deallocate. I'd also think that the 'shared_memory'
>> should not be destructed until the last shmem::shared_ptr is released.

To maintain the shared memory open while you have allocated fragments
you can try this (I've not compiled it so it can have errors):

struct shmem_deleter
{
   boost::shared_ptr<named_shared_object> m_named_shared_object;

   shmem_deleter(const boost::shared_ptr<named_shared_object> &segment)
      : m_named_shared_object(segment){}

    void operator()(void *ptr)
    {
       m_named_shared_object.deallocate(ptr);
    }
};

int main ()
{
    shared_ptr<named_shared_object> segment(new named_shared_object);
    segment->create(/*...*/);

    shared_ptr<void> buffer
       (segment->allocate(1024), shmem_deleter(segment));
   return 0;
}

The idea is that the deleter of the shared pointer of an allocation has
a shared pointer to the segment (the deleter is shared between all
copies of buffer). When all shared pointers are deleted, the segment
will be destroyed if there are no more shared_ptr<named_shared_object>
objects around pointing to the same named_shared_object. The same can be
used with named objects, we must just change the deleter destruction
function to a m_named_shared_object.destroy_ptr(ptr).

Ion


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