今天在写TRIE的简单实现.考虑到动态内存分配会非常频繁.于是就对malloc和free的调用包裹了一层.一来
及时发现内存泄漏.二来可以较快的定位double free或者malloc memory corrupted这样的错误的位置.其实很
早就想写的.可一直懒得烦.
的确比较的方便.TRIE的内存泄漏的问题全靠它来简单的判断了.
比如我的运行结果:
- [XXXXXXX@lt simple_trie]$ ./test_simple_trie | grep alloc | wc -l
-
3027
-
[XXXXXXX@lt simple_trie]$ ./test_simple_trie | grep free | wc -l
-
3026
这里的free闭alloc少一次是由于根节点没有释放.所以少一次.
这样就可以乐观的确信没有内存泄漏...但绝对不是百分之百.
代码如下:
- /*
-
* This is a simple wrapers around the malloc-free family function.
-
* It can help quickly finding out memory leaks and positioning "double free"
- * or "malloc memory corrupted" like problems.
-
* any suggestions are welcomed.
-
* Email: liangtao@gamil.com
-
* 2011/5/14
-
*/
-
#ifndef WRAP_MALLOC_H
-
#define WRAP_MALLOC_H
-
#include <stdint.h>
-
#include <stdlib.h>
-
-
#ifdef _DEBUG_MALLOC_
-
-
static unsigned long total_malloced;
-
-
#define WRAP_malloc(__size__) ({\
-
uint8_t *ret;\
-
do {\
-
total_malloced += (__size__);\
-
printf("[malloc]:%-20s %-20s %-4d %-6d", __FILE__, __func__, \
-
__LINE__, (__size__));\
-
ret = (uint8_t *)malloc((__size__) + sizeof(uint32_t));\
-
*(uint32_t *)ret = (__size__);\
-
printf(" addr: %x\n", (unsigned int)ret); \
- if (total_malloced < 1024) \
- printf("[Total]:Total memory consumed %-6ld B\n", total_malloced);\
- else if (total_malloced > 1024 && total_malloced < 1024 * 1024) \
- printf("[Total]:Total memory consumed %-4.1f KB\n", total_malloced/1024.0);\
- else\
- printf("[Total]:Total memory consumed %-4.1f MB\n", total_malloced / 1024.0 / 1024.0)
-
} while(0); \
-
(void *)(ret + sizeof(uint32_t)); })
-
-
-
#define WRAP_calloc(__n__, __size__) ({\
-
uint8_t *ret;\
-
do {\
-
total_malloced += (__n__) * (__size__);\
-
printf("[calloc]:%-20s %-20s %-4d %-6d", __FILE__, __func__, \
-
__LINE__,(__n__) * (__size__));\
-
ret = (uint8_t *)calloc((__n__) , (__size__) + sizeof(uint32_t));\
-
*(uint32_t *)ret = (__n__) * (__size__);\
-
printf(" addr: %x\n", (unsigned int)ret); \
- if (total_malloced < 1024) \
- printf("[Total]:Total memory consumed %-6ld B\n", total_malloced);\
- else if (total_malloced > 1024 && total_malloced < 1024 * 1024) \
- printf("[Total]:Total memory consumed %-4.1f KB\n", total_malloced/1024.0);\
- else\
- printf("[Total]:Total memory consumed %-4.1f MB\n", total_malloced / 1024.0 / 1024.0)
-
} while (0);\
-
(ret + sizeof(uint32_t)); })
-
-
-
-
-
#define WRAP_free(p) \
-
do {\
-
if (!p)\
-
break;\
-
uint32_t size = *(uint32_t *)((uint8_t *)(p) - sizeof(uint32_t)); \
-
total_malloced -= size;\
-
printf("[free]\t:%-20s %-20s %-4d %-6d", __FILE__, __func__, \
-
__LINE__, size);\
-
printf(" addr: %x\n", (unsigned int)((uint8_t *)(p) - sizeof(uint32_t)));\
- if (total_malloced < 1024) \
- printf("[Total]:Total memory consumed %-6ld B\n", total_malloced);\
- else if (total_malloced > 1024 && total_malloced < 1024 * 1024) \
- printf("[Total]:Total memory consumed %-4.1f KB\n", total_malloced/1024.0);\
- else\
- printf("[Total]:Total memory consumed %-4.1f MB\n", total_malloced / 1024.0 / 1024.0)
-
free((uint8_t *)(p) - sizeof(uint32_t));\
-
} while (0)
-
-
-
-
#define OOM() \
-
do {\
-
printf("failed to alloc memory.OOM!%-20s %-20s %-6d\n", __FILE__, __func__, __LINE__); \
-
} while (0)
-
-
#else
-
-
#define WRAP_malloc(__size__) malloc(__size__)
-
-
#define WRAP_calloc(__n__, __size__) calloc(__n__, __size__)
-
-
#define WRAP_free(p) free(p)
-
-
#define OOM()
-
#endif
-
-
#endif
阅读(2631) | 评论(0) | 转发(0) |