#include "cpps/cpps.h"#ifdef USE_NEDMALLOC#include "nedmalloc.h"#endifcpps::memory_allocal::memory_allocal(){handler = NULL;_real = false;mainthreadid = 0;}cpps::memory_allocal::~memory_allocal(){if (_real) {#if defined _DEBUG || RELDEBUGhandler->dump();#endifdelete handler;handler = NULL;}}void cpps::memory_allocal::init(cpps_alloc_f _alloc_func, cpps_free_f _free_func){if (handler == NULL) {_real = true;mainthreadid = cpps_this_thread_get_id();handler = new memory_allocal_handler();handler->cpps_set_allocf(_alloc_func, _free_func);}}bool cpps::memory_allocal::global(){return !(_real && mainthreadid == cpps_this_thread_get_id());}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;}cpps::memory_allocal_handler::memory_allocal_handler(){alloc_func = NULL;free_func = NULL;}void* cpps::memory_allocal_handler::mmalloc(size_t __size, const char* file, unsigned int _line){char* ret = NULL;if (alloc_func != NULL) {ret = (char *)alloc_func(__size);}else {#ifdef USE_NEDMALLOCret = (char*)nedalloc::nedmalloc(__size);#else#ifdef _WIN32//__size = (size_t)ceil(__size / 1024.0) * 1024;//void* ret = (PBYTE)VirtualAlloc(NULL, __size, MEM_COMMIT, PAGE_READWRITE);__size++;ret = memory_allocal::instance().global() ? (char*)VirtualAlloc(NULL, __size, MEM_COMMIT, PAGE_READWRITE) : (char*)malloc(__size);ret[0] = memory_allocal::instance().global() ? 1 : 0;ret = ret + 1;//if(memory_allocal::instance().global())//printf("alloc %s memory...\n", memory_allocal::instance().global() ? "global" : "local");#elseret = (char*) malloc(__size);#endif#endif}#if defined _DEBUG || RELDEBUG#ifdef _WIN32if (ret == NULL) {system("pause");}#endif_lock.lock();_size += __size;memorylist.insert(memory_info_list::value_type(ret, new memory_info(__size, file, _line)));_lock.unlock();#endifreturn ret;}void cpps::memory_allocal_handler::mfree(void* m){#if defined _DEBUG || RELDEBUG_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");}#endifif (free_func != NULL) { free_func(m); return; }#ifdef USE_NEDMALLOCnedalloc::nedfree(m);#else#ifdef _WIN32if (m) {//printf("Thread ID:%d release Address: %I64d \r\n", GetCurrentThreadId(), (size_t)m);char* p = (char*)m;p = p - 1;if (p[0] == 1) VirtualFree(p, 0, MEM_RELEASE); else free(p);}#elseif(m) free(m);#endif#endif}void cpps::memory_allocal_handler::cpps_set_allocf(cpps_alloc_f _alloc_func, cpps_free_f _free_func){alloc_func = _alloc_func;free_func = _free_func;}#if defined _DEBUG || RELDEBUGsize_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);}char buffer[10240];sprintf(buffer, "_size: %I64d Bytes\r\n", _size);fwrite(buffer, strlen(buffer), 1, pFile);fclose(pFile);}_lock.unlock();}#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型