2
0

xmalloc.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #define MODULE "xalloc"
  2. #include "compiler.h"
  3. #include "xmalloc.h"
  4. #include "common.h"
  5. #include <assert.h>
  6. no_return __noinline alloc_panic(void)
  7. {
  8. assert(!"FATAL: OUT OF MEMORY!!!");
  9. while (1)
  10. suspend();
  11. }
  12. void *xrealloc(void *ptr, size_t len)
  13. {
  14. return null_check(realloc(ptr, Max(len,1)));
  15. }
  16. void *xmalloc(size_t len)
  17. {
  18. return null_check(malloc(Max(len,1)));
  19. }
  20. void *xmalloc_dma(size_t len)
  21. {
  22. return null_check(heap_caps_malloc(Max(len,1), MALLOC_CAP_DMA));
  23. }
  24. void *xzalloc(size_t len)
  25. {
  26. return null_check(calloc(Max(len,1), 1));
  27. }
  28. static inline size_t nsize(size_t nmemb, size_t size)
  29. {
  30. size2_t len = nmemb * size;
  31. if (unlikely(len >> CHAR_BIT*sizeof(size_t)))
  32. alloc_panic();
  33. return (size_t)len;
  34. }
  35. void *xnmalloc(size_t nmemb, size_t size)
  36. {
  37. return xmalloc(nsize(nmemb, size));
  38. }
  39. void *xcalloc(size_t nmemb, size_t size)
  40. {
  41. return xzalloc(nsize(nmemb, size));
  42. }
  43. void *xnrealloc(void *ptr, size_t nmemb, size_t size)
  44. {
  45. return xrealloc(ptr, nsize(nmemb, size));
  46. }