c++代码检测

code_test.h

/*
 * code_test.h
 *
 *  Created on: 2013-3-28
 *      Author: hp
 */

#ifndef CODE_TEST_H_
#define CODE_TEST_H_

#include <map>
#include "windef.h"

using namespace std;

struct ALLOC_INFO{
	DWORD address;
	DWORD size;
	char file[64];
	DWORD line;
};

class MyTestMalloc
{
public:
	static void CoutResult();
	static void AddTrack(DWORD addr, DWORD asize, const char *fname,
			DWORD lnum);
	static void RemoveTrack(DWORD addr, const char *fname, DWORD lnum);
	static int malloc_free_sub;
	static map<DWORD, ALLOC_INFO>* alloc_map;
};

//Redefined functions
void* mallocb(size_t size, const char* pszFile, int nLine);
void* reallocb(void* memblock, size_t size, const char* pszFile, int nLine);
void* callocb(size_t num, size_t size, const char* pszFile, int nLine);
char* strdupb(const char* chs, const char* pszFile, int nLine);
void  freeb(void* memblock, const char* pszFile, int nLine);

//For C++
void* operator new(size_t size, const char* pszFile, int nLine);

void* operator new[](size_t size, const char* pszFile, int nLine);

void operator delete(void* memblock) throw();

void operator delete[](void* memblock) throw();


#endif /* CODE_TEST_H_ */

detect.h

#ifndef DETECT_H
#define DETECT_H

#ifdef __QX_DEBUG_CODE_TEST__

#include "code_test.h"

//Redefine new and delete
#define new 	new(__FILE__, __LINE__)
//Redefines
#define malloc(size) mallocb(size, __FILE__, __LINE__)
#define realloc(memblock, size) reallocb(memblock, size, __FILE__, __LINE__)
#define calloc(num, size) callocb(num, size, __FILE__, __LINE__)
#define strdup(ch) strdupb(chs, __FILE__, __LINE__)
#define free(memblock) freeb(memblock, __FILE__, __LINE__)

#endif

#endif

code_test.cpp

/*
 * code_test.cpp
 *
 *  Created on: 2013-3-28
 *      Author: hp
 */

#include "code_test/code_test.h"

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int MyTestMalloc::malloc_free_sub = 0;
map<DWORD, ALLOC_INFO>* MyTestMalloc::alloc_map = 0;


void MyTestMalloc::AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
{
	MyTestMalloc::malloc_free_sub++;
	if (!MyTestMalloc::alloc_map)
	{
		MyTestMalloc::alloc_map = new map<DWORD, ALLOC_INFO>;
	}

	ALLOC_INFO info;
	memset(&info, 0, sizeof(info));
	info.address = addr;
	strncpy(info.file, fname, 63);
	info.line = lnum;
	info.size = asize;

	MyTestMalloc::alloc_map->insert(make_pair(addr, info));
}

void MyTestMalloc::RemoveTrack(DWORD addr, const char *fname, DWORD lnum)
{
	//printf("size:%d\n", MyTestMalloc::alloc_map->size());
	if (MyTestMalloc::alloc_map->size() == 0)
	{
		return;
	}

	size_t t = MyTestMalloc::alloc_map->erase(addr);
	if(t == 1)
	{
		MyTestMalloc::malloc_free_sub--;
	}
}

void MyTestMalloc::CoutResult()
{
	printf("\n---------check memory-------------------------------------- \n");
	printf("malloc_free_sub:%d\n", malloc_free_sub);

	DWORD totalSize = 0;
	char buf[128];
	map<DWORD, ALLOC_INFO>::iterator iter = MyTestMalloc::alloc_map->begin();
	for (; iter != MyTestMalloc::alloc_map->end(); iter++)
	{
		memset(buf, 0, 128);
		sprintf(buf, "%s\t line:%ld\t address:%ld\t unfreed:%ld ", iter->second.file,
				iter->second.line, iter->second.address, iter->second.size);
		printf(buf);
		totalSize += iter->second.size;
	}
	memset(buf, 0, 128);
	sprintf(buf, "\n\nTotal Unfreed: %ld bytes ", totalSize);
	printf(buf);
	printf("\n-----------------------------------------------------------\n");

	if(MyTestMalloc::alloc_map)
	{
		delete MyTestMalloc::alloc_map;
	}
}

void* mallocb(size_t size, const char *pszFile, int nLine)
{
	void* ptr = malloc(size);
	MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);
	return ptr;
}

void* reallocb(void *memblock, size_t size, const char *pszFile, int nLine)
{
    void * ptr;
    ptr = realloc(memblock, size);
    MyTestMalloc::RemoveTrack((DWORD)memblock, pszFile, nLine);
    MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);
    return ptr;
}

void* callocb(size_t num, size_t size, const char *pszFile, int nLine)
{
	void* ptr = calloc(num, size);
	MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);
	return ptr;
}

char* strdupb(const char* chs, const char* pszFile, int nLine)
{
	char* ptr = strdup(chs);
	MyTestMalloc::AddTrack((DWORD)ptr, strlen((char*)chs), pszFile, nLine);
	return ptr;
}

void freeb(void *memblock, const char *pszFile, int nLine)
{
	MyTestMalloc::RemoveTrack((DWORD)memblock, pszFile, nLine);

	free(memblock);
}

void pre_delete(const char* pszFile, int nLine)
{

}

void* operator new(size_t size, const char* pszFile, int nLine)
{
	void* ptr = malloc(size);
	MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);
	return ptr;
}

void* operator new[](size_t size, const char* pszFile, int nLine)
{
	void * ptr = malloc(size);

	MyTestMalloc::AddTrack((DWORD)ptr, size, pszFile, nLine);
	return ptr;
}

void operator delete(void* memblock) throw()
{
	if (memblock)
	{
		MyTestMalloc::RemoveTrack((DWORD)memblock, "", -1);
	}

	free(memblock);
}

void operator delete[](void* memblock) throw()
{
	if (memblock)
	{
		MyTestMalloc::RemoveTrack((DWORD)memblock, "", -1);
	}

	free(memblock);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值