2
0

xmalloc.h 872 B

12345678910111213141516171819202122232425262728
  1. #pragma once
  2. #include "compiler.h"
  3. #include <stdlib.h>
  4. extern_c no_return __noinline alloc_panic(void);
  5. #define null_check(x) ({ \
  6. __typeof__(x) __x = (x); \
  7. if (unlikely(!__x)) \
  8. alloc_panic(); \
  9. __x; \
  10. })
  11. extern_c void * __safe_alloc(1) __malloc_func xmalloc(size_t);
  12. extern_c void * __safe_alloc(1) __malloc_func xmalloc_dma(size_t);
  13. extern_c void * __safe_alloc(1) __malloc_func xzalloc(size_t);
  14. extern_c void * __safe_alloc(1,2) __malloc_func xncalloc(size_t, size_t);
  15. extern_c void * __safe_alloc(1,2) __malloc_func xcalloc(size_t, size_t);
  16. extern_c void * __safe_alloc(2) xrealloc(void *, size_t);
  17. extern_c void * __safe_alloc(2,3) xnrealloc(void *, size_t, size_t);
  18. #define xnew(x) ((x) = xzalloc(sizeof(*(x))))
  19. #define xnnew(x,n) ((x) = xcalloc((n),sizeof(*(x))))
  20. static inline void xfree(void *p)
  21. {
  22. if (p)
  23. free(p);
  24. }