Topic: what's allocator?


Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/03/31
Raw View
"suj_h" <suj_h@microsoft.com> writes:
> If you look at the header file for std::list, it is defined as follow.
> What's the purpose of allocator? In what occasion do you need it?

The list implementation needs to allocate space internally for its
data structures. The allocator parameter gives you a chance to tell
the implementation how it should do so. It's used when you're doing
stupid memory tricks, like trying to allocate data structures in a
block of shared memory.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/03/31
Raw View
suj_h wrote:
>
> If you look at the header file for std::list, it is defined as follow.
> What's the purpose of allocator? In what occasion do you need it?
>
> template<class T, class A = allocator<T> >
> class list
> {
> public:
> typedef A allocator_type; typedef A::size_type size_type; typedef
> A::difference_type difference_type;
> ...

std::allocator<> is the default allocator used by all of the standard
library's containers. It's defined in section 20.4.1. It must meet all
of the standard's Allocator requirements specified in Section 20.1.5;
otherwise it wouldn't legal to use the default value for the Allocator
parameter. It is more specific than the general Allocators, in that it
is defined to use 'new' to perform the allocations, and of course to use
'delete' to deallocate them. Here's the synopsis give in 20.4.1.

namespace std {
  template <class T> class allocator;

  // _specialize for_  void:
  template <> class allocator<void> {
  public:
    typedef void*        pointer;
    typedef const void * const_pointer;
    // _reference-to-_ void _members are impossible._
    typedef void  value_type;
    template <class U> struct rebind { typedef allocator<U> other; };
  };
  template <class T> class allocator {
   public:
    typedef size_t    size_type;
    typedef ptrdiff_t difference_type;
    typedef T*        pointer;
    typedef const T*  const_pointer;
    typedef T&        reference;
    typedef const T&  const_reference;
    typedef T         value_type;
    template <class U> struct rebind { typedef allocator<U> other; };

    allocator() throw();
    allocator(const allocator&) throw();
    template <class U> allocator(const allocator<U>&) throw();
   ~allocator() throw();

    pointer address(reference x) const;
    const_pointer address(reference x) const;
    const_pointer address(const_reference x) const;
    pointer allocate(
      size_type, allocator<void>::const_pointer hint = 0);
    void deallocate(pointer p, size_type n);
    size_type max_size() const throw();

    void construct(pointer p, const T& val);
    void destroy(pointer p);
  };

  template <class T1, class T2>
    bool operator==(const allocator<T1>&, const allocator<T2>&) throw();
  template <class T1, class T2>
    bool operator!=(const allocator<T1>&, const allocator<T2>&) throw();
}

Note that the size_type, difference_type, pointer and const_pointer
typedefs are required to be specific types for std::allocator<>, whereas
the general Allocator requirements are more liberal.
Another difference is that general Allocators are only required to
support '==' and '!=' for allocators of the same value_type, and it's
not specified whether they're global functions or member functions.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Niklas Pettersson" <npedt97@student.vxu.se (nospam)>
Date: 2000/04/01
Raw View
Can you implement a garbage collector for C++ using alloctators?

/ Niklas


"suj_h" <suj_h@microsoft.com> wrote in message
news:01bf9a2b$639f51c0$e5a7490c@aristo22...
> If you look at the header file for std::list, it is defined as follow.
> What's the purpose of allocator? In what occasion do you need it?
>
> template<class T, class A = allocator<T> >
> class list
>

> public:
> typedef A allocator_type; typedef A::size_type size_type; typedef
> A::difference_type difference_type;
> ...
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "suj_h" <suj_h@microsoft.com>
Date: 2000/03/30
Raw View
If you look at the header file for std::list, it is defined as follow.
What's the purpose of allocator? In what occasion do you need it?

template<class T, class A = allocator<T> >
class list
{
public:
typedef A allocator_type; typedef A::size_type size_type; typedef
A::difference_type difference_type;
...

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]