]>
Commit | Line | Data |
---|---|---|
d38d4e5d | 1 | // Allocator that wraps "C" malloc -*- C++ -*- |
1ff9402d | 2 | |
7d9cb054 PC |
3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, |
4 | // 2010, 2011 | |
113008b5 | 5 | // Free Software Foundation, Inc. |
1ff9402d BK |
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 | |
748086b7 | 10 | // Free Software Foundation; either version 3, or (at your option) |
1ff9402d BK |
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 | ||
748086b7 JJ |
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/>. | |
1ff9402d | 26 | |
0aa06b18 BK |
27 | /** @file ext/malloc_allocator.h |
28 | * This file is a GNU extension to the Standard C++ Library. | |
29 | */ | |
30 | ||
1ff9402d BK |
31 | #ifndef _MALLOC_ALLOCATOR_H |
32 | #define _MALLOC_ALLOCATOR_H 1 | |
33 | ||
4ec3604f | 34 | #include <cstdlib> |
d38d4e5d | 35 | #include <new> |
a063e891 | 36 | #include <bits/functexcept.h> |
ca0f8fd1 | 37 | #include <bits/move.h> |
1ff9402d | 38 | |
12ffa228 BK |
39 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
40 | { | |
41 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 42 | |
05a2763e MG |
43 | using std::size_t; |
44 | using std::ptrdiff_t; | |
45 | ||
1ff9402d | 46 | /** |
0aa06b18 | 47 | * @brief An allocator that uses malloc. |
5b9daa7e | 48 | * @ingroup allocators |
d38d4e5d BK |
49 | * |
50 | * This is precisely the allocator defined in the C++ Standard. | |
51 | * - all allocation calls malloc | |
52 | * - all deallocation calls free | |
1ff9402d | 53 | */ |
d38d4e5d BK |
54 | template<typename _Tp> |
55 | class malloc_allocator | |
1ff9402d | 56 | { |
1ff9402d | 57 | public: |
d38d4e5d BK |
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; | |
1ff9402d | 65 | |
d38d4e5d BK |
66 | template<typename _Tp1> |
67 | struct rebind | |
68 | { typedef malloc_allocator<_Tp1> other; }; | |
1ff9402d | 69 | |
7d9cb054 | 70 | malloc_allocator() _GLIBCXX_USE_NOEXCEPT { } |
1ff9402d | 71 | |
7d9cb054 | 72 | malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { } |
d38d4e5d BK |
73 | |
74 | template<typename _Tp1> | |
7d9cb054 PC |
75 | malloc_allocator(const malloc_allocator<_Tp1>&) |
76 | _GLIBCXX_USE_NOEXCEPT { } | |
d38d4e5d | 77 | |
7d9cb054 | 78 | ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { } |
d38d4e5d BK |
79 | |
80 | pointer | |
7d9cb054 PC |
81 | address(reference __x) const _GLIBCXX_NOEXCEPT |
82 | { return std::__addressof(__x); } | |
d38d4e5d BK |
83 | |
84 | const_pointer | |
7d9cb054 PC |
85 | address(const_reference __x) const _GLIBCXX_NOEXCEPT |
86 | { return std::__addressof(__x); } | |
d38d4e5d BK |
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 | |
90f8b692 | 91 | allocate(size_type __n, const void* = 0) |
58c95921 | 92 | { |
e762c6f4 | 93 | if (__n > this->max_size()) |
a063e891 PC |
94 | std::__throw_bad_alloc(); |
95 | ||
113008b5 | 96 | pointer __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp))); |
58c95921 | 97 | if (!__ret) |
a063e891 | 98 | std::__throw_bad_alloc(); |
58c95921 DM |
99 | return __ret; |
100 | } | |
d38d4e5d BK |
101 | |
102 | // __p is not permitted to be a null pointer. | |
103 | void | |
dcec0389 | 104 | deallocate(pointer __p, size_type) |
113008b5 | 105 | { std::free(static_cast<void*>(__p)); } |
d38d4e5d BK |
106 | |
107 | size_type | |
7d9cb054 | 108 | max_size() const _GLIBCXX_USE_NOEXCEPT |
d38d4e5d BK |
109 | { return size_t(-1) / sizeof(_Tp); } |
110 | ||
734f5023 | 111 | #if __cplusplus >= 201103L |
45ba8f9f JW |
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 | |
d38d4e5d BK |
121 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
122 | // 402. wrong new expression in [some_] allocator::construct | |
123 | void | |
124 | construct(pointer __p, const _Tp& __val) | |
61fcb9fb PC |
125 | { ::new((void *)__p) value_type(__val); } |
126 | ||
d38d4e5d BK |
127 | void |
128 | destroy(pointer __p) { __p->~_Tp(); } | |
45ba8f9f | 129 | #endif |
1ff9402d | 130 | }; |
f263b26e BK |
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; } | |
3cbc7af0 | 141 | |
12ffa228 BK |
142 | _GLIBCXX_END_NAMESPACE_VERSION |
143 | } // namespace | |
1ff9402d BK |
144 | |
145 | #endif |