PassengerStatistics/Tools/LeakTracer/MemoryAllocationStackTracer.h
2024-03-23 11:57:23 +08:00

76 lines
1.8 KiB
C++

#ifndef __MEMORYALLOCATIONSTACKTRACER_H__
#define __MEMORYALLOCATIONSTACKTRACER_H__
#include <boost/stacktrace/stacktrace.hpp>
#include <unordered_map>
extern "C" {
extern void *__real_malloc(size_t size);
extern void __real_free(void *__ptr);
}
template <class T>
class DebugAllocator {
public:
using value_type = T;
using pointer = T *;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template <class U>
struct rebind {
typedef DebugAllocator<U> other;
};
DebugAllocator() {
}
template <class U>
DebugAllocator(const DebugAllocator<U> &other) noexcept {
}
T *allocate(std::size_t n) {
return reinterpret_cast<T *>(__real_malloc(n));
}
void deallocate(T *p, std::size_t n) {
__real_free(p);
}
template <class U, class... Args>
void construct(U *p, Args &&...args) {
::new ((void *)p) U(std::forward<Args>(args)...);
}
template <class U>
void destroy(U *p) {
p->~U();
}
pointer address(reference x) {
return (pointer)&x;
}
const_pointer address(const_reference x) {
return (const_pointer)&x;
}
};
class MemoryAllocationStackTracer {
public:
static void initialize();
static MemoryAllocationStackTracer *instance();
void push(uint64_t address, const boost::stacktrace::stacktrace &stacktrace);
void pop(uint64_t address);
private:
std::unordered_map<uint64_t, boost::stacktrace::stacktrace, std::hash<uint64_t>, std::equal_to<uint64_t>,
DebugAllocator<MemoryAllocationStackTracer>>
m_stacktraces;
static MemoryAllocationStackTracer *m_instance;
};
#endif // __MEMORYALLOCATIONSTACKTRACER_H__