libstdc++
|
00001 // Allocator that wraps "C" malloc -*- 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/malloc_allocator.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _MALLOC_ALLOCATOR_H 00030 #define _MALLOC_ALLOCATOR_H 1 00031 00032 #include <cstdlib> 00033 #include <cstddef> 00034 #include <new> 00035 #include <bits/functexcept.h> 00036 #include <bits/move.h> 00037 #if __cplusplus >= 201103L 00038 #include <type_traits> 00039 #endif 00040 00041 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00042 { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 using std::size_t; 00046 using std::ptrdiff_t; 00047 00048 /** 00049 * @brief An allocator that uses malloc. 00050 * @ingroup allocators 00051 * 00052 * This is precisely the allocator defined in the C++ Standard. 00053 * - all allocation calls malloc 00054 * - all deallocation calls free 00055 */ 00056 template<typename _Tp> 00057 class malloc_allocator 00058 { 00059 public: 00060 typedef size_t size_type; 00061 typedef ptrdiff_t difference_type; 00062 typedef _Tp* pointer; 00063 typedef const _Tp* const_pointer; 00064 typedef _Tp& reference; 00065 typedef const _Tp& const_reference; 00066 typedef _Tp value_type; 00067 00068 template<typename _Tp1> 00069 struct rebind 00070 { typedef malloc_allocator<_Tp1> other; }; 00071 00072 #if __cplusplus >= 201103L 00073 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00074 // 2103. propagate_on_container_move_assignment 00075 typedef std::true_type propagate_on_container_move_assignment; 00076 #endif 00077 00078 malloc_allocator() _GLIBCXX_USE_NOEXCEPT { } 00079 00080 malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { } 00081 00082 template<typename _Tp1> 00083 malloc_allocator(const malloc_allocator<_Tp1>&) 00084 _GLIBCXX_USE_NOEXCEPT { } 00085 00086 ~malloc_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* = 0) 00100 { 00101 if (__n > this->max_size()) 00102 std::__throw_bad_alloc(); 00103 00104 pointer __ret = 0; 00105 #if __cpp_aligned_new 00106 #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC 00107 if (alignof(_Tp) > alignof(std::max_align_t)) 00108 { 00109 __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp), 00110 __n * sizeof(_Tp))); 00111 } 00112 #else 00113 # define _GLIBCXX_CHECK_MALLOC_RESULT 00114 #endif 00115 #endif 00116 if (!__ret) 00117 __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp))); 00118 if (!__ret) 00119 std::__throw_bad_alloc(); 00120 #ifdef _GLIBCXX_CHECK_MALLOC_RESULT 00121 #undef _GLIBCXX_CHECK_MALLOC_RESULT 00122 if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp)) 00123 { 00124 // Memory returned by malloc is not suitably aligned for _Tp. 00125 deallocate(__ret, __n); 00126 std::__throw_bad_alloc(); 00127 } 00128 #endif 00129 return __ret; 00130 } 00131 00132 // __p is not permitted to be a null pointer. 00133 void 00134 deallocate(pointer __p, size_type) 00135 { std::free(static_cast<void*>(__p)); } 00136 00137 size_type 00138 max_size() const _GLIBCXX_USE_NOEXCEPT 00139 { return size_t(-1) / sizeof(_Tp); } 00140 00141 #if __cplusplus >= 201103L 00142 template<typename _Up, typename... _Args> 00143 void 00144 construct(_Up* __p, _Args&&... __args) 00145 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00146 00147 template<typename _Up> 00148 void 00149 destroy(_Up* __p) { __p->~_Up(); } 00150 #else 00151 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00152 // 402. wrong new expression in [some_] allocator::construct 00153 void 00154 construct(pointer __p, const _Tp& __val) 00155 { ::new((void *)__p) value_type(__val); } 00156 00157 void 00158 destroy(pointer __p) { __p->~_Tp(); } 00159 #endif 00160 }; 00161 00162 template<typename _Tp> 00163 inline bool 00164 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&) 00165 { return true; } 00166 00167 template<typename _Tp> 00168 inline bool 00169 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&) 00170 { return false; } 00171 00172 _GLIBCXX_END_NAMESPACE_VERSION 00173 } // namespace 00174 00175 #endif