xalloc.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* xalloc.h -- malloc with out-of-memory checking
  2. Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  3. 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #ifndef XALLOC_H_
  16. # define XALLOC_H_
  17. # include <stddef.h>
  18. # ifdef __cplusplus
  19. extern "C" {
  20. # endif
  21. # ifndef __attribute__
  22. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
  23. # define __attribute__(x)
  24. # endif
  25. # endif
  26. # ifndef ATTRIBUTE_NORETURN
  27. # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  28. # endif
  29. /* This function is always triggered when memory is exhausted.
  30. It must be defined by the application, either explicitly
  31. or by using gnulib's xalloc-die module. This is the
  32. function to call when one wants the program to die because of a
  33. memory allocation failure. */
  34. extern void xalloc_die (void) ATTRIBUTE_NORETURN;
  35. void *xmalloc (size_t s);
  36. void *xnmalloc (size_t n, size_t s);
  37. void *xzalloc (size_t s);
  38. void *xcalloc (size_t n, size_t s);
  39. void *xrealloc (void *p, size_t s);
  40. void *xnrealloc (void *p, size_t n, size_t s);
  41. void *x2realloc (void *p, size_t *pn);
  42. void *x2nrealloc (void *p, size_t *pn, size_t s);
  43. void *xmemdup (void const *p, size_t s);
  44. char *xstrdup (char const *str);
  45. /* Return 1 if an array of N objects, each of size S, cannot exist due
  46. to size arithmetic overflow. S must be positive and N must be
  47. nonnegative. This is a macro, not an inline function, so that it
  48. works correctly even when SIZE_MAX < N.
  49. By gnulib convention, SIZE_MAX represents overflow in size
  50. calculations, so the conservative dividend to use here is
  51. SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
  52. However, malloc (SIZE_MAX) fails on all known hosts where
  53. sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
  54. exactly-SIZE_MAX allocations on such hosts; this avoids a test and
  55. branch when S is known to be 1. */
  56. # define xalloc_oversized(n, s) \
  57. ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
  58. # ifdef __cplusplus
  59. }
  60. # endif
  61. #endif /* !XALLOC_H_ */