00001 #ifndef DataFormatsCommonDebuggingAllocator 00002 #define DataFormatsCommonDebuggingAllocator 00003 00004 //--------------------------------------------------------------------- 00005 // 00006 // This file declares and defines an allocator class template. 00007 // This allocator is intended for use with the memory checking tool 00008 // valgrind. It is a minimum conformant implementation which makes sure 00009 // not use use any unitialized memory, and so it causes no spurious error 00010 // reports from valgrind. 00011 // 00012 // The intended use is in the declarations of objects from STL templates, 00013 // e.g. 00014 // typedef vector<int, edm::debugging_allocator<int> > vint; 00015 // etc. 00016 // 00017 // $Id: debugging_allocator.h,v 1.2 2008/01/17 20:26:23 wmtan Exp $ 00018 //--------------------------------------------------------------------- 00019 00020 #include <limits> 00021 #include <cstddef> 00022 00023 namespace edm 00024 { 00025 template <class T> 00026 class debugging_allocator 00027 { 00028 public: 00029 typedef size_t size_type; 00030 typedef ptrdiff_t difference_type; 00031 typedef T* pointer; 00032 typedef T const* const_pointer; 00033 typedef T& reference; 00034 typedef T const& const_reference; 00035 typedef T value_type; 00036 00037 template <class U> struct rebind { typedef debugging_allocator<U> other; }; 00038 00039 00040 debugging_allocator() throw() : dummy('x') { } 00041 00042 debugging_allocator(debugging_allocator const&) throw() : dummy('c') { } 00043 00044 template <class U> debugging_allocator(debugging_allocator<U> const&) throw() : dummy('u') { } 00045 00046 ~debugging_allocator() throw() { }; 00047 00048 pointer address(reference value) const {return &value;} 00049 00050 const_pointer address(const_reference value) const {return &value; } 00051 00052 size_type max_size() const throw() { return std::numeric_limits<size_type>::max()/sizeof(T); } 00053 00054 pointer allocate(size_type num, void const* hint = 0) 00055 { 00056 // allocate objects with global new 00057 return (pointer)(::operator new(num*sizeof(T))); 00058 } 00059 00060 void construct(pointer p, T const& value) { new((void*)p)T(value); } 00061 00062 void destroy(pointer p) { p->~T(); } 00063 00064 void deallocate(pointer p, size_type num) { ::operator delete((void*)p); } 00065 00066 private: 00067 char dummy; 00068 00069 }; 00070 00071 // instances of all specializations of this allocator are equal 00072 template <class X, class Y> 00073 bool operator== (debugging_allocator<X> const&, debugging_allocator<Y> const&) throw() { return true; } 00074 00075 template <class X, class Y> 00076 bool operator!= (debugging_allocator<X> const&, debugging_allocator<Y> const&) throw() { return false; } 00077 00078 } //namespace edm 00079 00080 00081 #endif