This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: gcc 3.x custom allocators
- From: Benjamin Kosnik <bkoz at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org
- Cc: rob at welcomehome dot org
- Date: Fri, 10 Jan 2003 12:12:54 -0600
- Subject: Re: gcc 3.x custom allocators
- References: <20030109173544.A785@welcomehome.org>
> 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) { }
>};
I've enclosed some weak example code, based on the custom allocators a
la the pool allocator in TCPL 19.4.2 A User-Defined Allocator.
> 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 ?
I thought the only change was the removal of the "reallocate" member.
> 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.
I don't suppose you can post a full example of what you are trying to
do, one that works with 2.95.3? Then I could try to port it for you.
It would be really nice to have a complete, usable example in the
libstdc++ docs for this. It would be really, really nice to have docs
for custom allocators that used: 1) libhoard, 2) persistent allocation,
3) shared segments like you are trying to do.
-benjamin