libstdc++
malloc_allocator.h
Go to the documentation of this file.
1 // Allocator that wraps "C" malloc -*- C++ -*-
2 
3 // Copyright (C) 2001-2014 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file ext/malloc_allocator.h
26  * This file is a GNU extension to the Standard C++ Library.
27  */
28 
29 #ifndef _MALLOC_ALLOCATOR_H
30 #define _MALLOC_ALLOCATOR_H 1
31 
32 #include <cstdlib>
33 #include <new>
34 #include <bits/functexcept.h>
35 #include <bits/move.h>
36 #if __cplusplus >= 201103L
37 #include <type_traits>
38 #endif
39 
40 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44  using std::size_t;
45  using std::ptrdiff_t;
46 
47  /**
48  * @brief An allocator that uses malloc.
49  * @ingroup allocators
50  *
51  * This is precisely the allocator defined in the C++ Standard.
52  * - all allocation calls malloc
53  * - all deallocation calls free
54  */
55  template<typename _Tp>
56  class malloc_allocator
57  {
58  public:
59  typedef size_t size_type;
60  typedef ptrdiff_t difference_type;
61  typedef _Tp* pointer;
62  typedef const _Tp* const_pointer;
63  typedef _Tp& reference;
64  typedef const _Tp& const_reference;
65  typedef _Tp value_type;
66 
67  template<typename _Tp1>
68  struct rebind
69  { typedef malloc_allocator<_Tp1> other; };
70 
71 #if __cplusplus >= 201103L
72  // _GLIBCXX_RESOLVE_LIB_DEFECTS
73  // 2103. propagate_on_container_move_assignment
74  typedef std::true_type propagate_on_container_move_assignment;
75 #endif
76 
77  malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
78 
79  malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
80 
81  template<typename _Tp1>
82  malloc_allocator(const malloc_allocator<_Tp1>&)
83  _GLIBCXX_USE_NOEXCEPT { }
84 
85  ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
86 
87  pointer
88  address(reference __x) const _GLIBCXX_NOEXCEPT
89  { return std::__addressof(__x); }
90 
91  const_pointer
92  address(const_reference __x) const _GLIBCXX_NOEXCEPT
93  { return std::__addressof(__x); }
94 
95  // NB: __n is permitted to be 0. The C++ standard says nothing
96  // about what the return value is when __n == 0.
97  pointer
98  allocate(size_type __n, const void* = 0)
99  {
100  if (__n > this->max_size())
101  std::__throw_bad_alloc();
102 
103  pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
104  if (!__ret)
105  std::__throw_bad_alloc();
106  return __ret;
107  }
108 
109  // __p is not permitted to be a null pointer.
110  void
111  deallocate(pointer __p, size_type)
112  { std::free(static_cast<void*>(__p)); }
113 
114  size_type
115  max_size() const _GLIBCXX_USE_NOEXCEPT
116  { return size_t(-1) / sizeof(_Tp); }
117 
118 #if __cplusplus >= 201103L
119  template<typename _Up, typename... _Args>
120  void
121  construct(_Up* __p, _Args&&... __args)
122  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
123 
124  template<typename _Up>
125  void
126  destroy(_Up* __p) { __p->~_Up(); }
127 #else
128  // _GLIBCXX_RESOLVE_LIB_DEFECTS
129  // 402. wrong new expression in [some_] allocator::construct
130  void
131  construct(pointer __p, const _Tp& __val)
132  { ::new((void *)__p) value_type(__val); }
133 
134  void
135  destroy(pointer __p) { __p->~_Tp(); }
136 #endif
137  };
138 
139  template<typename _Tp>
140  inline bool
141  operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
142  { return true; }
143 
144  template<typename _Tp>
145  inline bool
146  operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
147  { return false; }
148 
149 _GLIBCXX_END_NAMESPACE_VERSION
150 } // namespace
151 
152 #endif
integral_constant
Definition: type_traits:57
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:76
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47