root/maint/gnulib/lib/xalloc.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. xnmalloc
  2. xrealloc
  3. xreallocarray
  4. xnrealloc
  5. x2realloc
  6. x2nrealloc
  7. xmemdup

   1 /* xalloc.h -- malloc with out-of-memory checking
   2 
   3    Copyright (C) 1990-2000, 2003-2004, 2006-2021 Free Software Foundation, Inc.
   4 
   5    This program is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9 
  10    This program is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU General Public License for more details.
  14 
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 #ifndef XALLOC_H_
  19 #define XALLOC_H_
  20 
  21 #include <stddef.h>
  22 #include <stdlib.h>
  23 #include <stdint.h>
  24 
  25 #if GNULIB_XALLOC
  26 # include "idx.h"
  27 # include "intprops.h"
  28 #endif
  29 
  30 #ifndef _GL_INLINE_HEADER_BEGIN
  31  #error "Please include config.h first."
  32 #endif
  33 _GL_INLINE_HEADER_BEGIN
  34 #ifndef XALLOC_INLINE
  35 # define XALLOC_INLINE _GL_INLINE
  36 #endif
  37 
  38 
  39 #ifdef __cplusplus
  40 extern "C" {
  41 #endif
  42 
  43 
  44 #if GNULIB_XALLOC_DIE
  45 
  46 /* This function is always triggered when memory is exhausted.
  47    It must be defined by the application, either explicitly
  48    or by using gnulib's xalloc-die module.  This is the
  49    function to call when one wants the program to die because of a
  50    memory allocation failure.  */
  51 /*extern*/ _Noreturn void xalloc_die (void);
  52 
  53 #endif /* GNULIB_XALLOC_DIE */
  54 
  55 #if GNULIB_XALLOC
  56 
  57 void *xmalloc (size_t s)
  58   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  59   _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  60 void *ximalloc (idx_t s)
  61   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  62   _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  63 void *xzalloc (size_t s)
  64   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  65   _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  66 void *xizalloc (idx_t s)
  67   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  68   _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  69 void *xcalloc (size_t n, size_t s)
  70   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  71   _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  72 void *xicalloc (idx_t n, idx_t s)
  73   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  74   _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  75 void *xrealloc (void *p, size_t s)
  76   _GL_ATTRIBUTE_ALLOC_SIZE ((2));
  77 void *xirealloc (void *p, idx_t s)
  78   _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  79 void *xreallocarray (void *p, size_t n, size_t s)
  80   _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
  81 void *xireallocarray (void *p, idx_t n, idx_t s)
  82   _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  83 void *x2realloc (void *p, size_t *ps) /* superseded by xpalloc */
  84   _GL_ATTRIBUTE_RETURNS_NONNULL;
  85 void *x2nrealloc (void *p, size_t *pn, size_t s) /* superseded by xpalloc */
  86   _GL_ATTRIBUTE_RETURNS_NONNULL;
  87 void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
  88   _GL_ATTRIBUTE_RETURNS_NONNULL;
  89 void *xmemdup (void const *p, size_t s)
  90   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  91   _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  92 void *ximemdup (void const *p, idx_t s)
  93   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  94   _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
  95 char *ximemdup0 (void const *p, idx_t s)
  96   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
  97   _GL_ATTRIBUTE_RETURNS_NONNULL;
  98 char *xstrdup (char const *str)
  99   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
 100   _GL_ATTRIBUTE_RETURNS_NONNULL;
 101 
 102 /* In the following macros, T must be an elementary or structure/union or
 103    typedef'ed type, or a pointer to such a type.  To apply one of the
 104    following macros to a function pointer or array type, you need to typedef
 105    it first and use the typedef name.  */
 106 
 107 /* Allocate an object of type T dynamically, with error checking.  */
 108 /* extern t *XMALLOC (typename t); */
 109 # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
 110 
 111 /* Allocate memory for N elements of type T, with error checking.  */
 112 /* extern t *XNMALLOC (size_t n, typename t); */
 113 # define XNMALLOC(n, t) \
 114     ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
 115 
 116 /* Allocate an object of type T dynamically, with error checking,
 117    and zero it.  */
 118 /* extern t *XZALLOC (typename t); */
 119 # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
 120 
 121 /* Allocate memory for N elements of type T, with error checking,
 122    and zero it.  */
 123 /* extern t *XCALLOC (size_t n, typename t); */
 124 # define XCALLOC(n, t) \
 125     ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
 126 
 127 
 128 /* Allocate an array of N objects, each with S bytes of memory,
 129    dynamically, with error checking.  S must be nonzero.  */
 130 
 131 void *xnmalloc (size_t n, size_t s)
     /* [previous][next][first][last][top][bottom][index][help] */
 132   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
 133   _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
 134 
 135 /* FIXME: Deprecate this in favor of xreallocarray?  */
 136 /* Change the size of an allocated block of memory P to an array of N
 137    objects each of S bytes, with error checking.  S must be nonzero.  */
 138 
 139 XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
 140   _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
 141 XALLOC_INLINE void *
 142 xnrealloc (void *p, size_t n, size_t s)
 143 {
 144   return xreallocarray (p, n, s);
 145 }
 146 
 147 /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
 148    except it returns char *.  */
 149 
 150 char *xcharalloc (size_t n)
 151   _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
 152   _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
 153 
 154 #endif /* GNULIB_XALLOC */
 155 
 156 
 157 #ifdef __cplusplus
 158 }
 159 #endif
 160 
 161 
 162 #if GNULIB_XALLOC && defined __cplusplus
 163 
 164 /* C++ does not allow conversions from void * to other pointer types
 165    without a cast.  Use templates to work around the problem when
 166    possible.  */
 167 
 168 template <typename T> inline T *
 169 xrealloc (T *p, size_t s)
     /* [previous][next][first][last][top][bottom][index][help] */
 170 {
 171   return (T *) xrealloc ((void *) p, s);
 172 }
 173 
 174 template <typename T> inline T *
 175 xreallocarray (T *p, size_t n, size_t s)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177   return (T *) xreallocarray ((void *) p, n, s);
 178 }
 179 
 180 /* FIXME: Deprecate this in favor of xreallocarray?  */
 181 template <typename T> inline T *
 182 xnrealloc (T *p, size_t n, size_t s)
     /* [previous][next][first][last][top][bottom][index][help] */
 183 {
 184   return xreallocarray (p, n, s);
 185 }
 186 
 187 template <typename T> inline T *
 188 x2realloc (T *p, size_t *pn)
     /* [previous][next][first][last][top][bottom][index][help] */
 189 {
 190   return (T *) x2realloc ((void *) p, pn);
 191 }
 192 
 193 template <typename T> inline T *
 194 x2nrealloc (T *p, size_t *pn, size_t s)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196   return (T *) x2nrealloc ((void *) p, pn, s);
 197 }
 198 
 199 template <typename T> inline T *
 200 xmemdup (T const *p, size_t s)
     /* [previous][next][first][last][top][bottom][index][help] */
 201 {
 202   return (T *) xmemdup ((void const *) p, s);
 203 }
 204 
 205 #endif /* GNULIB_XALLOC && C++ */
 206 
 207 
 208 _GL_INLINE_HEADER_END
 209 
 210 #endif /* !XALLOC_H_ */

/* [previous][next][first][last][top][bottom][index][help] */