技术控

    今日:51| 主题:49312
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Flexible C memory allocation scheme with leak checking

[复制链接]
生活如此心酸 发表于 2016-10-14 21:51:53
127 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
mulle_allocator

  ... is a leak and double free checker for tests (and at runtime)
  ... provides a way to pass around the memory scheme du jour
  ... has identical API to malloc, realloc, free
  mulle_allocatorcomes as two libraries:    mulle_allocatorand    mulle_test_allocator.    mulle_test_allocatorprovides the error detection, which you may want to include or leave out.  
  Use    mulle_test_allocatorfor leak detection  

  If you use    mulle_mallocand friends instead of    mallocin your code, you can easily check for leaks during tests using the    mulle_test_allocatorlibrary.  
  So instead of:
  1. malloc( 1848);
  2.    calloc( 18, 48);
  3.    s = strdup( "VfL Bochum 1848");
  4.    realloc( s, 18);
  5.    free( s);
复制代码
write
  1. mulle_malloc( 1848);
  2.    mulle_calloc( 18, 48);
  3.    s = mulle_strdup( "VfL Bochum 1848");
  4.    mulle_realloc( s, 18);
  5.    mulle_free( s);
复制代码
Then wrap your test code inside
  1. mulle_test_allocator_initialize();
  2. mulle_default_allocator = mulle_test_allocator;
  3. {
  4.    // do stuff
  5. }
  6. mulle_test_allocator_reset();
复制代码
and    mulle_test_allocator_resetwill tell you about your leaks. All    mulle_test_allocatorroutines will check for erroneus frees and wrong pointers.  
  Use    mulle_allocatorto make your code more flexible  

  You can make your code, and especially your data structures, more flexible by using    mulle_allocatorAs this decoupled your data structure from    stdib, it enables your data structure to reside in shared memory for example with no additional code. Your API consumers just have to pass their own allocators.  
  Also it it helpful to isolate your datastructure memory allocation during tests. This way, other, possibly benign, code leaks, do not obscure the test.
  What is an allocator ?

  The    mulle_allocatorstruct is mostly a collection of function pointers and looks like this:  
  1. struct mulle_allocator
  2. {
  3.    void   *(*calloc)( size_t n, size_t size);
  4.    void   *(*realloc)( void *block, size_t size);
  5.    void   (*free)( void *block);
  6.    int    (*abafree)( void *aba, void (*free)( void *), void *block);
  7.    void   *aba;
  8. };
复制代码
By default    .abaand    .abafreeare not available. If you need ABA safe freeing, it is recommended to use    mulle-aba.  
  In general you will not jump through the vectors directly, but use supplied inline functions like    mulle_allocator_mallocto allocate memory using the allocator.  
  A pointer to this structure could be passed to your data structure code. It would use the allocator in this fashion:
  1. struct my_string
  2. {
  3.    struct mulle_allocator   *allocator;
  4.    char                     s[ 1];
  5. };
  6. struct my_string   *my_string_alloc( char *s, struct mulle_allocator *allocator)
  7. {
  8.    size_t              len;
  9.    struct my_string    *p;
  10.    len = s ? strlen( s) : 0;
  11.    p   = mulle_allocator_malloc( allocator, sizeof( struct my_string) + len);
  12.    if( p)
  13.    {
  14.       dst->allocator = allocator;
  15.       memcpy( p->s, s, len);
  16.       p->s[ len] = 0;
  17.    }
  18.    return( p);
  19. }
  20. static inline void   my_string_free( struct my_string *p)
  21. {
  22.    if( p)
  23.       mulle_allocator_free( p->allocator, p);
  24. }
复制代码
But if you don't want to store the allocator inside the data structure, you can of course also just pass it in again:
  1. struct my_other_string
  2. {
  3.    char   s[ 1];
  4. };
  5. struct my_other_string   *my_other_string_alloc( char *s, struct mulle_allocator *allocator)
  6. {
  7.    size_t                    len;
  8.    struct my_other_string    *p;
  9.    len = s ? strlen( s) : 0;
  10.    p   = mulle_allocator_malloc( allocator, sizeof( struct my_other_string) + len);
  11.    if( p)
  12.    {
  13.       memcpy( p->s, s, len);
  14.       p->s[ len] = 0;
  15.    }
  16.    return( p);
  17. }
  18. static inline void   my_other_string_free( struct my_other_string *p,
  19.                                            struct mulle_allocator *allocator)
  20. {
  21.    mulle_allocator_free( allocator, p);
  22. }
复制代码
Install

  On OS X and Linux you can use    homebrew, respectively    linuxbrewto install the library:  
  1. brew tap mulle-kybernetik/software
  2. brew install mulle-allocator
复制代码
On other platforms you can use    mulle-installfrom    mulle-buildto install the library:  
  1. mulle-install --prefix /usr/local --branch release https://www.mulle-kybernetik.com/repositories/mulle-allocator
复制代码
Otherwise read:
  
       
  •       How to Build  
  API

  
       
  •       Allocator   
  •       Test Allocator  
  Platforms and Compilers

  All platforms and compilers supported by    mulle-c11and    mulle-thread.  
  Author

      Nat!for    Mulle kybernetiKand    Codeon GmbH
友荐云推荐




上一篇:Gradient Descent Learns Linear Dynamical Systems
下一篇:10 best new web design tools for October 2016
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

lopa2380 发表于 2016-10-17 22:15:47
这次必须是沙发!
回复 支持 反对

使用道具 举报

lee007lee 发表于 2016-10-18 05:32:20
收藏了,怕楼主删了!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表