Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

allocator



Summary

The default allocator object for storage management in Standard Library containers.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <memory>
template <class T>
class allocator;

Description

Containers in the Standard Library allow you control of storage management through the use of allocator objects. Each container has an allocator template parameter specifying the type of allocator to be used. Every constructor, except the copy constructor, has an allocator parameter, allowing you to pass in a specific allocator. A container uses that allocator for all storage management.

The library has a default allocator, called allocator. This allocator uses the global new and delete operators. By default, all containers use this allocator. You can also design your own allocator, but if you do so it must have an appropriate interface. The standard interface and an alternate interface are specified below. The alternate interface works on all supported compilers.

The Alternate Allocator

As of this writing, very few compilers support the full range of features needed by the standard allocator. If your compiler does not support member templates, both classes and functions, then you must use the alternate allocator interface. This alternate interface requires no special features of a compiler and offers most of the functionality of the standard allocator interface. The only thing missing is the ability to use special pointer and reference types. The alternate allocator fixes these as T* and T&. If your compiler supports partial specialization, then even this restriction is removed.

From outside a container, use of the alternate allocator is transparent. Simply pass the allocator as a template or function parameter exactly as you would pass the standard allocator.

Within a container, the alternate allocator interface is more complicated to use because it requires two separate classes, rather than one class with another class nested inside. If you plan to write your own containers and need to use the alternate allocator interface, we recommend that you support the default interface as well, since that is the only way to ensure long-term portability. See the User's Guide section on building containers for an explanation of how to support both the standard and the alternate allocator interfaces.

A generic allocator must be able to allocate space for objects of arbitrary type, and it must be able to construct those objects on that space. For this reason, the allocator must be type aware, but it must be aware on any arbitrary number of different types, since there is no way to predict the storage needs of any given container.

Consider an ordinary template. Although you may be able to instantiate on any fixed number of types, the resulting object is aware of only those types and any other types that can be built up from them (T*, for instance), as well as any types you specify up front. This won't work for an allocator, because you can't make any assumptions about the types a container needs to construct. It may well need to construct Ts (or it may not), but it may also need to allocate node objects and other data structures necessary to manage the contents of the container. Clearly there is no way to predict what an arbitrary container might need to construct. As with everything else within the Standard Library, it is absolutely essential to be fully generic.

The Standard allocator interface solves the problem with member templates. The precise type you are going to construct is not specified when you create an allocator, but when you actually go to allocate space or construct an object on existing space.

The alternate allocator interface uses a different technique. The alternate interface breaks the allocator into two pieces: an interface and an implementation. The interface is a template class containing a pointer to an implementation. The implementation is a simple class providing raw un-typed storage. Anything can be constructed on it. The interface template types the raw storage based on the template parameter. Only the implementation object is passed into a container. The container constructs interface objects as necessary, using the implementation to manage the storage of data.

Since all interface objects use the one copy of the implementation object to allocate space, that one implementation object manages all storage acquisition for the container. The container makes calls to the allocator_interface objects in the same way it would make calls to a standard allocator object.

For example, if your container needs to allocate T objects and node objects, you need to have two allocator_interface objects in your container:

You then use the value_allocator for all allocation, construction, etc. of values (Ts), and use the node_allocator object to allocate and deallocate nodes.

The only significant drawback is the lack of special pointer types and the inability to alter the behavior of the construct and destroy functions, since these must reside in the interface class. If your compiler has partial specialization, then this restriction goes away, since you can provide specialized interfaces along with your implementation.

Standard Interface

Types

size_type
difference_type
pointer
const_pointer
reference
const_reference
value_type
template <class U> struct rebind;

Constructors

allocator()
template <class U> 
allocator(const allocator<U>&) 

Destructors

~allocator()

Member Functions

pointer
address(reference x) const;
const_pointer 
address(const_reference x) const;
pointer 
allocate(size_type n, 
         typename allocator<void>::const_pointer p = 0)
void 
deallocate(pointer p, size_type n)
size_type 
max_size() const;
void 
construct(pointer p, const T& val);
void 
destroy(pointer p)

Alternate Interface

Alternate Allocator Description

The description for the operations of allocator_interface<T> are generally the same as for corresponding operations of the standard allocator. The exception is that allocator_interface members allocate and deallocate call respective functions in allocator, which are in turn implemented like the standard allocator functions.

See the container section of the Class Reference for a further description of how to use the alternate allocator within a user-defined container.

See Also

Containers



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998