4
0

xalloc.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #ifndef XALLOC_H_
  16. # define XALLOC_H_
  17. # include <stddef.h>
  18. # ifndef __attribute__
  19. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
  20. # define __attribute__(x)
  21. # endif
  22. # endif
  23. # ifndef ATTRIBUTE_NORETURN
  24. # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  25. # endif
  26. /* If this pointer is non-zero, run the specified function upon each
  27. allocation failure. It is initialized to zero. */
  28. extern void (*xalloc_fail_func) (void);
  29. /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
  30. message is output. It is translated via gettext.
  31. Its value is "memory exhausted". */
  32. extern char const xalloc_msg_memory_exhausted[];
  33. /* This function is always triggered when memory is exhausted. It is
  34. in charge of honoring the two previous items. It exits with status
  35. exit_failure (defined in exitfail.h). This is the
  36. function to call when one wants the program to die because of a
  37. memory allocation failure. */
  38. extern void xalloc_die (void) ATTRIBUTE_NORETURN;
  39. void *xmalloc (size_t s);
  40. void *xnmalloc (size_t n, size_t s);
  41. void *xzalloc (size_t s);
  42. void *xcalloc (size_t n, size_t s);
  43. void *xrealloc (void *p, size_t s);
  44. void *xnrealloc (void *p, size_t n, size_t s);
  45. void *x2realloc (void *p, size_t *pn);
  46. void *x2nrealloc (void *p, size_t *pn, size_t s);
  47. void *xclone (void const *p, size_t s);
  48. char *xstrdup (const char *str);
  49. /* Return 1 if an array of N objects, each of size S, cannot exist due
  50. to size arithmetic overflow. S must be positive and N must be
  51. nonnegative. This is a macro, not an inline function, so that it
  52. works correctly even when SIZE_MAX < N.
  53. By gnulib convention, SIZE_MAX represents overflow in size
  54. calculations, so the conservative dividend to use here is
  55. SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
  56. However, malloc (SIZE_MAX) fails on all known hosts where
  57. sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
  58. exactly-SIZE_MAX allocations on such hosts; this avoids a test and
  59. branch when S is known to be 1. */
  60. # define xalloc_oversized(n, s) \
  61. ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
  62. /* These macros are deprecated; they will go away soon, and are retained
  63. temporarily only to ease conversion to the functions described above. */
  64. # define CCLONE(p, n) xclone (p, (n) * sizeof *(p))
  65. # define CLONE(p) xclone (p, sizeof *(p))
  66. # define NEW(type, var) type *var = xmalloc (sizeof (type))
  67. # define XCALLOC(type, n) xcalloc (n, sizeof (type))
  68. # define XMALLOC(type, n) xnmalloc (n, sizeof (type))
  69. # define XREALLOC(p, type, n) xnrealloc (p, n, sizeof (type))
  70. # define XFREE(p) free (p)
  71. #endif /* !XALLOC_H_ */