#ifndef XMALLOC_H #define XMALLOC_H #include "compiler.h" #include #include void malloc_fail(void) __NORETURN; #define xnew(type, n) (type *)xmalloc(sizeof(type) * (n)) #define xnew0(type, n) (type *)xmalloc0(sizeof(type) * (n)) #define xrenew(type, mem, n) (type *)xrealloc(mem, sizeof(type) * (n)) static inline void * __MALLOC xmalloc(size_t size) { void *ptr = malloc(size); if (unlikely(ptr == NULL)) malloc_fail(); return ptr; } static inline void * __MALLOC xmalloc0(size_t size) { void *ptr = calloc(1, size); if (unlikely(ptr == NULL)) malloc_fail(); return ptr; } static inline void * __MALLOC xrealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); if (unlikely(ptr == NULL)) malloc_fail(); return ptr; } static inline char * __MALLOC xstrdup(const char *str) { char *s = strdup(str); if (unlikely(s == NULL)) malloc_fail(); return s; } char * __MALLOC xstrndup(const char *str, size_t n); char **str_array_add(char **a, int *allocp, int *posp, char *str); static inline char * __MALLOC xxstrdup(const char *str) { if (str == NULL) return NULL; return xstrdup(str); } static inline void free_str_array(char **array) { int i; if (array == NULL) return; for (i = 0; array[i]; i++) free(array[i]); free(array); } #endif