-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-memory.h
88 lines (74 loc) · 2.27 KB
/
dump-memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// dump-memory.h
//
// Created by massimo on 7/17/18.
//
#pragma once
#include <iostream>
#include <string>
#include <type_traits>
#include <cxxabi.h>
#include <cstring>
////////////////////////////////////////////////////////////////////////////////
// useful macros
// in case we want to dump a variable
#define DMV(var) utilities::dumpMemory(&(var), sizeof(decltype(var)))
//#define DMV(var) utilities::dumpMemory(&(var), sizeof(decltype(var)), getDemangledTypeName<decltype(var)>())
// in case we want to dump a memory of type type, pointed to by a pointer ptr
#define DMP(ptr, type) utilities::dumpMemory(ptr, sizeof(type))
//#define DMP(ptr, type) utilities::dumpMemory(ptr, sizeof(type), getDemangledTypeName<type>())
namespace demangle
{
template<typename T>
static
std::string
getDemangledTypeName() noexcept;
template<typename T>
static
std::string
getDemangledTypeName() noexcept
{
int status{};
char *demangledName{abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status)};
std::string demangledNameString{static_cast<std::string>(demangledName)};
free(demangledName);
return demangledNameString;
}
} // namespace demangle
namespace utilities
{
void
dumpMemory(const void* ptr,
std::size_t size,
std::string&& demangledTypeName = "",
std::ostream& os = std::cout) noexcept;
template <typename T>
void
dumpMemory(T ptr,
std::size_t size,
std::ostream& os = std::cout) noexcept;
template <typename T>
void
dumpMemory(const T ptr,
size_t size,
std::ostream& os) noexcept
{
static_assert(std::is_pointer<T>::value, "pointer needed as arg 1 for dumpMemory()");
dumpMemory(reinterpret_cast<const void*>(ptr),
size,
demangle::getDemangledTypeName<decltype(typename std::remove_pointer<T>::type())>(),
os);
}
template <typename T>
void
dumpMemory(T&& var, std::ostream& os = std::cout) noexcept;
template <typename T>
void
dumpMemory(T&& var, std::ostream& os) noexcept
{
static_assert(std::is_reference<decltype(var)>::value != 0,
"reference, or either lvalue or rvalue reference needed as arg 1 for dumpMemory()");
dumpMemory(&var, sizeof(var), os);
}
void dumpMemory(const char a[], std::ostream& os = std::cout);
} // namespace utilities