OSDN Git Service

93bae7a2d44236131b892e58ca403e59c6f9fc26
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / allocator_traits.h
1 // Allocators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 /*
31  * Copyright (c) 1996-1997
32  * Silicon Graphics Computer Systems, Inc.
33  *
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.
41  */
42
43 #ifndef _ALLOCATOR_TRAITS_H
44 #define _ALLOCATOR_TRAITS_H 1
45
46 #include <cstddef>
47
48 namespace std
49 {
50   /**
51    *  @if maint
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.
55    *
56    *  This is neither "standard"-conforming nor "SGI".  The _Alloc parameter
57    *  must be "SGI" style.
58    *  @endif
59    *  (See @link Allocators allocators info @endlink for more.)
60    */
61   template<typename _Tp, typename _Alloc>
62     class __simple_alloc
63     {
64     public:
65       static _Tp*
66       allocate(size_t __n)
67       {
68         _Tp* __ret = 0;
69         if (__n)
70           __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
71         return __ret;
72       }
73   
74       static _Tp*
75       allocate()
76       { return (_Tp*) _Alloc::allocate(sizeof (_Tp)); }
77   
78       static void
79       deallocate(_Tp* __p, size_t __n)
80       { if (0 != __n) _Alloc::deallocate(__p, __n * sizeof (_Tp)); }
81   
82       static void
83       deallocate(_Tp* __p)
84       { _Alloc::deallocate(__p, sizeof (_Tp)); }
85     };
86
87
88   /**
89    *  @if maint
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>.
97    *  @endif
98    *  (See @link Allocators allocators info @endlink for more.)
99    */
100   template<typename _Tp, typename _Alloc>
101     struct __allocator
102     {
103       _Alloc __underlying_alloc;
104       
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;
112
113       template<typename _Tp1>
114         struct rebind
115         { typedef __allocator<_Tp1, _Alloc> other; };
116
117       __allocator() throw() { }
118
119       __allocator(const __allocator& __a) throw()
120       : __underlying_alloc(__a.__underlying_alloc) { }
121
122       template<typename _Tp1>
123         __allocator(const __allocator<_Tp1, _Alloc>& __a) throw()
124         : __underlying_alloc(__a.__underlying_alloc) { }
125
126       ~__allocator() throw() { }
127
128       pointer
129       address(reference __x) const { return &__x; }
130
131       const_pointer
132       address(const_reference __x) const { return &__x; }
133
134       // NB: __n is permitted to be 0.  The C++ standard says nothing
135       // about what the return value is when __n == 0.
136       _Tp*
137       allocate(size_type __n, const void* = 0)
138       {
139         _Tp* __ret = 0;
140         if (__n)
141           __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp)));
142         return __ret;
143       }
144
145       // __p is not permitted to be a null pointer.
146       void
147       deallocate(pointer __p, size_type __n)
148       { __underlying_alloc.deallocate(__p, __n * sizeof(_Tp)); }
149       
150       size_type
151       max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
152
153       // _GLIBCXX_RESOLVE_LIB_DEFECTS
154       // 402. wrong new expression in [some_]allocator::construct
155       void
156       construct(pointer __p, const _Tp& __val) { ::new(__p) _Tp(__val); }
157       
158       void
159       destroy(pointer __p) { __p->~_Tp(); }
160     };
161
162   template<typename _Alloc>
163     struct __allocator<void, _Alloc>
164     {
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;
170
171       template<typename _Tp1>
172         struct rebind
173         { typedef __allocator<_Tp1, _Alloc> other; };
174     };
175
176   template<typename _Tp, typename _Alloc>
177     inline bool
178     operator==(const __allocator<_Tp,_Alloc>& __a1, 
179                const __allocator<_Tp,_Alloc>& __a2)
180     { return __a1.__underlying_alloc == __a2.__underlying_alloc; }
181
182   template<typename _Tp, typename _Alloc>
183     inline bool
184     operator!=(const __allocator<_Tp, _Alloc>& __a1,
185                const __allocator<_Tp, _Alloc>& __a2)
186     { return __a1.__underlying_alloc != __a2.__underlying_alloc; }
187
188
189   /**
190    *  @if maint
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.
198    *
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.)
205    *
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
215    *
216    *  -  static _Tp* allocate(size_t)
217    *  -  static void deallocate(_Tp*, size_t)
218    *
219    *  The size_t parameters are "standard" style (see top of
220    *  allocator.h) in that they take counts, not sizes.
221    *
222    *  @endif
223    *  (See @link Allocators allocators info @endlink for more.)
224    */
225   // The fully general version.
226   template<typename _Tp, typename _Allocator>
227     struct _Alloc_traits
228     {
229       static const bool _S_instanceless = false;
230       typedef typename _Allocator::template rebind<_Tp>::other allocator_type;
231     };
232
233   template<typename _Tp, typename _Allocator>
234     const bool _Alloc_traits<_Tp, _Allocator>::_S_instanceless;
235 } // namespace std
236
237 #endif