资源简介
mtrace可以用来 跟踪当前 系统/模块的内存 使用情况 ,以及估算系统/模块的占用内存的极值 , 系统/模块 卸载时的 内存 泄露情况。
代码片段和文件信息
#include
#include “mtrace.h“
#if( FS_TRACE_MEM>2)
#elif( FS_TRACE_MEM>0) //mode 12
#define MH_MAGIC_MASK 0xFFFFFFFC
#define MH_TYPE_MASK 0x00000003
#define MH_MAGIC_ALLOC 0xCCCCCCCC
#define MH_MAGIC_FREE 0xEEEEEEEE
#define MH_TYPE_C 0
#define MH_TYPE_M 1
#define MH_TYPE_R 2
typedef struct
{
int magic; //0xcccc low nibble for type
int size;
#if( FS_TRACE_MEM&2)
int logindex;
#endif
}meminfo;
#define meminfo_len sizeof(meminfo)
DEFINE_MEMSTAT(default_memstat); //total mem trace for all fs
#if( FS_TRACE_MEM&2)
#define MAX_ALLOC_FILNAMELEN 32
#define MEMLOG_STATEFREE 0
#define thisfs_memlog stat->log
static void init_memlog(memstat *stat)
{
if(thisfs_memlog.flag&memstat_log_inited)
return ;
#if 0
char *log =(char *) raw_calloc(sizeof(char *)thisfs_memlog.limit);
if (!log)
{
mtrace_log_error(“Failed to calloc %d logs\n“ (long)thisfs_memlog.limit);
return ;
}
thisfs_memlog.logstr = (char **)log;
#endif
for (int i=0;i {
thisfs_memlog.logstr[i]=MEMLOG_STATEFREE;
}
thisfs_memlog.max_index=-1;
thisfs_memlog.free_index=0;
thisfs_memlog.alloc_count=0;
thisfs_memlog.flag |= memstat_log_inited;
if(default_memstat.log.flag&memstat_log_inited)
return ;
// default_memstat.log.limit = MAX_ALLOC_ENTRYS;
#if 0
log =(char *) raw_calloc(sizeof(char *)default_memstat.log.limit);
if (!log)
{
mtrace_log_error(“Failed to calloc %d logs\n“ (long)default_memstat.log.limit);
return ;
}
thisfs_memlog.logstr = (char **)log;
#endif
for ( i=0;i < default_memstat.log.limit;i++)
{
default_memstat.log.logstr[i]=MEMLOG_STATEFREE;
}
default_memstat.log.max_index=-1;
default_memstat.log.free_index=0;
default_memstat.log.alloc_count=0;
default_memstat.log.flag |= memstat_log_inited;
}
#define logfmt “%d%d%d%s“
static size_t o_size;
static char o_file[MAX_ALLOC_FILNAMELEN];
static int o_line;
static int o_times;
static int register_log(meminfo *pint line char *filememstat *stat)
{
int ij;
int retry=0;
size_t size = p ->size;
init_memlog(stat);
p->logindex = memstat_log_index_invalid;
//use the already log entry
for( i=0 ;i<=thisfs_memlog.max_index;i++)
{
if(thisfs_memlog.logstr[i]==MEMLOG_STATEFREE)
continue;
sscanf(thisfs_memlog.logstr[i] logfmt &o_times&o_size&o_line o_file);
if(/*(o_size==size)&&*/(o_line==line)&&(strcmp(o_filefile)==0)&&o_times>0)
{
snprintf((char*) thisfs_memlog.logstr[i] MAX_ALLOC_FILNAMELEN logfmt o_times+1size+o_sizelinefile);
p->logindex = i;
return 0;
}
}
//create new log
if(thisfs_memlog.flag & memstat_log_allused)
{
mtrace_log_error(“all used out now\n“);
return -1;
}
j=thisfs_memlog.free_index;
if(thisfs_memlog.logstr[j])
{
mtrace_log_error(“already used now\n“)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 754 2010-08-10 10:38 config.h
文件 11338 2010-08-10 10:36 mtrace.c
文件 2650 2010-08-10 10:27 mtrace.h
----------- --------- ---------- ----- ----
14742 3
- 上一篇:简易绘图的制作 (一)WPF
- 下一篇:武汉大学本科生论文格式自动排版工具
评论
共有 条评论