.\" ident @(#)allocator.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH allocator 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2allocator\fP \ - The default allocator object for storage management in Standard Library containers. .SH SYNOPSIS .br #include .br template .br class allocator; .SH 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 \f2allocator\fP. This allocator uses the global \f2new\fP and \f2delete\fP 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. .SH 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 \f2T* \fPand \f2T&\fP. 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 (\f2T*\fP, 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 \f2T\fPs (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 \f2T\fP objects and node objects, you need to have two allocator_interface objects in your container: \f2allocator_interface value_allocator; allocator_interface node_allocator;\fP You then use the \f2value_allocator\fP for all allocation, construction, etc. of values (\f2T\fPs), and use the \f2node_allocator\fP 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 \f2construct\fP and \f2destroy\fP 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. .SH STANDARD INTERFACE .br template .br class allocator { .RE .RS 1 typedef size_t size_type; .br typedef ptrdiff_t difference_type; .br typedef T* pointer; .br typedef const T* const_pointer; .br typedef T& reference; .br typedef const T& const_reference; .br typedef T value_type; .RE .RS 0 .RE .RS 1 template struct rebind { .RE .RS 3 typedef allocator other; .RE .RS 2 }; .RE .RS 1 allocator () throw(); .br allocator (const allocator&) throw (); .br template allocator(const allocator&) throw(); .br template .RE .RS 3 allocator& operator=(const allocator&) throw(); .RE .RS 2 ~allocator () throw(); .RE .RS 1 pointer address (reference) const; .br const_pointer address (const_reference) const; .br pointer allocate (size_type, .RE .RS 4 typename allocator::const_pointer = 0); .RE .RS 1 void deallocate(pointer p, size_type n); .br size_type max_size () const; .br void construct (pointer, const T&); .br void destroy (pointer); .RE .RS 0 }; .br // specialize for void: .RE .RS 1 template <> class allocator { .br public: .RE .RS 0 .RE .RS 3 typedef void* pointer; .br typedef const void* const_pointer; .RE .RS 4 // reference-to-void members are impossible. .RE .RS 3 typedef void value_type; .br template .RE .RS 5 struct rebind { typedef allocator other; }; .RE .RS 2 }; .RE .RS 0 .br // globals .br template .RE .RS 1 bool operator==(const allocator&, .RE .RS 17 const allocator&) throw(); .RE .RS 0 template .RE .RS 1 bool operator!=(const allocator&, .RE .RS 17 const allocator&) throw(); .SH TYPES .RE .RS 0 size_type .RE .RS 3 Type used to hold the size of an allocated block of storage. .RE .br difference_type .RE .RS 3 Type used to hold values representing distances between storage addresses. .RE .br pointer .RE .RS 3 Type of pointer returned by allocator. .RE .br const_pointer .RE .RS 3 \f2Const\fP version of \f2pointer\fP. .RE .br reference .RE .RS 3 Type of reference to allocated objects. .RE .br const_reference .RE .RS 3 \f2Const\fP version of \f2reference\fP. .RE .br value_type .RE .RS 3 Type of allocated object. .RE .br template struct rebind; .RE .RS 3 Converts an allocator templatized on one type to an allocator templatized on another type. This struct contains a single type member: \f2typedef allocator other\fP .RE .SH CONSTRUCTORS .br allocator() .RE .RS 3 Default constructor. .RE .br template .br allocator(const allocator&) .RE .RS 3 Copy constructor. .RE .SH DESTRUCTORS .br ~allocator() .RE .RS 3 Destructor. .RE .SH MEMBER FUNCTIONS .br pointer .br address(reference x) const; .RE .RS 3 Returns the address of the reference \f2x\fP as a pointer. .RE .br const_pointer .br address(const_reference x) const; .RE .RS 3 Returns the address of the reference \f2x\fP as a \f2const_pointer\fP. .RE .br pointer .br allocate(size_type n, .RE .RS 8 typename allocator::const_pointer p = 0) .RE .RS 3 Allocates storage. Returns a pointer to the first element in a block of storage \f2n*sizeof(T)\fP bytes in size. The block is aligned appropriately for objects of type \f2T\fP. Throws the exception \f2bad_alloc\fP if the storage is unavailable. This function uses operator \f2new(size_t)\fP. The second parameter \f2p\fP can be used by an allocator to localize memory allocation, but the default allocator does not use it. .RE .RE .RS 0 void .br deallocate(pointer p, size_type n) .RE .RS 3 Deallocates the storage obtained by a call to \f2allocate\fP with arguments \f2n\fP and \f2p\fP. .RE .br size_type .br max_size() const; .RE .RS 3 Returns the largest size for which a call to \f2allocate\fP might succeed. .RE .br void .br construct(pointer p, const T& val); .RE .RS 3 Constructs an object of type \f2T2\fP with the initial value of \f2val\fP at the location specified by \f2p\fP. This function calls the \f2placement new\fP operator. .RE .br void .br destroy(pointer p) .RE .RS 3 Calls the destructor on the object pointed to by \f2p\fP, but does not delete. .RE .SH ALTERNATE INTERFACE .br class allocator .br { .br public: .br typedef size_t size_type ; .br typedef ptrdiff_t difference_type ; .br allocator (); .RE .RS 2 ~allocator (); .RE .RS 0 void * allocate (size_type, void * = 0); .br void deallocate (void*); .br }; .br template .br class allocator_interface . .br { .RE .RS 2 public: .br typedef Allocator allocator_type ; .br typedef T* pointer ; . .br typedef const T* const_pointer ; .br typedef T& reference ; . .br typedef const T& const_reference ; .br typedef T value_type ; . .br typedef typename Allocator::size_type size_type ; .br typedef typename Allocator::size_type difference_type ; .RE .RS 0 .br protected: .RE .RS 2 allocator_type* alloc_; .RE .RS 0 .br public: .RE .RS 2 allocator_interface (); .br allocator_interface (Allocator*); .br pointer address (T& x); .br size_type max_size () const; .br pointer allocate (size_type, pointer = 0); .br void deallocate (pointer); .br void construct (pointer, const T&); .br void destroy (T*); .RE .RS 0 }; .br .br // .br // Specialization .br // .br class allocator_interface .RE .RS 1 { .RE .RS 0 typedef void* pointer ; .br typedef const void* const_pointer ; .RE .RS 1 }; .SH ALTERNATE ALLOCATOR DESCRIPTION The description for the operations of allocator_interface are generally the same as for corresponding operations of the standard allocator. The exception is that allocator_interface members \f2allocate\fP and \f2deallocate\fP 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. .SH SEE ALSO Containers