libstdc++
malloc_allocator.h
Go to the documentation of this file.
1 // Allocator that wraps "C" malloc -*- C++ -*-
2 
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 // 2010, 2011
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21 
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
26 
27 /** @file ext/malloc_allocator.h
28  * This file is a GNU extension to the Standard C++ Library.
29  */
30 
31 #ifndef _MALLOC_ALLOCATOR_H
32 #define _MALLOC_ALLOCATOR_H 1
33 
34 #include <cstdlib>
35 #include <new>
36 #include <bits/functexcept.h>
37 #include <bits/move.h>
38 
39 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  using std::size_t;
44  using std::ptrdiff_t;
45 
46  /**
47  * @brief An allocator that uses malloc.
48  * @ingroup allocators
49  *
50  * This is precisely the allocator defined in the C++ Standard.
51  * - all allocation calls malloc
52  * - all deallocation calls free
53  */
54  template<typename _Tp>
56  {
57  public:
58  typedef size_t size_type;
59  typedef ptrdiff_t difference_type;
60  typedef _Tp* pointer;
61  typedef const _Tp* const_pointer;
62  typedef _Tp& reference;
63  typedef const _Tp& const_reference;
64  typedef _Tp value_type;
65 
66  template<typename _Tp1>
67  struct rebind
68  { typedef malloc_allocator<_Tp1> other; };
69 
70  malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
71 
72  malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
73 
74  template<typename _Tp1>
76  _GLIBCXX_USE_NOEXCEPT { }
77 
78  ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
79 
80  pointer
81  address(reference __x) const _GLIBCXX_NOEXCEPT
82  { return std::__addressof(__x); }
83 
84  const_pointer
85  address(const_reference __x) const _GLIBCXX_NOEXCEPT
86  { return std::__addressof(__x); }
87 
88  // NB: __n is permitted to be 0. The C++ standard says nothing
89  // about what the return value is when __n == 0.
90  pointer
91  allocate(size_type __n, const void* = 0)
92  {
93  if (__n > this->max_size())
94  std::__throw_bad_alloc();
95 
96  pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
97  if (!__ret)
98  std::__throw_bad_alloc();
99  return __ret;
100  }
101 
102  // __p is not permitted to be a null pointer.
103  void
104  deallocate(pointer __p, size_type)
105  { std::free(static_cast<void*>(__p)); }
106 
107  size_type
108  max_size() const _GLIBCXX_USE_NOEXCEPT
109  { return size_t(-1) / sizeof(_Tp); }
110 
111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
112  template<typename _Up, typename... _Args>
113  void
114  construct(_Up* __p, _Args&&... __args)
115  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
116 
117  template<typename _Up>
118  void
119  destroy(_Up* __p) { __p->~_Up(); }
120 #else
121  // _GLIBCXX_RESOLVE_LIB_DEFECTS
122  // 402. wrong new expression in [some_] allocator::construct
123  void
124  construct(pointer __p, const _Tp& __val)
125  { ::new((void *)__p) value_type(__val); }
126 
127  void
128  destroy(pointer __p) { __p->~_Tp(); }
129 #endif
130  };
131 
132  template<typename _Tp>
133  inline bool
134  operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
135  { return true; }
136 
137  template<typename _Tp>
138  inline bool
139  operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
140  { return false; }
141 
142 _GLIBCXX_END_NAMESPACE_VERSION
143 } // namespace
144 
145 #endif