📄 umc_malloc.cpp
字号:
/* /////////////////////////////////////////////////////////////////////////////// *//*//// INTeL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright (c) 2005 Intel Corporation. All Rights Reserved.////*/#define VM_MALLOC_OWN#include "umc_malloc.h"#if defined(VM_MALLOC)#include <string.h>#include <stdio.h>#define vm_args const char* lpcFileName, vm_var32 iStringNumber, const char *lpcClassNameclass VM_MallocItem{public: void* m_lpv; vm_var32s m_size; const char* m_lpcFileName; vm_var32 m_iStringNumber; const char* m_lpcClassName; void Init(void* lpv, vm_var32s size, vm_args) { m_lpv = lpv; m_size = size; m_lpcFileName = lpcFileName; m_iStringNumber = iStringNumber; m_lpcClassName = lpcClassName; } static void ClearLog() { if(FILE *f = fopen("c:/malloc.csv", "w")) { fprintf(f, "class, block size, source file, string number\n"); fclose(f); } } void Report(vm_var32s size) { if(FILE *f = fopen("c:/malloc.csv", "a")) { fprintf(f, "%s, %d, %s, %d\n", 0 == m_lpcClassName ? "Unknown" : m_lpcClassName, size, 0 == m_lpcFileName ? "Unknown" : m_lpcFileName, m_iStringNumber); fclose(f); } } void ReportLeak() { if(FILE *f = fopen("c:/malloc.csv", "a")) { fprintf(f, "Memory leak: %s, %d, %s, %d\n", 0 == m_lpcClassName ? "Unknown" : m_lpcClassName, m_size, 0 == m_lpcFileName ? "Unknown" : m_lpcFileName, m_iStringNumber); fclose(f); } } void ReportAlloc() { Report( m_size); } void ReportFree () { Report(-m_size); }};VM_MallocItemArray vm_malloc_array;#define _vm_deleteMallocItem(lpv)\ vm_malloc_array.DeleteItem(lpv);#define _vm_addMallocItem\ VM_MallocItem* item = (VM_MallocItem*) malloc(sizeof(VM_MallocItem));\ item->Init(lpv, size, lpcFileName, iStringNumber, lpcClassName);\ vm_malloc_array.AddItem(item);VM_MallocItemArray::VM_MallocItemArray(){ m_array = 0; m_count = 0; m_allocated = 0; m_mem_usage_max = 0; m_mem_usage_current = 0;#if defined(VM_MALLOC_STATISTIC) VM_MallocItem::ClearLog();#endif}VM_MallocItemArray::~VM_MallocItemArray(){ char tmp[] = "Maximum usage"; VM_MallocItem item; item.Init(0, 0, 0, 0, tmp); item.Report(m_mem_usage_max); for(vm_var32 i=0; i<m_count; i++) {#if defined(VM_MALLOC_STATISTIC) if(m_array[i]->m_size > 0) m_array[i]->ReportLeak();#endif delete m_array[i]; } m_array = 0; m_count = 0; m_allocated = 0; m_mem_usage_max = 0; m_mem_usage_current = 0;}void VM_MallocItemArray::AddItem(VM_MallocItem* item){ if(m_count == m_allocated) { m_allocated += 10; m_array = (VM_MallocItem**)realloc(m_array, m_allocated*sizeof(VM_MallocItem*)); } m_array[m_count++] = item; vm_malloc_array.ChangeMemUsage(item->m_size);#if defined(VM_MALLOC_STATISTIC) item->ReportAlloc();#endif}void VM_MallocItemArray::DeleteItem(void* lpv){ for(vm_var32 i=0; i<m_count; i++) { if(m_array[i]->m_lpv == lpv) {#if defined(VM_MALLOC_STATISTIC) m_array[i]->ReportFree();#endif vm_malloc_array.ChangeMemUsage(-vm_var32s(m_array[i]->m_size)); delete m_array[i]; m_array[i] = m_array[--m_count]; } }}void VM_MallocItemArray::ChangeMemUsage(vm_var32s size){ m_mem_usage_current += size; if(m_mem_usage_current > m_mem_usage_max) m_mem_usage_max = m_mem_usage_current;}void* vm_malloc(vm_var32s size, vm_args){ void* lpv = malloc(size); _vm_addMallocItem; return lpv;}void* vm_calloc(size_t num, vm_var32s size, vm_args){ void* lpv = calloc(num, size); size *= num; _vm_addMallocItem; return lpv;}void* vm_realloc(void *lpv, vm_var32s size, vm_args){ void *lpv_old = lpv; lpv = realloc(lpv, size); _vm_addMallocItem; _vm_deleteMallocItem(lpv_old); return lpv;}void vm_free(void *lpv){ _vm_deleteMallocItem(lpv); free(lpv);}#define _ippsMalloc(type)\Ipp##type* vm_ippsMalloc_##type(vm_var32s size, vm_args)\{\ Ipp##type* lpv = ippsMalloc_##type(size);\ _vm_addMallocItem;\ return lpv;\}_ippsMalloc(8u )_ippsMalloc(16u )_ippsMalloc(32u )_ippsMalloc(8s )_ippsMalloc(16s )_ippsMalloc(32s )_ippsMalloc(64s )_ippsMalloc(32f )_ippsMalloc(64f )_ippsMalloc(8sc )_ippsMalloc(16sc)_ippsMalloc(32sc)_ippsMalloc(64sc)_ippsMalloc(32fc)_ippsMalloc(64fc)void vm_ippsFree(void *lpv){ _vm_deleteMallocItem(lpv); ippsFree(lpv);}void vm_malloc_measure(vm_var32 start, vm_args){ static void* lpv = 0; vm_var32s size; static MEMORYSTATUSEX status_before = {0,}; static MEMORYSTATUSEX status_after = {0,}; status_before.dwLength = sizeof(status_before); status_after .dwLength = sizeof(status_after); if(start) { GlobalMemoryStatusEx(&status_before); lpv = (void*)lpcFileName; } if(!start) { GlobalMemoryStatusEx(&status_after); size = (vm_var32s)max(0, (vm_var64s)(status_before.ullAvailPageFile - status_after.ullAvailPageFile)); _vm_addMallocItem; item->m_size = 0; // Disable memory leak reporting for this block. }}void* __cdecl operator new(size_t size){ return vm_malloc(size);}void* __cdecl operator new(size_t size, vm_args){ return vm_malloc(size, lpcFileName, iStringNumber, lpcClassName);}void __cdecl operator delete(void *lpv){ vm_free(lpv);}void __cdecl operator delete(void *lpv, const char*, vm_var32, const char *){ vm_free(lpv);}#endif //VM_MALLOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -