C++多线程环境中进行内存分配跟踪的接口类设计(全局重载new/delete操作符)

本文主要是介绍C++多线程环境中进行内存分配跟踪的接口类设计(全局重载new/delete操作符),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

通过全局重载newdelete操作符,实现堆区空间的分配和释放的跟踪记录

// Memory.h
#if TRACK_MEMORY
#ifdef PLATFORM_WINDOWS_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new(size_t size);_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new[](size_t size);_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new(size_t size, const char* desc);_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new[](size_t size, const char* desc);_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new(size_t size, const char* file, int line);_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new[](size_t size, const char* file, int line);void __CRTDECL operator delete(void* memory);
void __CRTDECL operator delete(void* memory, const char* desc);
void __CRTDECL operator delete(void* memory, const char* file, int line);
void __CRTDECL operator delete[](void* memory);
void __CRTDECL operator delete[](void* memory, const char* desc);
void __CRTDECL operator delete[](void* memory, const char* file, int line);#define hnew new(__FILE__, __LINE__)	// 源文件、行号,用于跟踪进行内存分配的位置
#define hdelete delete#else#warning "Memory tracking not available on non-Windows platform"
#define hnew new
#define hdelete delete#endif#else#define hnew new
#define hdelete delete#endif
// Memory.cpp
#if TRACK_MEMORY && PLATFORM_WINDOWS// windows平台的MSVC编译器的标注和属性
_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR	
void* __CRTDECL operator new(size_t size)
{return Allocator::Allocate(size);	// 分配一块大小为 size 字节的内存。
}_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new[](size_t size)
{return Allocator::Allocate(size);	
}_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new(size_t size, const char* desc)
{return Allocator::Allocate(size, desc);	// 分配一块大小为 size 字节的内存,并附带一个描述字符串。
}_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new[](size_t size, const char* desc)
{return Allocator::Allocate(size, desc);
}_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new(size_t size, const char* file, int line)
{return Allocator::Allocate(size, file, line);	// 分配一块大小为 size 字节的内存,并记录文件名和行号。
}_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(size) _VCRT_ALLOCATOR
void* __CRTDECL operator new[](size_t size, const char* file, int line)
{return Allocator::Allocate(size, file, line);
}void __CRTDECL operator delete(void* memory)
{return Allocator::Free(memory);
}void __CRTDECL operator delete(void* memory, const char* desc)
{return Allocator::Free(memory);
}void __CRTDECL operator delete(void* memory, const char* file, int line)
{return Allocator::Free(memory);
}void __CRTDECL operator delete[](void* memory)
{return Allocator::Free(memory);
}void __CRTDECL operator delete[](void* memory, const char* desc)
{return Allocator::Free(memory);
}void __CRTDECL operator delete[](void* memory, const char* file, int line)
{return Allocator::Free(memory);
}#endif

自定义内存分配接口

// Memory.h
#pragma once#include <map>
#include <mutex>// 用于记录整个程序内存分配的情况
struct AllocationStats
{size_t TotalAllocated = 0;size_t TotalFreed = 0;
};// 用于记录单个内存分配的信息
struct Allocation
{void* Memory = 0;size_t Size = 0;const char* Category = 0;	// 描述信息,比如记录申请内存分配的代码的位置,该内存的用处等等
};// 对外接口,用于获取记录分配情况的静态对象(仅Memory.cpp可见)
namespace Memory
{const AllocationStats& GetAllocationStats();
}// Map Allocator 自定义的内存分配器,用于管理std::map的键值对的内存分配,
template <class T>
struct Mallocator
{typedef T value_type;Mallocator() = default;template <class U> constexpr Mallocator(const Mallocator <U>&) noexcept {}T* allocate(std::size_t n){
#undef max// 64位操作系统最大寻址内存值为2^64,因此要保证传入的n是小于这个的if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))throw std::bad_array_new_length();if (auto p = static_cast<T*>(std::malloc(n * sizeof(T)))) {return p;}throw std::bad_alloc();}void deallocate(T* p, std::size_t n) noexcept {std::free(p);}
};struct AllocatorData
{// 2个自定义分配器,分别用于管理std::map中的 这种键值对的内存分配: // key:                   value:// const void* const  --  Allocation// const char* const  --  AllocationStatsusing MapAlloc = Mallocator<std::pair<const void* const, Allocation>>;using StatsMapAlloc = Mallocator<std::pair<const char* const, AllocationStats>>;using AllocationStatsMap = std::map<const char*, AllocationStats, std::less<const char*>, StatsMapAlloc>;// 两个std::map容器// key:内存地址;value: Allocation结构体,记录了内存的指针、大小、描述信息std::map<const void*, Allocation, std::less<const void*>, MapAlloc> m_AllocationMap;// key:描述信息;value: 内存总共分配数量、释放数量AllocationStatsMap m_AllocationStatsMap;std::mutex m_Mutex, m_StatsMutex;
};// 内存分配器接口定义
class Allocator
{
public:static void Init();static void* AllocateRaw(size_t size);static void* Allocate(size_t size);static void* Allocate(size_t size, const char* desc);static void* Allocate(size_t size, const char* file, int line);static void Free(void* memory);static const AllocatorData::AllocationStatsMap& GetAllocationStats() { return s_Data->m_AllocationStatsMap; }
private:inline static AllocatorData* s_Data = nullptr;
};
#include "Memory.h"#include <memory>
#include <map>
#include <mutex>#include "Log.h"
// 用于记录全局内存分配、释放的信息
static Hazel::AllocationStats s_GlobalStats;// 分配器是否正在进行初始化操作(应付多线程)
static bool s_InInit = false;// 初始化阶段,主要是分配一个静态的AllocatorData对象(lazy 初始化)
void Allocator::Init()
{if (s_Data)return;s_InInit = true;AllocatorData* data = (AllocatorData*)Allocator::AllocateRaw(sizeof(AllocatorData));new(data) AllocatorData();	// 定位new(placement new)在指定地址构造目标对象,并调用构造函数初始化,释放需要调用operator deletes_Data = data;s_InInit = false;
}// 利用malloc进行原始内存分配(即不会调用构造和析构),记得手动调用Allocator::free
void* Allocator::AllocateRaw(size_t size)
{return malloc(size);
}void* Allocator::Allocate(size_t size)
{// 如果一个线程正在执行Init()函数,分配请求用原始内存分配来处理if (s_InInit)return AllocateRaw(size);if (!s_Data)Init();void* memory = malloc(size);{std::scoped_lock<std::mutex> lock(s_Data->m_Mutex);Allocation& alloc = s_Data->m_AllocationMap[memory];	// 没有该key就创建,有就返回alloc.Memory = memory;alloc.Size = size;s_GlobalStats.TotalAllocated += size;}return memory;
}// 分配带有描述信息的内存,这个内存不仅要记录到总分配内存计数器中,还要把这种类型的内存单独进行计数
void* Allocator::Allocate(size_t size, const char* desc)
{if (!s_Data)Init();void* memory = malloc(size);{std::scoped_lock<std::mutex> lock(s_Data->m_Mutex);Allocation& alloc = s_Data->m_AllocationMap[memory];alloc.Memory = memory;alloc.Size = size;alloc.Category = desc;s_GlobalStats.TotalAllocated += size;if (desc)s_Data->m_AllocationStatsMap[desc].TotalAllocated += size; // 单独计数}return memory;
}
// line没用到,目前只想逐源文件记录内存分配量
void* Allocator::Allocate(size_t size, const char* file, int line)
{if (!s_Data)Init();void* memory = malloc(size);{std::scoped_lock<std::mutex> lock(s_Data->m_Mutex);Allocation& alloc = s_Data->m_AllocationMap[memory];alloc.Memory = memory;alloc.Size = size;alloc.Category = file;s_GlobalStats.TotalAllocated += size;s_Data->m_AllocationStatsMap[file].TotalAllocated += size;}return memory;
}void Allocator::Free(void* memory)
{if (memory == nullptr)return;{// map中有,计数更新并移除bool found = false;{std::scoped_lock<std::mutex> lock(s_Data->m_Mutex);auto allocMapIt = s_Data->m_AllocationMap.find(memory);found = allocMapIt != s_Data->m_AllocationMap.end();if (found)	{const Allocation& alloc = allocMapIt->second;s_GlobalStats.TotalFreed += alloc.Size;if (alloc.Category)s_Data->m_AllocationStatsMap[alloc.Category].TotalFreed += alloc.Size;s_Data->m_AllocationMap.erase(memory);}}if (!found)LOG("Memory", "Memory block {0} not present in alloc map", memory);}free(memory);
}namespace Memory {const AllocationStats& GetAllocationStats() { return s_GlobalStats; }
}

这篇关于C++多线程环境中进行内存分配跟踪的接口类设计(全局重载new/delete操作符)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/636752

相关文章

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr