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