Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-09-02 14:18:46


--- 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.

1. The allocator parameter should implement the UserAllocator interface.
2. All instances of UserAllocator must be equal and stateless.
3. If your class is using STL containers, use boost::memory::allocator
   adapter (see bellow).
4. To construct/destroy objects, use boost::memory::construct/destroy.
5. Avoid using explicit references to allocators.

// boost standard implementation
namespace boost
{
struct memory
{
  //UserAllocator to Standard Allocator adapter
  temlate<typename T, typename UserAllocator>
  struct allocator
  {
     ... *** implement std::allocator interface using UserAllocator
  };

  //construct n objects of type T using UserAllocator
  template<typename T, typename UserAllocator>
  static T* construct( size_t n ) {...}

  //destroy n objects of type T using UserAllocator
  template<typename T, typename UserAllocator>
  static void destroy( T* p, size_t n );
  
};
};

Usage example:
==============
template< typename T, typename UserAllocator = boost::default_user_allocator_new_delete >
class X
{
    T* _p;
    std::vector<T, boost::memory::allocator<T, UserAllocator> > _v;

    X()
    {
        _p = boost::memory::construct<T, UserAllocator>( 1 )
    }
    ~X()
    {
        if( _p )
            boost::memory::destroy<T, UserAllocator>( _p, 1 );
    }
};

Eugene

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com


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