allocator.h revision 259065
1214117Sjamie// Allocators -*- C++ -*-
2223190Sjamie
3214117Sjamie// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4214117Sjamie// Free Software Foundation, Inc.
5214117Sjamie//
6214117Sjamie// This file is part of the GNU ISO C++ Library.  This library is free
7214117Sjamie// software; you can redistribute it and/or modify it under the
8214117Sjamie// terms of the GNU General Public License as published by the
9214117Sjamie// Free Software Foundation; either version 2, or (at your option)
10214117Sjamie// any later version.
11214117Sjamie
12214117Sjamie// This library is distributed in the hope that it will be useful,
13214117Sjamie// but WITHOUT ANY WARRANTY; without even the implied warranty of
14214117Sjamie// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15214117Sjamie// GNU General Public License for more details.
16214117Sjamie
17214117Sjamie// You should have received a copy of the GNU General Public License along
18214117Sjamie// with this library; see the file COPYING.  If not, write to the Free
19214117Sjamie// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20214117Sjamie// USA.
21214117Sjamie
22214117Sjamie// As a special exception, you may use this file as part of a free software
23214117Sjamie// library without restriction.  Specifically, if other files instantiate
24214117Sjamie// templates or use macros or inline functions from this file, or you compile
25214117Sjamie// this file and link it with other files to produce an executable, this
26214117Sjamie// file does not by itself cause the resulting executable to be covered by
27214117Sjamie// the GNU General Public License.  This exception does not however
28214117Sjamie// invalidate any other reasons why the executable file might be covered by
29214117Sjamie// the GNU General Public License.
30214117Sjamie
31214117Sjamie/*
32214117Sjamie * Copyright (c) 1996-1997
33214117Sjamie * Silicon Graphics Computer Systems, Inc.
34214117Sjamie *
35214117Sjamie * Permission to use, copy, modify, distribute and sell this software
36214117Sjamie * and its documentation for any purpose is hereby granted without fee,
37214117Sjamie * provided that the above copyright notice appear in all copies and
38214117Sjamie * that both that copyright notice and this permission notice appear
39214117Sjamie * in supporting documentation.  Silicon Graphics makes no
40214117Sjamie * representations about the suitability of this software for any
41214117Sjamie * purpose.  It is provided "as is" without express or implied warranty.
42214117Sjamie */
43214117Sjamie
44214117Sjamie/** @file allocator.h
45214117Sjamie *  This is an internal header file, included by other library headers.
46214117Sjamie *  You should not attempt to use it directly.
47214117Sjamie */
48214117Sjamie
49214117Sjamie#ifndef _ALLOCATOR_H
50214117Sjamie#define _ALLOCATOR_H 1
51214117Sjamie
52214423Sjamie// Define the base class to std::allocator.
53248854Sjamie#include <bits/c++allocator.h>
54214117Sjamie
55214117Sjamie#include <bits/cpp_type_traits.h> // for __is_empty
56214117Sjamie
57214117Sjamie_GLIBCXX_BEGIN_NAMESPACE(std)
58214117Sjamie
59214117Sjamie  template<typename _Tp>
60214117Sjamie    class allocator;
61214649Sjamie
62214649Sjamie  /// allocator<void> specialization.
63223189Sjamie  template<>
64223189Sjamie    class allocator<void>
65223189Sjamie    {
66214117Sjamie    public:
67214117Sjamie      typedef size_t      size_type;
68214117Sjamie      typedef ptrdiff_t   difference_type;
69214117Sjamie      typedef void*       pointer;
70214117Sjamie      typedef const void* const_pointer;
71214117Sjamie      typedef void        value_type;
72214117Sjamie
73214117Sjamie      template<typename _Tp1>
74214117Sjamie        struct rebind
75234988Sjamie        { typedef allocator<_Tp1> other; };
76234988Sjamie    };
77214117Sjamie
78214117Sjamie  /**
79214117Sjamie   * @brief  The "standard" allocator, as per [20.4].
80214117Sjamie   *
81214117Sjamie   *  Further details:
82214117Sjamie   *  http://gcc.gnu.org/onlinedocs/libstdc++/20_util/allocator.html
83214117Sjamie   */
84214117Sjamie  template<typename _Tp>
85214117Sjamie    class allocator: public __glibcxx_base_allocator<_Tp>
86214117Sjamie    {
87214117Sjamie   public:
88214117Sjamie      typedef size_t     size_type;
89214117Sjamie      typedef ptrdiff_t  difference_type;
90214117Sjamie      typedef _Tp*       pointer;
91214117Sjamie      typedef const _Tp* const_pointer;
92223351Sjamie      typedef _Tp&       reference;
93214117Sjamie      typedef const _Tp& const_reference;
94214117Sjamie      typedef _Tp        value_type;
95223351Sjamie
96214117Sjamie      template<typename _Tp1>
97214117Sjamie        struct rebind
98214423Sjamie        { typedef allocator<_Tp1> other; };
99214117Sjamie
100214423Sjamie      allocator() throw() { }
101223351Sjamie
102214117Sjamie      allocator(const allocator& __a) throw()
103223351Sjamie      : __glibcxx_base_allocator<_Tp>(__a) { }
104214117Sjamie
105214117Sjamie      template<typename _Tp1>
106214117Sjamie        allocator(const allocator<_Tp1>&) throw() { }
107214783Sjamie
108223189Sjamie      ~allocator() throw() { }
109214423Sjamie
110214423Sjamie      // Inherit everything else.
111214423Sjamie    };
112214423Sjamie
113214423Sjamie  template<typename _T1, typename _T2>
114214423Sjamie    inline bool
115232242Sjamie    operator==(const allocator<_T1>&, const allocator<_T2>&)
116214423Sjamie    { return true; }
117214423Sjamie
118223351Sjamie  template<typename _Tp>
119214117Sjamie    inline bool
120223351Sjamie    operator==(const allocator<_Tp>&, const allocator<_Tp>&)
121214117Sjamie    { return true; }
122214117Sjamie
123214117Sjamie  template<typename _T1, typename _T2>
124214117Sjamie    inline bool
125214117Sjamie    operator!=(const allocator<_T1>&, const allocator<_T2>&)
126214117Sjamie    { return false; }
127214117Sjamie
128214423Sjamie  template<typename _Tp>
129214117Sjamie    inline bool
130214117Sjamie    operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
131214117Sjamie    { return false; }
132214117Sjamie
133214117Sjamie  // Inhibit implicit instantiations for required instantiations,
134214117Sjamie  // which are defined via explicit instantiations elsewhere.
135214117Sjamie  // NB: This syntax is a GNU extension.
136214117Sjamie#if _GLIBCXX_EXTERN_TEMPLATE
137214117Sjamie  extern template class allocator<char>;
138214117Sjamie  extern template class allocator<wchar_t>;
139214117Sjamie#endif
140214117Sjamie
141223188Sjamie  // Undefine.
142214117Sjamie#undef __glibcxx_base_allocator
143214117Sjamie
144223188Sjamie  // To implement Option 3 of DR 431.
145214117Sjamie  template<typename _Alloc, bool = std::__is_empty<_Alloc>::__value>
146214117Sjamie    struct __alloc_swap
147214117Sjamie    { static void _S_do_it(_Alloc&, _Alloc&) { } };
148214117Sjamie
149214117Sjamie  template<typename _Alloc>
150214117Sjamie    struct __alloc_swap<_Alloc, false>
151214117Sjamie    {
152214117Sjamie      static void
153214117Sjamie      _S_do_it(_Alloc& __one, _Alloc& __two)
154214117Sjamie      {
155214117Sjamie	// Precondition: swappable allocators.
156214117Sjamie	if (__one != __two)
157214117Sjamie	  swap(__one, __two);
158214117Sjamie      }
159214117Sjamie    };
160214117Sjamie
161214117Sjamie_GLIBCXX_END_NAMESPACE
162214117Sjamie
163214117Sjamie#endif
164214117Sjamie