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