1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * Copyright (c) 1996-1997
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
43 #ifndef _ALLOCATOR_TRAITS_H
44 #define _ALLOCATOR_TRAITS_H 1
52 * This is used primarily (only?) in _Alloc_traits and other places to
53 * help provide the _Alloc_type typedef. All it does is forward the
54 * requests after some minimal checking.
56 * This is neither "standard"-conforming nor "SGI". The _Alloc parameter
57 * must be "SGI" style.
59 * (See @link Allocators allocators info @endlink for more.)
61 template<typename _Tp, typename _Alloc>
70 __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
76 { return (_Tp*) _Alloc::allocate(sizeof (_Tp)); }
79 deallocate(_Tp* __p, size_t __n)
80 { if (0 != __n) _Alloc::deallocate(__p, __n * sizeof (_Tp)); }
84 { _Alloc::deallocate(__p, sizeof (_Tp)); }
90 * Allocator adaptor to turn an "SGI" style allocator (e.g.,
91 * __alloc, __malloc_alloc) into a "standard" conforming
92 * allocator. Note that this adaptor does *not* assume that all
93 * objects of the underlying alloc class are identical, nor does it
94 * assume that all of the underlying alloc's member functions are
95 * static member functions. Note, also, that __allocator<_Tp,
96 * __alloc> is essentially the same thing as allocator<_Tp>.
98 * (See @link Allocators allocators info @endlink for more.)
100 template<typename _Tp, typename _Alloc>
103 _Alloc __underlying_alloc;
105 typedef size_t size_type;
106 typedef ptrdiff_t difference_type;
107 typedef _Tp* pointer;
108 typedef const _Tp* const_pointer;
109 typedef _Tp& reference;
110 typedef const _Tp& const_reference;
111 typedef _Tp value_type;
113 template<typename _Tp1>
115 { typedef __allocator<_Tp1, _Alloc> other; };
117 __allocator() throw() { }
119 __allocator(const __allocator& __a) throw()
120 : __underlying_alloc(__a.__underlying_alloc) { }
122 template<typename _Tp1>
123 __allocator(const __allocator<_Tp1, _Alloc>& __a) throw()
124 : __underlying_alloc(__a.__underlying_alloc) { }
126 ~__allocator() throw() { }
129 address(reference __x) const { return &__x; }
132 address(const_reference __x) const { return &__x; }
134 // NB: __n is permitted to be 0. The C++ standard says nothing
135 // about what the return value is when __n == 0.
137 allocate(size_type __n, const void* = 0)
141 __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
145 // __p is not permitted to be a null pointer.
147 deallocate(pointer __p, size_type __n)
148 { __underlying_alloc.deallocate(__p, __n * sizeof(_Tp)); }
151 max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // 402. wrong new expression in [some_]allocator::construct
156 construct(pointer __p, const _Tp& __val) { ::new(__p) _Tp(__val); }
159 destroy(pointer __p) { __p->~_Tp(); }
162 template<typename _Alloc>
163 struct __allocator<void, _Alloc>
165 typedef size_t size_type;
166 typedef ptrdiff_t difference_type;
167 typedef void* pointer;
168 typedef const void* const_pointer;
169 typedef void value_type;
171 template<typename _Tp1>
173 { typedef __allocator<_Tp1, _Alloc> other; };
176 template<typename _Tp, typename _Alloc>
178 operator==(const __allocator<_Tp,_Alloc>& __a1,
179 const __allocator<_Tp,_Alloc>& __a2)
180 { return __a1.__underlying_alloc == __a2.__underlying_alloc; }
182 template<typename _Tp, typename _Alloc>
184 operator!=(const __allocator<_Tp, _Alloc>& __a1,
185 const __allocator<_Tp, _Alloc>& __a2)
186 { return __a1.__underlying_alloc != __a2.__underlying_alloc; }
191 * Another allocator adaptor: _Alloc_traits. This serves two purposes.
192 * First, make it possible to write containers that can use either "SGI"
193 * style allocators or "standard" allocators. Second, provide a mechanism
194 * so that containers can query whether or not the allocator has distinct
195 * instances. If not, the container can avoid wasting a word of memory to
196 * store an empty object. For examples of use, see stl_vector.h, etc, or
197 * any of the other classes derived from this one.
199 * This adaptor uses partial specialization. The general case of
200 * _Alloc_traits<_Tp, _Alloc> assumes that _Alloc is a
201 * standard-conforming allocator, possibly with non-equal instances and
202 * non-static members. (It still behaves correctly even if _Alloc has
203 * static member and if all instances are equal. Refinements affect
204 * performance, not correctness.)
206 * There are always two members: allocator_type, which is a standard-
207 * conforming allocator type for allocating objects of type _Tp, and
208 * _S_instanceless, a static const member of type bool. If
209 * _S_instanceless is true, this means that there is no difference
210 * between any two instances of type allocator_type. Furthermore, if
211 * _S_instanceless is true, then _Alloc_traits has one additional
212 * member: _Alloc_type. This type encapsulates allocation and
213 * deallocation of objects of type _Tp through a static interface; it
214 * has two member functions, whose signatures are
216 * - static _Tp* allocate(size_t)
217 * - static void deallocate(_Tp*, size_t)
219 * The size_t parameters are "standard" style (see top of
220 * allocator.h) in that they take counts, not sizes.
223 * (See @link Allocators allocators info @endlink for more.)
225 // The fully general version.
226 template<typename _Tp, typename _Allocator>
229 static const bool _S_instanceless = false;
230 typedef typename _Allocator::template rebind<_Tp>::other allocator_type;
233 template<typename _Tp, typename _Allocator>
234 const bool _Alloc_traits<_Tp, _Allocator>::_S_instanceless;