LCOV - code coverage report
Current view: top level - include - xmalloc.h (source / functions) Hit Total Coverage
Test: coverage3.info Lines: 1 1 100.0 %
Date: 2014-04-22 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : #ifndef __CR_XMALLOC_H__
       2                 :            : #define __CR_XMALLOC_H__
       3                 :            : 
       4                 :            : #include <stdlib.h>
       5                 :            : #include <string.h>
       6                 :            : 
       7                 :            : #include "log.h"
       8                 :            : 
       9                 :            : #define __xalloc(op, size, ...)                                         \
      10                 :            :         ({                                                              \
      11                 :            :                 void *___p = op( __VA_ARGS__ );                         \
      12                 :            :                 if (!___p)                                              \
      13                 :            :                         pr_err("%s: Can't allocate %li bytes\n",      \
      14                 :            :                                __func__, (long)(size));                 \
      15                 :            :                 ___p;                                                   \
      16                 :            :         })
      17                 :            : 
      18                 :            : #define xstrdup(str)            __xalloc(strdup, strlen(str) + 1, str)
      19                 :            : #define xmalloc(size)           __xalloc(malloc, size, size)
      20                 :            : #define xzalloc(size)           __xalloc(calloc, size, 1, size)
      21                 :            : #define xrealloc(p, size)       __xalloc(realloc, size, p, size)
      22                 :            : 
      23                 :            : #define xfree(p)                do { if (p) free(p); } while (0)
      24                 :            : 
      25                 :            : #define xrealloc_safe(pptr, size)                                       \
      26                 :            :         ({                                                              \
      27                 :            :                 int __ret = -1;                                         \
      28                 :            :                 void *new = xrealloc(*pptr, size);                      \
      29                 :            :                 if (new) {                                              \
      30                 :            :                         *pptr = new;                                    \
      31                 :            :                         __ret = 0;                                      \
      32                 :            :                 }                                                       \
      33                 :            :                 __ret;                                                  \
      34                 :            :          })
      35                 :            : 
      36                 :            : #define memzero_p(p)            memset(p, 0, sizeof(*p))
      37                 :            : #define memzero(p, size)        memset(p, 0, size)
      38                 :            : 
      39                 :            : /*
      40                 :            :  * Helper for allocating trees with single xmalloc.
      41                 :            :  * This one advances the void *pointer on s bytes and
      42                 :            :  * returns the previous value. Use like this
      43                 :            :  *
      44                 :            :  * m = xmalloc(total_size);
      45                 :            :  * a = xptr_pull(&m, tree_root_t);
      46                 :            :  * a->b = xptr_pull(&m, leaf_a_t);
      47                 :            :  * a->c = xptr_pull(&m, leaf_c_t);
      48                 :            :  * ...
      49                 :            :  */
      50                 :            : static inline void *xptr_pull_s(void **m, size_t s)
      51                 :            : {
      52                 :            :         void *ret = (*m);
      53                 :      75204 :         (*m) += s;
      54                 :            :         return ret;
      55                 :            : }
      56                 :            : 
      57                 :            : #define xptr_pull(m, type)      xptr_pull_s(m, sizeof(type))
      58                 :            : 
      59                 :            : #endif /* __CR_XMALLOC_H__ */

Generated by: LCOV version 1.9