00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef STXXL_NEW_ALLOC_HEADER
00015 #define STXXL_NEW_ALLOC_HEADER
00016
00017 #include <memory>
00018 #include <limits>
00019 #include <stxxl/bits/namespace.h>
00020
00021
00022 __STXXL_BEGIN_NAMESPACE
00023
00024 template <class T>
00025 class new_alloc;
00026
00027 template <typename T, typename U>
00028 struct new_alloc_rebind;
00029
00030 template <typename T>
00031 struct new_alloc_rebind<T, T>{
00032 typedef new_alloc<T> other;
00033 };
00034
00035 template <typename T, typename U>
00036 struct new_alloc_rebind {
00037 typedef std::allocator<U> other;
00038 };
00039
00040
00041
00042 template <class T>
00043 class new_alloc {
00044 public:
00045
00046 typedef T value_type;
00047 typedef T * pointer;
00048 typedef const T * const_pointer;
00049 typedef T & reference;
00050 typedef const T & const_reference;
00051 typedef std::size_t size_type;
00052 typedef std::ptrdiff_t difference_type;
00053
00054
00055 template <class U>
00056 struct rebind {
00057 typedef typename new_alloc_rebind<T, U>::other other;
00058 };
00059
00060
00061 pointer address(reference value) const
00062 {
00063 return &value;
00064 }
00065 const_pointer address(const_reference value) const
00066 {
00067 return &value;
00068 }
00069
00070 new_alloc() throw () { }
00071 new_alloc(const new_alloc &) throw () { }
00072 template <class U>
00073 new_alloc(const new_alloc<U> &) throw () { }
00074 ~new_alloc() throw () { }
00075
00076 template <class U>
00077 operator std::allocator<U>()
00078 {
00079 static std::allocator<U> helper_allocator;
00080 return helper_allocator;
00081 }
00082
00083
00084 size_type max_size() const throw ()
00085 {
00086 return (std::numeric_limits<size_type>::max)() / sizeof(T);
00087 }
00088
00089
00090 pointer allocate(size_type num, const void * = 0)
00091 {
00092 return static_cast<T *>(T::operator new (num * sizeof(T)));
00093 }
00094
00095
00096
00097
00098 void construct(pointer p, const T & value)
00099 {
00100
00101 ::new ((void *)p)T(value);
00102 }
00103
00104 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00105 template <typename ... Args>
00106 void construct(pointer p, Args && ... args)
00107 {
00108 ::new ((void *)p)T(std::forward<Args>(args) ...);
00109 }
00110 #endif
00111
00112
00113 void destroy(pointer p)
00114 {
00115
00116 p->~T();
00117 }
00118
00119
00120 void deallocate(pointer p, size_type )
00121 {
00122 T::operator delete (p);
00123 }
00124 };
00125
00126
00127 template <class T1, class T2>
00128 inline bool operator == (const new_alloc<T1> &,
00129 const new_alloc<T2> &) throw ()
00130 {
00131 return true;
00132 }
00133
00134 template <class T1, class T2>
00135 inline bool operator != (const new_alloc<T1> &,
00136 const new_alloc<T2> &) throw ()
00137 {
00138 return false;
00139 }
00140
00141 __STXXL_END_NAMESPACE
00142
00143 #endif // !STXXL_NEW_ALLOC_HEADER
00144