1132720Skan// Allocators -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4169691Skan// Free Software Foundation, Inc.
5132720Skan//
6132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
7132720Skan// software; you can redistribute it and/or modify it under the
8132720Skan// terms of the GNU General Public License as published by the
9132720Skan// Free Software Foundation; either version 2, or (at your option)
10132720Skan// any later version.
11132720Skan
12132720Skan// This library is distributed in the hope that it will be useful,
13132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132720Skan// GNU General Public License for more details.
16132720Skan
17132720Skan// You should have received a copy of the GNU General Public License along
18132720Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20132720Skan// USA.
21132720Skan
22132720Skan// As a special exception, you may use this file as part of a free software
23132720Skan// library without restriction.  Specifically, if other files instantiate
24132720Skan// templates or use macros or inline functions from this file, or you compile
25132720Skan// this file and link it with other files to produce an executable, this
26132720Skan// file does not by itself cause the resulting executable to be covered by
27132720Skan// the GNU General Public License.  This exception does not however
28132720Skan// invalidate any other reasons why the executable file might be covered by
29132720Skan// the GNU General Public License.
30132720Skan
31132720Skan/*
32132720Skan * Copyright (c) 1996-1997
33132720Skan * Silicon Graphics Computer Systems, Inc.
34132720Skan *
35132720Skan * Permission to use, copy, modify, distribute and sell this software
36132720Skan * and its documentation for any purpose is hereby granted without fee,
37132720Skan * provided that the above copyright notice appear in all copies and
38132720Skan * that both that copyright notice and this permission notice appear
39132720Skan * in supporting documentation.  Silicon Graphics makes no
40132720Skan * representations about the suitability of this software for any
41132720Skan * purpose.  It is provided "as is" without express or implied warranty.
42132720Skan */
43132720Skan
44132720Skan/** @file allocator.h
45132720Skan *  This is an internal header file, included by other library headers.
46132720Skan *  You should not attempt to use it directly.
47132720Skan */
48132720Skan
49132720Skan#ifndef _ALLOCATOR_H
50132720Skan#define _ALLOCATOR_H 1
51132720Skan
52132720Skan// Define the base class to std::allocator.
53132720Skan#include <bits/c++allocator.h>
54132720Skan
55169691Skan#include <bits/cpp_type_traits.h> // for __is_empty
56169691Skan
57169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
58169691Skan
59132720Skan  template<typename _Tp>
60132720Skan    class allocator;
61132720Skan
62169691Skan  /// allocator<void> specialization.
63132720Skan  template<>
64132720Skan    class allocator<void>
65132720Skan    {
66132720Skan    public:
67132720Skan      typedef size_t      size_type;
68132720Skan      typedef ptrdiff_t   difference_type;
69132720Skan      typedef void*       pointer;
70132720Skan      typedef const void* const_pointer;
71132720Skan      typedef void        value_type;
72132720Skan
73132720Skan      template<typename _Tp1>
74132720Skan        struct rebind
75132720Skan        { typedef allocator<_Tp1> other; };
76132720Skan    };
77132720Skan
78132720Skan  /**
79169691Skan   * @brief  The "standard" allocator, as per [20.4].
80132720Skan   *
81169691Skan   *  Further details:
82169691Skan   *  http://gcc.gnu.org/onlinedocs/libstdc++/20_util/allocator.html
83132720Skan   */
84132720Skan  template<typename _Tp>
85169691Skan    class allocator: public __glibcxx_base_allocator<_Tp>
86132720Skan    {
87132720Skan   public:
88132720Skan      typedef size_t     size_type;
89132720Skan      typedef ptrdiff_t  difference_type;
90132720Skan      typedef _Tp*       pointer;
91132720Skan      typedef const _Tp* const_pointer;
92132720Skan      typedef _Tp&       reference;
93132720Skan      typedef const _Tp& const_reference;
94132720Skan      typedef _Tp        value_type;
95132720Skan
96132720Skan      template<typename _Tp1>
97132720Skan        struct rebind
98132720Skan        { typedef allocator<_Tp1> other; };
99132720Skan
100132720Skan      allocator() throw() { }
101132720Skan
102169691Skan      allocator(const allocator& __a) throw()
103169691Skan      : __glibcxx_base_allocator<_Tp>(__a) { }
104132720Skan
105132720Skan      template<typename _Tp1>
106132720Skan        allocator(const allocator<_Tp1>&) throw() { }
107132720Skan
108132720Skan      ~allocator() throw() { }
109132720Skan
110132720Skan      // Inherit everything else.
111132720Skan    };
112132720Skan
113132720Skan  template<typename _T1, typename _T2>
114132720Skan    inline bool
115132720Skan    operator==(const allocator<_T1>&, const allocator<_T2>&)
116132720Skan    { return true; }
117132720Skan
118228780Spfg  template<typename _Tp>
119228780Spfg    inline bool
120228780Spfg    operator==(const allocator<_Tp>&, const allocator<_Tp>&)
121228780Spfg    { return true; }
122228780Spfg
123132720Skan  template<typename _T1, typename _T2>
124132720Skan    inline bool
125132720Skan    operator!=(const allocator<_T1>&, const allocator<_T2>&)
126132720Skan    { return false; }
127132720Skan
128228780Spfg  template<typename _Tp>
129228780Spfg    inline bool
130228780Spfg    operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
131228780Spfg    { return false; }
132228780Spfg
133132720Skan  // Inhibit implicit instantiations for required instantiations,
134132720Skan  // which are defined via explicit instantiations elsewhere.
135132720Skan  // NB: This syntax is a GNU extension.
136132720Skan#if _GLIBCXX_EXTERN_TEMPLATE
137132720Skan  extern template class allocator<char>;
138132720Skan  extern template class allocator<wchar_t>;
139132720Skan#endif
140132720Skan
141132720Skan  // Undefine.
142169691Skan#undef __glibcxx_base_allocator
143132720Skan
144169691Skan  // To implement Option 3 of DR 431.
145169691Skan  template<typename _Alloc, bool = std::__is_empty<_Alloc>::__value>
146169691Skan    struct __alloc_swap
147169691Skan    { static void _S_do_it(_Alloc&, _Alloc&) { } };
148169691Skan
149169691Skan  template<typename _Alloc>
150169691Skan    struct __alloc_swap<_Alloc, false>
151169691Skan    {
152169691Skan      static void
153169691Skan      _S_do_it(_Alloc& __one, _Alloc& __two)
154169691Skan      {
155169691Skan	// Precondition: swappable allocators.
156169691Skan	if (__one != __two)
157169691Skan	  swap(__one, __two);
158169691Skan      }
159169691Skan    };
160169691Skan
161169691Skan_GLIBCXX_END_NAMESPACE
162169691Skan
163132720Skan#endif
164