Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370705
  • 博文数量: 56
  • 博客积分: 1449
  • 博客等级: 中尉
  • 技术积分: 822
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-08 10:24
文章分类

全部博文(56)

文章存档

2014年(7)

2012年(13)

2011年(10)

2010年(26)

分类: C/C++

2011-05-14 18:13:51

  今天在写TRIE的简单实现.考虑到动态内存分配会非常频繁.于是就对malloc和free的调用包裹了一层.一来
及时发现内存泄漏.二来可以较快的定位double free或者malloc memory corrupted这样的错误的位置.其实很
早就想写的.可一直懒得烦.

  的确比较的方便.TRIE的内存泄漏的问题全靠它来简单的判断了.
  
  比如我的运行结果:
  
  1. [XXXXXXX@lt simple_trie]$ ./test_simple_trie | grep alloc | wc -l
  2. 3027
  3. [XXXXXXX@lt simple_trie]$ ./test_simple_trie | grep free | wc -l
  4. 3026
  这里的free闭alloc少一次是由于根节点没有释放.所以少一次.
  这样就可以乐观的确信没有内存泄漏...但绝对不是百分之百.
  代码如下:
  1. /*
  2.  * This is a simple wrapers around the malloc-free family function.
  3.  * It can help quickly finding out memory leaks and positioning "double free" 
  4.  * or "malloc memory corrupted" like problems.
  5.  * any suggestions are welcomed.
  6.  *         Email: liangtao@gamil.com
  7.  *         2011/5/14
  8.  */

  9. #ifndef WRAP_MALLOC_H
  10. #define WRAP_MALLOC_H
  11. #include <stdint.h>
  12. #include <stdlib.h>

  13. #ifdef _DEBUG_MALLOC_

  14. static unsigned long total_malloced;

  15. #define WRAP_malloc(__size__) ({\
  16. uint8_t *ret;\
  17. do {\
  18.     total_malloced += (__size__);\
  19.     printf("[malloc]:%-20s %-20s %-4d %-6d", __FILE__, __func__, \
  20.                         __LINE__, (__size__));\
  21.     ret = (uint8_t *)malloc((__size__) + sizeof(uint32_t));\
  22.     *(uint32_t *)ret = (__size__);\
  23.     printf(" addr: %x\n", (unsigned int)ret); \
  24.     if (total_malloced < 1024) \
  25. printf("[Total]:Total memory consumed %-6ld B\n", total_malloced);\
  26.    else if (total_malloced > 1024 && total_malloced < 1024 * 1024) \
  27. printf("[Total]:Total memory consumed %-4.1f KB\n", total_malloced/1024.0);\
  28.    else\
  29. printf("[Total]:Total memory consumed %-4.1f MB\n", total_malloced / 1024.0 / 1024.0)
  30. } while(0); \
  31. (void *)(ret + sizeof(uint32_t)); })


  32. #define WRAP_calloc(__n__, __size__) ({\
  33. uint8_t *ret;\
  34. do {\
  35.     total_malloced += (__n__) * (__size__);\
  36.     printf("[calloc]:%-20s %-20s %-4d %-6d", __FILE__, __func__, \
  37.                     __LINE__,(__n__) * (__size__));\
  38.     ret = (uint8_t *)calloc((__n__) , (__size__) + sizeof(uint32_t));\
  39.     *(uint32_t *)ret = (__n__) * (__size__);\
  40.     printf(" addr: %x\n", (unsigned int)ret); \
  41.     if (total_malloced < 1024) \
  42.           printf("[Total]:Total memory consumed %-6ld B\n", total_malloced);\
  43.   else if (total_malloced > 1024 && total_malloced < 1024 * 1024) \
  44.           printf("[Total]:Total memory consumed %-4.1f KB\n", total_malloced/1024.0);\
  45.    else\
  46.   printf("[Total]:Total memory consumed %-4.1f MB\n", total_malloced / 1024.0 / 1024.0)
  47. } while (0);\
  48. (ret + sizeof(uint32_t)); })




  49. #define WRAP_free(p) \
  50. do {\
  51.     if (!p)\
  52.         break;\
  53.     uint32_t size = *(uint32_t *)((uint8_t *)(p) - sizeof(uint32_t)); \
  54.     total_malloced -= size;\
  55.     printf("[free]\t:%-20s %-20s %-4d %-6d", __FILE__, __func__, \
  56.                         __LINE__, size);\
  57.     printf(" addr: %x\n", (unsigned int)((uint8_t *)(p) - sizeof(uint32_t)));\
  58.     if (total_malloced < 1024) \
  59.           printf("[Total]:Total memory consumed %-6ld B\n", total_malloced);\
  60.   else if (total_malloced > 1024 && total_malloced < 1024 * 1024) \
  61.           printf("[Total]:Total memory consumed %-4.1f KB\n", total_malloced/1024.0);\
  62.    else\
  63.   printf("[Total]:Total memory consumed %-4.1f MB\n", total_malloced / 1024.0 / 1024.0)
  64.     free((uint8_t *)(p) - sizeof(uint32_t));\
  65. } while (0)



  66. #define OOM() \
  67. do {\
  68.     printf("failed to alloc memory.OOM!%-20s %-20s %-6d\n", __FILE__, __func__, __LINE__); \
  69. } while (0)

  70. #else

  71. #define WRAP_malloc(__size__) malloc(__size__)

  72. #define WRAP_calloc(__n__, __size__) calloc(__n__, __size__)

  73. #define WRAP_free(p) free(p)

  74. #define OOM()
  75. #endif

  76. #endif
阅读(2540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~