13 Star 69 Fork 22

johnsonyl/cpps

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cpps_new.cpp 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
johnsonyl 提交于 2021-01-26 14:47 +08:00 . 2021-01-26 更新
#include "cpps/cpps.h"
#ifdef USE_NEDMALLOC
#include "nedmalloc.h"
#endif
cpps::memory_allocal::memory_allocal()
{
handler = NULL;
_real = false;
}
cpps::memory_allocal::~memory_allocal()
{
if (_real) {
#ifdef _DEBUG
handler->dump();
#endif
delete handler;
handler = NULL;
}
}
void cpps::memory_allocal::init()
{
if (handler == NULL) {
_real = true;
handler = new memory_allocal_handler();
}
}
cpps::memory_allocal_handler* cpps::memory_allocal::gethandler()
{
return handler;
}
void cpps::memory_allocal::sethandler(memory_allocal_handler* h)
{
handler = h;
}
cpps::memory_allocal& cpps::memory_allocal::instance()
{
static memory_allocal ins;
return ins;
}
void* cpps::memory_allocal_handler::mmalloc(size_t __size, const char* file, unsigned int _line)
{
#ifdef USE_NEDMALLOC
void* ret = nedalloc::nedmalloc(__size);
#else
#ifdef _WIN32
auto hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, __size);
void* ret = GlobalLock(hMem);
GlobalUnlock(hMem);
#else
void* ret = malloc(__size);
#endif
#endif
#ifdef _DEBUG
_lock.lock();
_size += __size;
memorylist.insert(memory_info_list::value_type(ret, new memory_info(__size, file, _line)));
_lock.unlock();
#endif
return ret;
}
void cpps::memory_allocal_handler::mfree(void* m)
{
#ifdef _DEBUG
_lock.lock();
auto it = memorylist.find(m);
if (it != memorylist.end()) {
auto info = it->second;
_size -= info->size;
delete info;
memorylist.erase(it);
}
_lock.unlock();
if (m == NULL) {
system("pause");
}
#endif
#ifdef USE_NEDMALLOC
nedalloc::nedfree(m);
#else
#ifdef _WIN32
auto pMem = GlobalHandle(m);
GlobalFree(pMem);
#else
free(m);
#endif
#endif
}
#ifdef _DEBUG
size_t cpps::memory_allocal_handler::size()
{
return _size;
}
void cpps::memory_allocal_handler::dump()
{
_lock.lock();
if (!memorylist.empty()) {
char outFileName[256];
//ȡϵͳʱ
sprintf(outFileName, "%s_%d.log", "dump_no_delete_memory.txt", rand());
FILE* pFile = fopen(outFileName, "wb+");
std::string out;
for (auto it : memorylist) {
auto info = it.second;
char buffer[10240];
sprintf(buffer, "file :%s line:%d \r\n", info->file, info->line);
fwrite(buffer, strlen(buffer), 1, pFile);
}
fclose(pFile);
}
_lock.unlock();
}
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cppscript/cpps.git
git@gitee.com:cppscript/cpps.git
cppscript
cpps
cpps
v1.0.0

搜索帮助