LCOV - code coverage report
Current view: top level - libpkg - xmalloc.h (source / functions) Hit Total Coverage
Test: rapport Lines: 26 32 81.2 %
Date: 2021-12-10 16:22:55 Functions: 6 6 100.0 %
Branches: 6 12 50.0 %

           Branch data     Line data    Source code
       1                 :            : #ifndef XMALLOC_H
       2                 :            : #define XMALLOC_H
       3                 :            : 
       4                 :            : #include <stdarg.h>
       5                 :            : #include <stdio.h>
       6                 :            : #include <string.h>
       7                 :            : 
       8                 :      38776 : static inline void *xmalloc(size_t size)
       9                 :            : {
      10                 :      38776 :         void *ptr = malloc(size);
      11         [ -  + ]:      38776 :         if (ptr == NULL)
      12                 :          0 :                 abort();
      13                 :      38776 :         return (ptr);
      14                 :            : }
      15                 :            : 
      16                 :    5428096 : static inline void *xcalloc(size_t n, size_t size)
      17                 :            : {
      18                 :    5428096 :         void *ptr = calloc(n, size);
      19         [ -  + ]:    5428096 :         if (ptr == NULL)
      20                 :          0 :                 abort();
      21                 :    5428096 :         return (ptr);
      22                 :            : }
      23                 :            : 
      24                 :        293 : static inline void *xrealloc(void *ptr, size_t size)
      25                 :            : {
      26                 :        293 :         ptr = realloc(ptr, size);
      27         [ -  + ]:        293 :         if (ptr == NULL)
      28                 :          0 :                 abort();
      29                 :        293 :         return (ptr);
      30                 :            : }
      31                 :            : 
      32                 :    5416338 : static inline char *xstrdup(const char *str)
      33                 :            : {
      34                 :    5416338 :         char *s = strdup(str);
      35         [ -  + ]:    5416338 :         if (s == NULL)
      36                 :          0 :                 abort();
      37                 :    5416338 :         return (s);
      38                 :            : }
      39                 :            : 
      40                 :         52 : static inline char *xstrndup(const char *str, size_t n)
      41                 :            : {
      42                 :         52 :         char *s = strndup(str, n);
      43         [ -  + ]:         52 :         if (s == NULL)
      44                 :          0 :                 abort();
      45                 :         52 :         return (s);
      46                 :            : }
      47                 :            : 
      48                 :      31528 : static inline int xasprintf(char **ret, const char *fmt, ...)
      49                 :            : {
      50                 :            :         va_list ap;
      51                 :            :         int i;
      52                 :            : 
      53                 :      31528 :         va_start(ap, fmt);
      54                 :      31528 :         i = vasprintf(ret, fmt, ap);
      55                 :      31528 :         va_end(ap);
      56                 :            : 
      57         [ +  - ]:      31528 :         if (i < 0 || *ret == NULL)
      58                 :          0 :                 abort();
      59                 :            : 
      60                 :      31528 :         return (i);
      61                 :            : }
      62                 :            : #endif

Generated by: LCOV version 1.15