libstdc++
|
00001 // array allocator -*- C++ -*- 00002 00003 // Copyright (C) 2004-2013 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 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 /// Base class. 00049 template<typename _Tp> 00050 class array_allocator_base 00051 { 00052 public: 00053 typedef size_t size_type; 00054 typedef ptrdiff_t difference_type; 00055 typedef _Tp* pointer; 00056 typedef const _Tp* const_pointer; 00057 typedef _Tp& reference; 00058 typedef const _Tp& const_reference; 00059 typedef _Tp value_type; 00060 00061 pointer 00062 address(reference __x) const _GLIBCXX_NOEXCEPT 00063 { return std::__addressof(__x); } 00064 00065 const_pointer 00066 address(const_reference __x) const _GLIBCXX_NOEXCEPT 00067 { return std::__addressof(__x); } 00068 00069 void 00070 deallocate(pointer, size_type) 00071 { 00072 // Does nothing. 00073 } 00074 00075 size_type 00076 max_size() const _GLIBCXX_USE_NOEXCEPT 00077 { return size_t(-1) / sizeof(_Tp); } 00078 00079 #if __cplusplus >= 201103L 00080 template<typename _Up, typename... _Args> 00081 void 00082 construct(_Up* __p, _Args&&... __args) 00083 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00084 00085 template<typename _Up> 00086 void 00087 destroy(_Up* __p) { __p->~_Up(); } 00088 #else 00089 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00090 // 402. wrong new expression in [some_] allocator::construct 00091 void 00092 construct(pointer __p, const _Tp& __val) 00093 { ::new((void *)__p) value_type(__val); } 00094 00095 void 00096 destroy(pointer __p) { __p->~_Tp(); } 00097 #endif 00098 }; 00099 00100 /** 00101 * @brief An allocator that uses previously allocated memory. 00102 * This memory can be externally, globally, or otherwise allocated. 00103 * @ingroup allocators 00104 */ 00105 template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> > 00106 class array_allocator : public array_allocator_base<_Tp> 00107 { 00108 public: 00109 typedef size_t size_type; 00110 typedef ptrdiff_t difference_type; 00111 typedef _Tp* pointer; 00112 typedef const _Tp* const_pointer; 00113 typedef _Tp& reference; 00114 typedef const _Tp& const_reference; 00115 typedef _Tp value_type; 00116 typedef _Array array_type; 00117 00118 #if __cplusplus >= 201103L 00119 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00120 // 2103. std::allocator propagate_on_container_move_assignment 00121 typedef std::true_type propagate_on_container_move_assignment; 00122 #endif 00123 00124 private: 00125 array_type* _M_array; 00126 size_type _M_used; 00127 00128 public: 00129 template<typename _Tp1, typename _Array1 = _Array> 00130 struct rebind 00131 { typedef array_allocator<_Tp1, _Array1> other; }; 00132 00133 array_allocator(array_type* __array = 0) _GLIBCXX_USE_NOEXCEPT 00134 : _M_array(__array), _M_used(size_type()) { } 00135 00136 array_allocator(const array_allocator& __o) _GLIBCXX_USE_NOEXCEPT 00137 : _M_array(__o._M_array), _M_used(__o._M_used) { } 00138 00139 template<typename _Tp1, typename _Array1> 00140 array_allocator(const array_allocator<_Tp1, _Array1>&) 00141 _GLIBCXX_USE_NOEXCEPT 00142 : _M_array(0), _M_used(size_type()) { } 00143 00144 ~array_allocator() _GLIBCXX_USE_NOEXCEPT { } 00145 00146 pointer 00147 allocate(size_type __n, const void* = 0) 00148 { 00149 if (_M_array == 0 || _M_used + __n > _M_array->size()) 00150 std::__throw_bad_alloc(); 00151 pointer __ret = _M_array->begin() + _M_used; 00152 _M_used += __n; 00153 return __ret; 00154 } 00155 }; 00156 00157 template<typename _Tp, typename _Array> 00158 inline bool 00159 operator==(const array_allocator<_Tp, _Array>&, 00160 const array_allocator<_Tp, _Array>&) 00161 { return true; } 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 false; } 00168 00169 _GLIBCXX_END_NAMESPACE_VERSION 00170 } // namespace 00171 00172 #endif