NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
trace_memory.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_MEMORY_TRACE_MEMORY_HPP__
2#define NEFORCE_CORE_MEMORY_TRACE_MEMORY_HPP__
3
12
16NEFORCE_BEGIN_NAMESPACE__
17
23
33template <typename T>
35 static_assert(is_allocable_v<T>, "allocator can`t alloc void, reference, function or const type.");
36
37public:
38 using value_type = T;
39 using pointer = T*;
40 using const_pointer = const T*;
41 using reference = T&;
42 using const_reference = const T&;
43 using size_type = size_t;
44 using difference_type = ptrdiff_t;
45
53 template <typename U>
54 struct rebind {
55 using other = trace_allocator<U>;
56 };
57
58private:
60
61public:
65 trace_allocator() = default;
66
72 traces_(other.traces_) {}
73
80 if (_NEFORCE addressof(other) == this) {
81 return *this;
82 }
83 traces_ = other.traces_;
84 return *this;
85 }
86
93 if (!traces_.empty()) {
94 _NEFORCE printcln(color::red(), "Memory leaks detected! \n");
96 }
97 }
98
104 void print_stacktrace() const {
105 for (auto& entry: traces_) {
106 if (entry.first == 0) {
107 continue;
108 }
109 _NEFORCE printcln(color::red(), "Leaked pointer: ", static_cast<void*>(entry.first));
110 _NEFORCE printcln(color::red(), "Allocation stack trace:\n", entry.second);
111 }
112 }
113
121 NEFORCE_NODISCARD NEFORCE_ALLOC_OPTIMIZE pointer allocate(const size_type n) {
122 pointer ptr = allocator<T>().allocate(n);
123 stacktrace st{};
124 traces_[ptr] = _NEFORCE move(st);
125 return ptr;
126 }
127
134 NEFORCE_NODISCARD NEFORCE_ALLOC_OPTIMIZE pointer allocate() { return this->allocate(1); }
135
143 void deallocate(pointer p, const size_type n) noexcept {
144 auto it = traces_.find(p);
145 if (it != traces_.end()) {
146 traces_.erase(it);
147 }
148 allocator<T>().deallocate(p, n);
149 }
150
157 void deallocate(pointer p) noexcept { this->deallocate(p, 1); }
158};
159
170template <typename T, typename U>
171bool operator==(const trace_allocator<T>& lhs, const trace_allocator<U>& rhs) noexcept {
172 return true;
173}
174
183template <typename T, typename U>
184bool operator!=(const trace_allocator<T>& lhs, const trace_allocator<U>& rhs) noexcept {
185 return false;
186}
187 // Stacktrace
189
190NEFORCE_END_NAMESPACE__
191#endif // NEFORCE_CORE_MEMORY_TRACE_MEMORY_HPP__
static constexpr color red() noexcept
红色
堆栈跟踪类
内存追踪分配器
~trace_allocator()
析构函数
void print_stacktrace() const
打印所有未释放内存的调用栈
void deallocate(pointer p) noexcept
释放单个元素的内存
trace_allocator()=default
默认构造函数
NEFORCE_NODISCARD NEFORCE_ALLOC_OPTIMIZE pointer allocate()
分配单个元素的内存
NEFORCE_NODISCARD NEFORCE_ALLOC_OPTIMIZE pointer allocate(const size_type n)
分配内存
trace_allocator & operator=(const trace_allocator &other)
拷贝赋值运算符
void deallocate(pointer p, const size_type n) noexcept
释放内存
trace_allocator(const trace_allocator &other)
拷贝构造函数
无序映射容器
控制台输入输出工具
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_INLINE17 constexpr bool is_allocable_v
is_allocable的便捷变量模板
void printcln(const color &color, Args &&... args)
带颜色打印多个值并换行
standard_allocator< T > allocator
标准分配器别名
uint64_t size_t
无符号大小类型
int64_t ptrdiff_t
指针差类型
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
bool operator!=(const trace_allocator< T > &lhs, const trace_allocator< U > &rhs) noexcept
不等比较运算符
bool operator==(const trace_allocator< T > &lhs, const trace_allocator< U > &rhs) noexcept
相等比较运算符
堆栈跟踪工具
分配器绑定模板
无序映射容器