alloc.c 764 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #if (!defined(ALLOC_PRF) || !defined(ALLOC_TYPE))
  2. #error
  3. #endif
  4. #include "util.h"
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. ALLOC_TYPE* ALLOC_PRF(_alloc)(size_t count)
  8. {
  9. return malloc(count * sizeof(ALLOC_TYPE));
  10. }
  11. void ALLOC_PRF(_free)(ALLOC_TYPE* arr)
  12. {
  13. free(arr);
  14. }
  15. ALLOC_TYPE* ALLOC_PRF(_realloc)(ALLOC_TYPE* arr, size_t count)
  16. {
  17. size_t newsz = count * sizeof(ALLOC_TYPE);
  18. ALLOC_TYPE* newarr = realloc(arr, newsz);
  19. if (count != 0 && newarr == NULL)
  20. {
  21. static char msg[128];
  22. sprintf
  23. ( msg
  24. , "realloc(%p, %u*%u=%u) failed: to provide an alternative behaviour."
  25. , arr, (unsigned int) count, (unsigned int) sizeof(ALLOC_TYPE)
  26. , (unsigned int) newsz
  27. );
  28. ABORT(msg);
  29. };
  30. return newarr;
  31. }
  32. #undef ALLOC_PRF
  33. #undef ALLOC_TYPE