(root)/
findutils-4.9.0/
gl/
lib/
xalloc.h
       1  /* xalloc.h -- malloc with out-of-memory checking
       2  
       3     Copyright (C) 1990-2000, 2003-2004, 2006-2022 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 *xinmalloc (idx_t n, idx_t s)
      64    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      65    _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      66  void *xzalloc (size_t s)
      67    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      68    _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      69  void *xizalloc (idx_t s)
      70    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      71    _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      72  void *xcalloc (size_t n, size_t s)
      73    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      74    _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      75  void *xicalloc (idx_t n, idx_t s)
      76    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      77    _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      78  void *xrealloc (void *p, size_t s)
      79    _GL_ATTRIBUTE_ALLOC_SIZE ((2));
      80  void *xirealloc (void *p, idx_t s)
      81    _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      82  void *xreallocarray (void *p, size_t n, size_t s)
      83    _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
      84  void *xireallocarray (void *p, idx_t n, idx_t s)
      85    _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      86  void *x2realloc (void *p, size_t *ps) /* superseded by xpalloc */
      87    _GL_ATTRIBUTE_RETURNS_NONNULL;
      88  void *x2nrealloc (void *p, size_t *pn, size_t s) /* superseded by xpalloc */
      89    _GL_ATTRIBUTE_RETURNS_NONNULL;
      90  void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
      91    _GL_ATTRIBUTE_RETURNS_NONNULL;
      92  void *xmemdup (void const *p, size_t s)
      93    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      94    _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      95  void *ximemdup (void const *p, idx_t s)
      96    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
      97    _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
      98  char *ximemdup0 (void const *p, idx_t s)
      99    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
     100    _GL_ATTRIBUTE_RETURNS_NONNULL;
     101  char *xstrdup (char const *str)
     102    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
     103    _GL_ATTRIBUTE_RETURNS_NONNULL;
     104  
     105  /* In the following macros, T must be an elementary or structure/union or
     106     typedef'ed type, or a pointer to such a type.  To apply one of the
     107     following macros to a function pointer or array type, you need to typedef
     108     it first and use the typedef name.  */
     109  
     110  /* Allocate an object of type T dynamically, with error checking.  */
     111  /* extern t *XMALLOC (typename t); */
     112  # define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
     113  
     114  /* Allocate memory for N elements of type T, with error checking.  */
     115  /* extern t *XNMALLOC (size_t n, typename t); */
     116  # define XNMALLOC(n, t) \
     117      ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
     118  
     119  /* Allocate an object of type T dynamically, with error checking,
     120     and zero it.  */
     121  /* extern t *XZALLOC (typename t); */
     122  # define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
     123  
     124  /* Allocate memory for N elements of type T, with error checking,
     125     and zero it.  */
     126  /* extern t *XCALLOC (size_t n, typename t); */
     127  # define XCALLOC(n, t) \
     128      ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
     129  
     130  
     131  /* Allocate an array of N objects, each with S bytes of memory,
     132     dynamically, with error checking.  S must be nonzero.  */
     133  
     134  void *xnmalloc (size_t n, size_t s)
     135    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
     136    _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL;
     137  
     138  /* FIXME: Deprecate this in favor of xreallocarray?  */
     139  /* Change the size of an allocated block of memory P to an array of N
     140     objects each of S bytes, with error checking.  S must be nonzero.  */
     141  
     142  XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
     143    _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
     144  XALLOC_INLINE void *
     145  xnrealloc (void *p, size_t n, size_t s)
     146  {
     147    return xreallocarray (p, n, s);
     148  }
     149  
     150  /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
     151     except it returns char *.  */
     152  
     153  char *xcharalloc (size_t n)
     154    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
     155    _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL;
     156  
     157  #endif /* GNULIB_XALLOC */
     158  
     159  
     160  #ifdef __cplusplus
     161  }
     162  #endif
     163  
     164  
     165  #if GNULIB_XALLOC && defined __cplusplus
     166  
     167  /* C++ does not allow conversions from void * to other pointer types
     168     without a cast.  Use templates to work around the problem when
     169     possible.  */
     170  
     171  template <typename T> inline T *
     172  xrealloc (T *p, size_t s)
     173  {
     174    return (T *) xrealloc ((void *) p, s);
     175  }
     176  
     177  template <typename T> inline T *
     178  xreallocarray (T *p, size_t n, size_t s)
     179  {
     180    return (T *) xreallocarray ((void *) p, n, s);
     181  }
     182  
     183  /* FIXME: Deprecate this in favor of xreallocarray?  */
     184  template <typename T> inline T *
     185  xnrealloc (T *p, size_t n, size_t s)
     186  {
     187    return xreallocarray (p, n, s);
     188  }
     189  
     190  template <typename T> inline T *
     191  x2realloc (T *p, size_t *pn)
     192  {
     193    return (T *) x2realloc ((void *) p, pn);
     194  }
     195  
     196  template <typename T> inline T *
     197  x2nrealloc (T *p, size_t *pn, size_t s)
     198  {
     199    return (T *) x2nrealloc ((void *) p, pn, s);
     200  }
     201  
     202  template <typename T> inline T *
     203  xmemdup (T const *p, size_t s)
     204  {
     205    return (T *) xmemdup ((void const *) p, s);
     206  }
     207  
     208  #endif /* GNULIB_XALLOC && C++ */
     209  
     210  
     211  _GL_INLINE_HEADER_END
     212  
     213  #endif /* !XALLOC_H_ */