xalloc.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* xalloc.h -- malloc with out-of-memory checking
  2. Copyright (C) 1990-1998, 1999, 2000 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #ifndef XALLOC_H_
  15. # define XALLOC_H_
  16. # ifndef PARAMS
  17. # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
  18. # define PARAMS(Args) Args
  19. # else
  20. # define PARAMS(Args) ()
  21. # endif
  22. # endif
  23. # ifndef __attribute__
  24. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
  25. # define __attribute__(x)
  26. # endif
  27. # endif
  28. # ifndef ATTRIBUTE_NORETURN
  29. # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  30. # endif
  31. /* Exit value when the requested amount of memory is not available.
  32. It is initialized to EXIT_FAILURE, but the caller may set it to
  33. some other value. */
  34. extern int xalloc_exit_failure;
  35. /* If this pointer is non-zero, run the specified function upon each
  36. allocation failure. It is initialized to zero. */
  37. extern void (*xalloc_fail_func) PARAMS ((void));
  38. /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
  39. message is output. It is translated via gettext.
  40. Its value is "memory exhausted". */
  41. extern char const xalloc_msg_memory_exhausted[];
  42. /* This function is always triggered when memory is exhausted. It is
  43. in charge of honoring the three previous items. This is the
  44. function to call when one wants the program to die because of a
  45. memory allocation failure. */
  46. extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN;
  47. void *xmalloc PARAMS ((size_t n));
  48. void *xcalloc PARAMS ((size_t n, size_t s));
  49. void *xrealloc PARAMS ((void *p, size_t n));
  50. char *xstrdup PARAMS ((const char *str));
  51. # define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items)))
  52. # define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items)))
  53. # define XREALLOC(Ptr, Type, N_items) \
  54. ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items)))
  55. /* Declare and alloc memory for VAR of type TYPE. */
  56. # define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1)
  57. /* Free VAR only if non NULL. */
  58. # define XFREE(Var) \
  59. do { \
  60. if (Var) \
  61. free (Var); \
  62. } while (0)
  63. /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */
  64. # define CCLONE(Src, Num) \
  65. (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num)))
  66. /* Return a malloc'ed copy of SRC. */
  67. # define CLONE(Src) CCLONE (Src, 1)
  68. #endif /* !XALLOC_H_ */