gcc 3.x custom allocators

Rob Savoye rob@welcomehome.org
Fri Jan 10 00:37:00 GMT 2003


 
  Howdy. I'm trying to grok the new gcc 3.2 style of writing a custom
allocator. I have a C++ based memory manager I use for a shared memory segment
so I can instantiate STL containers in the shared memory segment for
passing data around. This I guess now is the old style, that looks something
like this:

// code guts remove for clarity
template < typename T, typename CTASallocType = allocator<T> >
class ShmMem
{
public:
    CTASallocType alloc;
    typedef typename CTASallocType::size_type size_type;
    typedef typename CTASallocType::difference_type difference_type;
    typedef typename CTASallocType::pointer pointer;
    typedef typename CTASallocType::const_pointer const_pointer;
    typedef typename CTASallocType::reference reference;
    typedef typename CTASallocType::value_type value_type;
    typedef typename CTASallocType::const_reference const_reference;
    template <typename U> struct rebind {
        typedef ShmMem<U, 
                       typename CTASallocType::template rebind<U>::other>
other;
    };

    ShmMem(void) {  }
    ~ShmMem() {}

    ShmMem(const ShmMem& x)
        : alloc(x.alloc) {}

    template <typename U>
    ShmMem(const ShmMem<U, 
        typename CTASallocType::template rebind<U>::other>& x)
        : alloc(x.alloc) {}
  
    void construct(pointer p, const value_type &val) { }
    void destroy(pointer p) { }
    pointer allocate(size_type sz, const void* vp = 0) { }
    void deallocate(pointer p, size_type sz) { }
};

  All I really want to do is hook into allocate and deallocate to use my
custom allocator. I've been reading libstdc++ docs and stl_alloc.h, but
so far, I just don't get it. Plus I gather this is all gonna change again 
soon ?

  My code still works fine with gcc 2.95.x, but I'm trying to work using
gcc 3.2 as much as I can these days.

	- rob -



More information about the Libstdc++ mailing list