libstdc++
|
00001 // array allocator -*- C++ -*- 00002 00003 // Copyright (C) 2004-2015 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/array_allocator.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _ARRAY_ALLOCATOR_H 00030 #define _ARRAY_ALLOCATOR_H 1 00031 00032 #include <bits/c++config.h> 00033 #include <new> 00034 #include <bits/functexcept.h> 00035 #include <tr1/array> 00036 #include <bits/move.h> 00037 #if __cplusplus >= 201103L 00038 #include <type_traits> 00039 #endif 00040 00041 // Suppress deprecated warning for this file. 00042 #pragma GCC diagnostic push 00043 #pragma GCC diagnostic ignored "-Wdeprecated-declarations" 00044 00045 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00046 { 00047 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00048 00049 using std::size_t; 00050 using std::ptrdiff_t; 00051 00052 /// Base class. 00053 template<typename _Tp> 00054 class array_allocator_base 00055 { 00056 public: 00057 typedef size_t size_type; 00058 typedef ptrdiff_t difference_type; 00059 typedef _Tp* pointer; 00060 typedef const _Tp* const_pointer; 00061 typedef _Tp& reference; 00062 typedef const _Tp& const_reference; 00063 typedef _Tp value_type; 00064 00065 pointer 00066 address(reference __x) const _GLIBCXX_NOEXCEPT 00067 { return std::__addressof(__x); } 00068 00069 const_pointer 00070 address(const_reference __x) const _GLIBCXX_NOEXCEPT 00071 { return std::__addressof(__x); } 00072 00073 void 00074 deallocate(pointer, size_type) 00075 { 00076 // Does nothing. 00077 } 00078 00079 size_type 00080 max_size() const _GLIBCXX_USE_NOEXCEPT 00081 { return size_t(-1) / sizeof(_Tp); } 00082 00083 #if __cplusplus >= 201103L 00084 template<typename _Up, typename... _Args> 00085 void 00086 construct(_Up* __p, _Args&&... __args) 00087 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00088 00089 template<typename _Up> 00090 void 00091 destroy(_Up* __p) { __p->~_Up(); } 00092 #else 00093 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00094 // 402. wrong new expression in [some_] allocator::construct 00095 void 00096 construct(pointer __p, const _Tp& __val) 00097 { ::new((void *)__p) value_type(__val); } 00098 00099 void 00100 destroy(pointer __p) { __p->~_Tp(); } 00101 #endif 00102 } _GLIBCXX_DEPRECATED; 00103 00104 /** 00105 * @brief An allocator that uses previously allocated memory. 00106 * This memory can be externally, globally, or otherwise allocated. 00107 * @ingroup allocators 00108 */ 00109 template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> > 00110 class array_allocator : public array_allocator_base<_Tp> 00111 { 00112 public: 00113 typedef size_t size_type; 00114 typedef ptrdiff_t difference_type; 00115 typedef _Tp* pointer; 00116 typedef const _Tp* const_pointer; 00117 typedef _Tp& reference; 00118 typedef const _Tp& const_reference; 00119 typedef _Tp value_type; 00120 typedef _Array array_type; 00121 00122 #if __cplusplus >= 201103L 00123 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00124 // 2103. std::allocator propagate_on_container_move_assignment 00125 typedef std::true_type propagate_on_container_move_assignment; 00126 #endif 00127 00128 private: 00129 array_type* _M_array; 00130 size_type _M_used; 00131 00132 public: 00133 template<typename _Tp1, typename _Array1 = _Array> 00134 struct rebind 00135 { 00136 typedef array_allocator<_Tp1, _Array1> other _GLIBCXX_DEPRECATED; 00137 } _GLIBCXX_DEPRECATED; 00138 00139 array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT 00140 : _M_array(__array), _M_used(size_type()) { } 00141 00142 array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT 00143 : _M_array(__o._M_array), _M_used(__o._M_used) { } 00144 00145 template<typename _Tp1, typename _Array1> 00146 array_allocator(const array_allocator<_Tp1, _Array1>&) 00147 _GLIBCXX_USE_NOEXCEPT 00148 : _M_array(0), _M_used(size_type()) { } 00149 00150 ~array_allocator() _GLIBCXX_USE_NOEXCEPT { } 00151 00152 pointer 00153 allocate(size_type __n, const void* = 0) 00154 { 00155 if (_M_array == 0 || _M_used + __n > _M_array->size()) 00156 std::__throw_bad_alloc(); 00157 pointer __ret = _M_array->begin() + _M_used; 00158 _M_used += __n; 00159 return __ret; 00160 } 00161 } _GLIBCXX_DEPRECATED; 00162 00163 template<typename _Tp, typename _Array> 00164 inline bool 00165 operator==(const array_allocator<_Tp, _Array>&, 00166 const array_allocator<_Tp, _Array>&) 00167 { return true; } 00168 00169 template<typename _Tp, typename _Array> 00170 inline bool 00171 operator!=(const array_allocator<_Tp, _Array>&, 00172 const array_allocator<_Tp, _Array>&) 00173 { return false; } 00174 00175 _GLIBCXX_END_NAMESPACE_VERSION 00176 } // namespace 00177 00178 #pragma GCC diagnostic pop 00179 00180 #endif