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
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef _DEBUG_ALLOCATOR_H
00044 #define _DEBUG_ALLOCATOR_H 1
00045
00046 #include <stdexcept>
00047
00048 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00049 {
00050 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00051
00052 using std::size_t;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 template<typename _Alloc>
00063 class debug_allocator
00064 {
00065 public:
00066 typedef typename _Alloc::size_type size_type;
00067 typedef typename _Alloc::difference_type difference_type;
00068 typedef typename _Alloc::pointer pointer;
00069 typedef typename _Alloc::const_pointer const_pointer;
00070 typedef typename _Alloc::reference reference;
00071 typedef typename _Alloc::const_reference const_reference;
00072 typedef typename _Alloc::value_type value_type;
00073
00074 private:
00075
00076
00077 size_type _M_extra;
00078
00079 _Alloc _M_allocator;
00080
00081 public:
00082 debug_allocator()
00083 {
00084 const size_t __obj_size = sizeof(value_type);
00085 _M_extra = (sizeof(size_type) + __obj_size - 1) / __obj_size;
00086 }
00087
00088 pointer
00089 allocate(size_type __n)
00090 {
00091 pointer __res = _M_allocator.allocate(__n + _M_extra);
00092 size_type* __ps = reinterpret_cast<size_type*>(__res);
00093 *__ps = __n;
00094 return __res + _M_extra;
00095 }
00096
00097 pointer
00098 allocate(size_type __n, const void* __hint)
00099 {
00100 pointer __res = _M_allocator.allocate(__n + _M_extra, __hint);
00101 size_type* __ps = reinterpret_cast<size_type*>(__res);
00102 *__ps = __n;
00103 return __res + _M_extra;
00104 }
00105
00106 void
00107 deallocate(pointer __p, size_type __n)
00108 {
00109 if (__p)
00110 {
00111 pointer __real_p = __p - _M_extra;
00112 if (*reinterpret_cast<size_type*>(__real_p) != __n)
00113 {
00114 throw std::runtime_error("debug_allocator::deallocate"
00115 " wrong size");
00116 }
00117 _M_allocator.deallocate(__real_p, __n + _M_extra);
00118 }
00119 else
00120 throw std::runtime_error("debug_allocator::deallocate null pointer");
00121 }
00122 };
00123
00124 _GLIBCXX_END_NAMESPACE_VERSION
00125 }
00126
00127 #endif