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
00031
00032
00033
00034 #ifndef _ARRAY_ALLOCATOR_H
00035 #define _ARRAY_ALLOCATOR_H 1
00036
00037 #include <cstddef>
00038 #include <new>
00039 #include <bits/functexcept.h>
00040 #include <tr1/array>
00041
00042 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00043
00044 using std::size_t;
00045 using std::ptrdiff_t;
00046
00047
00048 template<typename _Tp>
00049 class array_allocator_base
00050 {
00051 public:
00052 typedef size_t size_type;
00053 typedef ptrdiff_t difference_type;
00054 typedef _Tp* pointer;
00055 typedef const _Tp* const_pointer;
00056 typedef _Tp& reference;
00057 typedef const _Tp& const_reference;
00058 typedef _Tp value_type;
00059
00060 pointer
00061 address(reference __x) const { return &__x; }
00062
00063 const_pointer
00064 address(const_reference __x) const { return &__x; }
00065
00066 void
00067 deallocate(pointer, size_type)
00068 {
00069
00070 }
00071
00072 size_type
00073 max_size() const throw()
00074 { return size_t(-1) / sizeof(_Tp); }
00075
00076
00077
00078 void
00079 construct(pointer __p, const _Tp& __val)
00080 { ::new(__p) value_type(__val); }
00081
00082 void
00083 destroy(pointer __p) { __p->~_Tp(); }
00084 };
00085
00086
00087
00088
00089
00090 template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
00091 class array_allocator : public array_allocator_base<_Tp>
00092 {
00093 public:
00094 typedef size_t size_type;
00095 typedef ptrdiff_t difference_type;
00096 typedef _Tp* pointer;
00097 typedef const _Tp* const_pointer;
00098 typedef _Tp& reference;
00099 typedef const _Tp& const_reference;
00100 typedef _Tp value_type;
00101 typedef _Array array_type;
00102
00103 private:
00104 array_type* _M_array;
00105 size_type _M_used;
00106
00107 public:
00108 template<typename _Tp1, typename _Array1 = _Array>
00109 struct rebind
00110 { typedef array_allocator<_Tp1, _Array1> other; };
00111
00112 array_allocator(array_type* __array = NULL) throw()
00113 : _M_array(__array), _M_used(size_type()) { }
00114
00115 array_allocator(const array_allocator& __o) throw()
00116 : _M_array(__o._M_array), _M_used(__o._M_used) { }
00117
00118 template<typename _Tp1, typename _Array1>
00119 array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
00120 : _M_array(NULL), _M_used(size_type()) { }
00121
00122 ~array_allocator() throw() { }
00123
00124 pointer
00125 allocate(size_type __n, const void* = 0)
00126 {
00127 if (_M_array == 0 || _M_used + __n > _M_array->size())
00128 std::__throw_bad_alloc();
00129 pointer __ret = _M_array->begin() + _M_used;
00130 _M_used += __n;
00131 return __ret;
00132 }
00133 };
00134
00135 template<typename _Tp, typename _Array>
00136 inline bool
00137 operator==(const array_allocator<_Tp, _Array>&,
00138 const array_allocator<_Tp, _Array>&)
00139 { return true; }
00140
00141 template<typename _Tp, typename _Array>
00142 inline bool
00143 operator!=(const array_allocator<_Tp, _Array>&,
00144 const array_allocator<_Tp, _Array>&)
00145 { return false; }
00146
00147 _GLIBCXX_END_NAMESPACE
00148
00149 #endif