xalloc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* malloc with out of memory checking.
  2. Copyright (C) 2001-2004, 2006 Free Software Foundation, Inc.
  3. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  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 3 of the License, or
  7. (at your option) 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, see <http://www.gnu.org/licenses/>. */
  14. #ifndef _XALLOC_H
  15. #define _XALLOC_H
  16. #include <stddef.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* Defined in xmalloc.c. */
  21. /* Allocate SIZE bytes of memory dynamically, with error checking. */
  22. extern void *xmalloc (size_t size);
  23. /* Allocate memory for NMEMB elements of SIZE bytes, with error checking.
  24. SIZE must be > 0. */
  25. extern void *xnmalloc (size_t nmemb, size_t size);
  26. /* Allocate SIZE bytes of memory dynamically, with error checking,
  27. and zero it. */
  28. extern void *xzalloc (size_t size);
  29. /* Allocate memory for NMEMB elements of SIZE bytes, with error checking,
  30. and zero it. */
  31. extern void *xcalloc (size_t nmemb, size_t size);
  32. /* Change the size of an allocated block of memory PTR to SIZE bytes,
  33. with error checking. If PTR is NULL, run xmalloc. */
  34. extern void *xrealloc (void *ptr, size_t size);
  35. #ifdef __cplusplus
  36. }
  37. template <typename T>
  38. inline T * xrealloc (T * ptr, size_t size)
  39. {
  40. return (T *) xrealloc ((void *) ptr, size);
  41. }
  42. extern "C" {
  43. #endif
  44. /* This function is always triggered when memory is exhausted. It is
  45. in charge of honoring the three previous items. This is the
  46. function to call when one wants the program to die because of a
  47. memory allocation failure. */
  48. extern void xalloc_die (void)
  49. #if (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)) && !__STRICT_ANSI__
  50. __attribute__ ((__noreturn__))
  51. #endif
  52. ;
  53. /* In the following macros, T must be an elementary or structure/union or
  54. typedef'ed type, or a pointer to such a type. To apply one of the
  55. following macros to a function pointer or array type, you need to typedef
  56. it first and use the typedef name. */
  57. /* Allocate an object of type T dynamically, with error checking. */
  58. /* extern T *XMALLOC (typename T); */
  59. #define XMALLOC(T) \
  60. ((T *) xmalloc (sizeof (T)))
  61. /* Allocate memory for NMEMB elements of type T, with error checking. */
  62. /* extern T *XNMALLOC (size_t nmemb, typename T); */
  63. #if HAVE_INLINE
  64. /* xnmalloc performs a division and multiplication by sizeof (T). Arrange to
  65. perform the division at compile-time and the multiplication with a factor
  66. known at compile-time. */
  67. # define XNMALLOC(N,T) \
  68. ((T *) (sizeof (T) == 1 \
  69. ? xmalloc (N) \
  70. : xnboundedmalloc(N, (size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / sizeof (T), sizeof (T))))
  71. static inline void *
  72. xnboundedmalloc (size_t n, size_t bound, size_t s)
  73. {
  74. if (n > bound)
  75. xalloc_die ();
  76. return xmalloc (n * s);
  77. }
  78. #else
  79. # define XNMALLOC(N,T) \
  80. ((T *) (sizeof (T) == 1 ? xmalloc (N) : xnmalloc (N, sizeof (T))))
  81. #endif
  82. /* Allocate an object of type T dynamically, with error checking,
  83. and zero it. */
  84. /* extern T *XZALLOC (typename T); */
  85. #define XZALLOC(T) \
  86. ((T *) xzalloc (sizeof (T)))
  87. /* Allocate memory for NMEMB elements of type T, with error checking,
  88. and zero it. */
  89. /* extern T *XCALLOC (size_t nmemb, typename T); */
  90. #define XCALLOC(N,T) \
  91. ((T *) xcalloc (N, sizeof (T)))
  92. /* Return a pointer to a new buffer of N bytes. This is like xmalloc,
  93. except it returns char *. */
  94. #define xcharalloc(N) \
  95. XNMALLOC (N, char)
  96. /* Defined in xstrdup.c. */
  97. /* Return a newly allocated copy of the N bytes of memory starting at P. */
  98. extern void *xmemdup (const void *p, size_t n);
  99. #ifdef __cplusplus
  100. }
  101. template <typename T>
  102. inline T * xmemdup (const T * p, size_t n)
  103. {
  104. return (T *) xmemdup ((const void *) p, n);
  105. }
  106. extern "C" {
  107. #endif
  108. /* Return a newly allocated copy of STRING. */
  109. extern char *xstrdup (const char *string);
  110. /* Return 1 if an array of N objects, each of size S, cannot exist due
  111. to size arithmetic overflow. S must be positive and N must be
  112. nonnegative. This is a macro, not an inline function, so that it
  113. works correctly even when SIZE_MAX < N.
  114. By gnulib convention, SIZE_MAX represents overflow in size
  115. calculations, so the conservative dividend to use here is
  116. SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
  117. However, malloc (SIZE_MAX) fails on all known hosts where
  118. sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
  119. exactly-SIZE_MAX allocations on such hosts; this avoids a test and
  120. branch when S is known to be 1. */
  121. # define xalloc_oversized(n, s) \
  122. ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
  123. #ifdef __cplusplus
  124. }
  125. #endif
  126. #endif /* _XALLOC_H */