|
Boost : |
From: Gregory Colvin (gregory.colvin_at_[hidden])
Date: 2003-09-02 16:06:59
On Tuesday, Sep 2, 2003, at 13:18 America/Denver, E. Gladyshev wrote:
> --- Gregory Colvin <gregory.colvin_at_[hidden]> wrote:
>> Yep. I still think UserAllocator is a good default, and that where it
>> doesn't suffice there is some value to playing nicely with STL.
>>
>> So even when we come up with some beautiful new thing to do the
>> allocation job better, we will still need adaptors both ways, so that
>> one can get an allocator from an STL container and turn it in to one
>> of our new things, or take one of our new things and turn it into an
>> allocator to use in an STL container.
>
> Am I right in trying to summarize your suggestion about UserAllocator?
>
>
> If you want to parametrize how a boost class manages memory,
> use UserAllocator parameter.
Unless you need the standard Allocator interface.
> 1. The allocator parameter should implement the UserAllocator
> interface.
> 2. All instances of UserAllocator must be equal and stateless.
The Boost pools use UserAllocator only as a type parameter. It is not
required to be constructible or comparable.
> 3. If your class is using STL containers, use boost::memory::allocator
> adapter (see bellow).
Why not just use std::allocator?
> 4. To construct/destroy objects, use boost::memory::construct/destroy.
See below.
> 5. Avoid using explicit references to allocators.
>
> ... Usage example:
> ==============
> template< typename T, typename UserAllocator =
> boost::default_user_allocator_new_delete >
> class X
> {
What if you want X to use the same allocator as some other STL container
constructed with a non-Boost allocator? That would be difficult unless
you have
template<typename T, class StdAllocator = std::allocator<T> >
struct X {
X(const StdAllocator&);
Also, it might sometimes be desirable, as it is for shared_ptr, to
defer the choice of allocator until construction time:
template<typename T>
struct X {
template<class StdAllocator = std::allocator<T> >
X(const StdAllocator&);
Or, if UserAllocator suffices:
template<typename T>
struct X {
template<class UserAllocator> X();
> T* _p;
Leading underscores are a no-no.
> std::vector<T, boost::memory::allocator<T, UserAllocator> > _v;
>
> X()
> {
> _p = boost::memory::construct<T, UserAllocator>( 1 )
How to pass arguments to T's constructor? Better just
p = new(UserAllocator::malloc(sizeof T)) T(...)
> }
> ~X()
> {
> if( _p )
> boost::memory::destroy<T, UserAllocator>( _p, 1 );
In which case, why not just
p->~T(), UserAllocator::free(p);
?
> }
> };
>
> Eugene
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk