#ifndef __LIBLSS_TOOLS_MEMUSAGE_HPP #define __LIBLSS_TOOLS_MEMUSAGE_HPP #include #include "libLSS/tools/static_init.hpp" #include "libLSS/tools/static_auto.hpp" #include "libLSS/tools/errors.hpp" #include #include namespace LibLSS { struct AllocationDetail { ssize_t allocated, freed; size_t peak; }; void report_allocation(size_t sz, const void *ptr); void report_free(size_t sz, const void *ptr); std::map memoryReport(); void clearReport(); template struct track_allocator: public std::allocator { public: typedef typename std::allocator parent; typedef typename parent::pointer pointer; typedef typename parent::size_type size_type; template struct rebind { typedef track_allocator other; }; track_allocator() throw(): std::allocator() {} track_allocator(const track_allocator& alloc) throw(): std::allocator(alloc) {} template track_allocator(const track_allocator& alloc) throw(): std::allocator(alloc) {} pointer allocate(size_type _Count, const void *_Hint = 0) { pointer p = std::allocator::allocate(_Count, _Hint); if (p) { report_allocation(_Count*sizeof(T), _Hint); } else { error_helper(boost::format("Memory allocation failed to allocate %d bytes") % (sizeof(T)*_Count)); } return p; } void deallocate(pointer _Ptr, size_type _Count) { std::allocator::deallocate(_Ptr, _Count); report_free(_Count*sizeof(T), _Ptr); } }; } AUTO_REGISTRATOR_DECL(memory_alloc); #endif