Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00069 }
00070
00071 size_type
00072 max_size() const throw()
00073 { return size_t(-1) / sizeof(_Tp); }
00074
00075
00076
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
00094
00095
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 }
00156
00157 #endif