libstdc++
|
00001 // Allocator that wraps operator new -*- C++ -*- 00002 00003 // Copyright (C) 2001-2018 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file ext/new_allocator.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _NEW_ALLOCATOR_H 00030 #define _NEW_ALLOCATOR_H 1 00031 00032 #include <bits/c++config.h> 00033 #include <new> 00034 #include <bits/functexcept.h> 00035 #include <bits/move.h> 00036 #if __cplusplus >= 201103L 00037 #include <type_traits> 00038 #endif 00039 00040 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00041 { 00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00043 00044 using std::size_t; 00045 using std::ptrdiff_t; 00046 00047 /** 00048 * @brief An allocator that uses global new, as per [20.4]. 00049 * @ingroup allocators 00050 * 00051 * This is precisely the allocator defined in the C++ Standard. 00052 * - all allocation calls operator new 00053 * - all deallocation calls operator delete 00054 * 00055 * @tparam _Tp Type of allocated object. 00056 */ 00057 template<typename _Tp> 00058 class new_allocator 00059 { 00060 public: 00061 typedef size_t size_type; 00062 typedef ptrdiff_t difference_type; 00063 typedef _Tp* pointer; 00064 typedef const _Tp* const_pointer; 00065 typedef _Tp& reference; 00066 typedef const _Tp& const_reference; 00067 typedef _Tp value_type; 00068 00069 template<typename _Tp1> 00070 struct rebind 00071 { typedef new_allocator<_Tp1> other; }; 00072 00073 #if __cplusplus >= 201103L 00074 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00075 // 2103. propagate_on_container_move_assignment 00076 typedef std::true_type propagate_on_container_move_assignment; 00077 #endif 00078 00079 new_allocator() _GLIBCXX_USE_NOEXCEPT { } 00080 00081 new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { } 00082 00083 template<typename _Tp1> 00084 new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { } 00085 00086 ~new_allocator() _GLIBCXX_USE_NOEXCEPT { } 00087 00088 pointer 00089 address(reference __x) const _GLIBCXX_NOEXCEPT 00090 { return std::__addressof(__x); } 00091 00092 const_pointer 00093 address(const_reference __x) const _GLIBCXX_NOEXCEPT 00094 { return std::__addressof(__x); } 00095 00096 // NB: __n is permitted to be 0. The C++ standard says nothing 00097 // about what the return value is when __n == 0. 00098 pointer 00099 allocate(size_type __n, const void* = static_cast<const void*>(0)) 00100 { 00101 if (__n > this->max_size()) 00102 std::__throw_bad_alloc(); 00103 00104 #if __cpp_aligned_new 00105 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) 00106 { 00107 std::align_val_t __al = std::align_val_t(alignof(_Tp)); 00108 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al)); 00109 } 00110 #endif 00111 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); 00112 } 00113 00114 // __p is not permitted to be a null pointer. 00115 void 00116 deallocate(pointer __p, size_type) 00117 { 00118 #if __cpp_aligned_new 00119 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) 00120 { 00121 ::operator delete(__p, std::align_val_t(alignof(_Tp))); 00122 return; 00123 } 00124 #endif 00125 ::operator delete(__p); 00126 } 00127 00128 size_type 00129 max_size() const _GLIBCXX_USE_NOEXCEPT 00130 { return size_t(-1) / sizeof(_Tp); } 00131 00132 #if __cplusplus >= 201103L 00133 template<typename _Up, typename... _Args> 00134 void 00135 construct(_Up* __p, _Args&&... __args) 00136 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00137 00138 template<typename _Up> 00139 void 00140 destroy(_Up* __p) { __p->~_Up(); } 00141 #else 00142 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00143 // 402. wrong new expression in [some_] allocator::construct 00144 void 00145 construct(pointer __p, const _Tp& __val) 00146 { ::new((void *)__p) _Tp(__val); } 00147 00148 void 00149 destroy(pointer __p) { __p->~_Tp(); } 00150 #endif 00151 }; 00152 00153 template<typename _Tp> 00154 inline bool 00155 operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 00156 { return true; } 00157 00158 template<typename _Tp> 00159 inline bool 00160 operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 00161 { return false; } 00162 00163 _GLIBCXX_END_NAMESPACE_VERSION 00164 } // namespace 00165 00166 #endif