1#ifndef MSTL_CORE_MEMORY_TRACE_MEMORY_HPP__
2#define MSTL_CORE_MEMORY_TRACE_MEMORY_HPP__
3#ifdef MTL_STATE_DEBUG__
4#include "../container/unordered_map.hpp"
5#include "../system/console.hpp"
6#include "../system/stacktrace.hpp"
15#ifdef MTL_STATE_DEBUG__
18class trace_allocator {
19 static_assert(is_allocable_v<T>,
"allocator can`t alloc void, reference, function or const type.");
24 using const_pointer =
const T*;
26 using const_reference =
const T&;
32 using other = trace_allocator<U>;
35 trace_allocator() =
default;
38 trace_allocator(
const trace_allocator<U>& a) : traces_(a.traces_) {}
39 trace_allocator&
operator =(
const trace_allocator& a) {
46 if (!traces_.empty()) {
47 _MSTL printcln(color::red(),
"Memory leaks detected! \n");
52 void print_stacktrace()
const {
53 for(
auto& entry : traces_) {
54 if (entry.first == 0)
continue;
55 _MSTL printcln(color::red(),
"Leaked pointer: ",
static_cast<void*
>(entry.first));
56 _MSTL printcln(color::red(),
"Allocation stack trace:\n", entry.second);
72 auto it = traces_.find(p);
73 if (it != traces_.end()) {
84 _MSTL unordered_map<T*, _MSTL stacktrace> traces_;
87template <
typename T,
typename U>
88bool operator ==(
const trace_allocator<T>&,
const trace_allocator<U>&)
noexcept {
92template <
typename T,
typename U>
93bool operator !=(
const trace_allocator<T>&,
const trace_allocator<U>&)
noexcept {
MSTL_ALLOC_NODISCARD MSTL_CONSTEXPR20 static MSTL_ALLOC_OPTIMIZE pointer allocate()
分配单个元素内存
_INNER alloc_size_t size_type
大小类型
static MSTL_CONSTEXPR20 void deallocate(pointer p, const size_type n) noexcept
释放先前分配的内存
MSTL_CONSTEXPR20 standard_allocator & operator=(const standard_allocator &) noexcept=default
赋值运算符
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
bool operator!=(const function< Res(Args...)> &f, nullptr_t null) noexcept
不等于空指针比较
bool operator==(const function< Res(Args...)> &f, nullptr_t null) noexcept
等于空指针比较
standard_allocator< T > allocator
标准分配器别名
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素