new_allocator.h
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
00031
00032
00033
00034 #ifndef _NEW_ALLOCATOR_H
00035 #define _NEW_ALLOCATOR_H 1
00036
00037 #include <new>
00038 #include <bits/functexcept.h>
00039
00040 namespace __gnu_cxx
00041 {
00042
00043
00044
00045
00046
00047
00048
00049 template<typename _Tp>
00050 class new_allocator
00051 {
00052 public:
00053 typedef size_t size_type;
00054 typedef ptrdiff_t difference_type;
00055 typedef _Tp* pointer;
00056 typedef const _Tp* const_pointer;
00057 typedef _Tp& reference;
00058 typedef const _Tp& const_reference;
00059 typedef _Tp value_type;
00060
00061 template<typename _Tp1>
00062 struct rebind
00063 { typedef new_allocator<_Tp1> other; };
00064
00065 new_allocator() throw() { }
00066
00067 new_allocator(const new_allocator&) throw() { }
00068
00069 template<typename _Tp1>
00070 new_allocator(const new_allocator<_Tp1>&) throw() { }
00071
00072 ~new_allocator() throw() { }
00073
00074 pointer
00075 address(reference __x) const { return &__x; }
00076
00077 const_pointer
00078 address(const_reference __x) const { return &__x; }
00079
00080
00081
00082 pointer
00083 allocate(size_type __n, const void* = 0)
00084 {
00085 if (__builtin_expect(__n > this->max_size(), false))
00086 std::__throw_bad_alloc();
00087
00088 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
00089 }
00090
00091
00092 void
00093 deallocate(pointer __p, size_type)
00094 { ::operator delete(__p); }
00095
00096 size_type
00097 max_size() const throw()
00098 { return size_t(-1) / sizeof(_Tp); }
00099
00100
00101
00102 void
00103 construct(pointer __p, const _Tp& __val)
00104 { ::new(__p) _Tp(__val); }
00105
00106 void
00107 destroy(pointer __p) { __p->~_Tp(); }
00108 };
00109
00110 template<typename _Tp>
00111 inline bool
00112 operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
00113 { return true; }
00114
00115 template<typename _Tp>
00116 inline bool
00117 operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
00118 { return false; }
00119 }
00120
00121 #endif