/compat/xalloc.h

https://github.com/dkurochkin/squid · C Header · 73 lines · 19 code · 11 blank · 43 comment · 1 complexity · 72a41b6f8918699ccd086f3413ec5e9a MD5 · raw file

  1. #ifndef _SQUID_COMPAT_XALLOC_H
  2. #define _SQUID_COMPAT_XALLOC_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /**
  7. * xcalloc() - same as calloc(3). Used for portability.
  8. * Never returns NULL; fatal on error.
  9. *
  10. * Define failure_notify to receive error message.
  11. * otherwise perror() is used to display it.
  12. */
  13. void *xcalloc(size_t n, size_t sz);
  14. /**
  15. * xmalloc() - same as malloc(3). Used for portability.
  16. * Never returns NULL; fatal on error.
  17. *
  18. * Define failure_notify to receive error message.
  19. * otherwise perror() is used to display it.
  20. */
  21. void *xmalloc(size_t sz);
  22. /**
  23. * xrealloc() - same as realloc(3). Used for portability.
  24. * Never returns NULL; fatal on error.
  25. */
  26. void *xrealloc(void *s, size_t sz);
  27. /**
  28. * xfree() - same as free(3). Used for portability.
  29. * Will not call free(3) if s == NULL.
  30. *
  31. * Define failure_notify to receive error message.
  32. * otherwise perror() is used to display it.
  33. */
  34. void xfree(void *s);
  35. /**
  36. * xxfree() / free_const() - Same as free(3). Used for portability.
  37. * Accepts pointers to dynamically allocated const data.
  38. *
  39. * Define failure_notify to receive error message.
  40. * otherwise perror() is used to display it.
  41. */
  42. void free_const(const void *s);
  43. /// Backward compatibility alias for free_const(const void *s)
  44. #define xxfree(x) free_const((x))
  45. /**
  46. * Accepts pointers to dynamically allocated const data.
  47. * Will not call free(3) if the pointer is NULL.
  48. * Sets the pointer to NULL on completion.
  49. *
  50. * Use xfree() if the pointer does not need to be set afterward.
  51. *
  52. * Define failure_notify to receive error message.
  53. * otherwise perror() is used to display it.
  54. */
  55. #define safe_free(x) while (x) { xxfree(x); (x) = NULL; }
  56. #ifdef __cplusplus
  57. }
  58. #endif
  59. #if XMALLOC_STATISTICS
  60. extern void malloc_statistics(void (*func) (int, int, int, void *), void *data);
  61. #endif
  62. #endif /* _SQUID_COMPAT_XALLOC_H */