Blame lib/xalloc.h

Packit Service a2489d
/* xalloc.h -- malloc with out-of-memory checking
Packit Service a2489d
Packit Service a2489d
   Copyright (C) 1990-2000, 2003-2004, 2006-2018 Free Software Foundation, Inc.
Packit Service a2489d
Packit Service a2489d
   This program is free software: you can redistribute it and/or modify
Packit Service a2489d
   it under the terms of the GNU General Public License as published by
Packit Service a2489d
   the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
   (at your option) any later version.
Packit Service a2489d
Packit Service a2489d
   This program is distributed in the hope that it will be useful,
Packit Service a2489d
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
   GNU General Public License for more details.
Packit Service a2489d
Packit Service a2489d
   You should have received a copy of the GNU General Public License
Packit Service a2489d
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2489d
Packit Service a2489d
#ifndef XALLOC_H_
Packit Service a2489d
#define XALLOC_H_
Packit Service a2489d
Packit Service a2489d
#include <stddef.h>
Packit Service a2489d
#include <stdint.h>
Packit Service a2489d
Packit Service a2489d
#include "xalloc-oversized.h"
Packit Service a2489d
Packit Service a2489d
#ifndef _GL_INLINE_HEADER_BEGIN
Packit Service a2489d
 #error "Please include config.h first."
Packit Service a2489d
#endif
Packit Service a2489d
_GL_INLINE_HEADER_BEGIN
Packit Service a2489d
#ifndef XALLOC_INLINE
Packit Service a2489d
# define XALLOC_INLINE _GL_INLINE
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
#ifdef __cplusplus
Packit Service a2489d
extern "C" {
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
Packit Service a2489d
#if ! defined __clang__ && \
Packit Service a2489d
    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
Packit Service a2489d
# define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
Packit Service a2489d
#else
Packit Service a2489d
# define _GL_ATTRIBUTE_ALLOC_SIZE(args)
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
/* This function is always triggered when memory is exhausted.
Packit Service a2489d
   It must be defined by the application, either explicitly
Packit Service a2489d
   or by using gnulib's xalloc-die module.  This is the
Packit Service a2489d
   function to call when one wants the program to die because of a
Packit Service a2489d
   memory allocation failure.  */
Packit Service a2489d
extern _Noreturn void xalloc_die (void);
Packit Service a2489d
Packit Service a2489d
void *xmalloc (size_t s)
Packit Service a2489d
      _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
Packit Service a2489d
void *xzalloc (size_t s)
Packit Service a2489d
      _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
Packit Service a2489d
void *xcalloc (size_t n, size_t s)
Packit Service a2489d
      _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
Packit Service a2489d
void *xrealloc (void *p, size_t s)
Packit Service a2489d
      _GL_ATTRIBUTE_ALLOC_SIZE ((2));
Packit Service a2489d
void *x2realloc (void *p, size_t *pn);
Packit Service a2489d
void *xmemdup (void const *p, size_t s)
Packit Service a2489d
      _GL_ATTRIBUTE_ALLOC_SIZE ((2));
Packit Service a2489d
char *xstrdup (char const *str)
Packit Service a2489d
      _GL_ATTRIBUTE_MALLOC;
Packit Service a2489d
Packit Service a2489d
/* In the following macros, T must be an elementary or structure/union or
Packit Service a2489d
   typedef'ed type, or a pointer to such a type.  To apply one of the
Packit Service a2489d
   following macros to a function pointer or array type, you need to typedef
Packit Service a2489d
   it first and use the typedef name.  */
Packit Service a2489d
Packit Service a2489d
/* Allocate an object of type T dynamically, with error checking.  */
Packit Service a2489d
/* extern t *XMALLOC (typename t); */
Packit Service a2489d
#define XMALLOC(t) ((t *) xmalloc (sizeof (t)))
Packit Service a2489d
Packit Service a2489d
/* Allocate memory for N elements of type T, with error checking.  */
Packit Service a2489d
/* extern t *XNMALLOC (size_t n, typename t); */
Packit Service a2489d
#define XNMALLOC(n, t) \
Packit Service a2489d
   ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t))))
Packit Service a2489d
Packit Service a2489d
/* Allocate an object of type T dynamically, with error checking,
Packit Service a2489d
   and zero it.  */
Packit Service a2489d
/* extern t *XZALLOC (typename t); */
Packit Service a2489d
#define XZALLOC(t) ((t *) xzalloc (sizeof (t)))
Packit Service a2489d
Packit Service a2489d
/* Allocate memory for N elements of type T, with error checking,
Packit Service a2489d
   and zero it.  */
Packit Service a2489d
/* extern t *XCALLOC (size_t n, typename t); */
Packit Service a2489d
#define XCALLOC(n, t) \
Packit Service a2489d
   ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t))))
Packit Service a2489d
Packit Service a2489d
Packit Service a2489d
/* Allocate an array of N objects, each with S bytes of memory,
Packit Service a2489d
   dynamically, with error checking.  S must be nonzero.  */
Packit Service a2489d
Packit Service a2489d
XALLOC_INLINE void *xnmalloc (size_t n, size_t s)
Packit Service a2489d
                    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2));
Packit Service a2489d
XALLOC_INLINE void *
Packit Service a2489d
xnmalloc (size_t n, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  if (xalloc_oversized (n, s))
Packit Service a2489d
    xalloc_die ();
Packit Service a2489d
  return xmalloc (n * s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* Change the size of an allocated block of memory P to an array of N
Packit Service a2489d
   objects each of S bytes, with error checking.  S must be nonzero.  */
Packit Service a2489d
Packit Service a2489d
XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s)
Packit Service a2489d
                    _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3));
Packit Service a2489d
XALLOC_INLINE void *
Packit Service a2489d
xnrealloc (void *p, size_t n, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  if (xalloc_oversized (n, s))
Packit Service a2489d
    xalloc_die ();
Packit Service a2489d
  return xrealloc (p, n * s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* If P is null, allocate a block of at least *PN such objects;
Packit Service a2489d
   otherwise, reallocate P so that it contains more than *PN objects
Packit Service a2489d
   each of S bytes.  S must be nonzero.  Set *PN to the new number of
Packit Service a2489d
   objects, and return the pointer to the new block.  *PN is never set
Packit Service a2489d
   to zero, and the returned pointer is never null.
Packit Service a2489d
Packit Service a2489d
   Repeated reallocations are guaranteed to make progress, either by
Packit Service a2489d
   allocating an initial block with a nonzero size, or by allocating a
Packit Service a2489d
   larger block.
Packit Service a2489d
Packit Service a2489d
   In the following implementation, nonzero sizes are increased by a
Packit Service a2489d
   factor of approximately 1.5 so that repeated reallocations have
Packit Service a2489d
   O(N) overall cost rather than O(N**2) cost, but the
Packit Service a2489d
   specification for this function does not guarantee that rate.
Packit Service a2489d
Packit Service a2489d
   Here is an example of use:
Packit Service a2489d
Packit Service a2489d
     int *p = NULL;
Packit Service a2489d
     size_t used = 0;
Packit Service a2489d
     size_t allocated = 0;
Packit Service a2489d
Packit Service a2489d
     void
Packit Service a2489d
     append_int (int value)
Packit Service a2489d
       {
Packit Service a2489d
         if (used == allocated)
Packit Service a2489d
           p = x2nrealloc (p, &allocated, sizeof *p);
Packit Service a2489d
         p[used++] = value;
Packit Service a2489d
       }
Packit Service a2489d
Packit Service a2489d
   This causes x2nrealloc to allocate a block of some nonzero size the
Packit Service a2489d
   first time it is called.
Packit Service a2489d
Packit Service a2489d
   To have finer-grained control over the initial size, set *PN to a
Packit Service a2489d
   nonzero value before calling this function with P == NULL.  For
Packit Service a2489d
   example:
Packit Service a2489d
Packit Service a2489d
     int *p = NULL;
Packit Service a2489d
     size_t used = 0;
Packit Service a2489d
     size_t allocated = 0;
Packit Service a2489d
     size_t allocated1 = 1000;
Packit Service a2489d
Packit Service a2489d
     void
Packit Service a2489d
     append_int (int value)
Packit Service a2489d
       {
Packit Service a2489d
         if (used == allocated)
Packit Service a2489d
           {
Packit Service a2489d
             p = x2nrealloc (p, &allocated1, sizeof *p);
Packit Service a2489d
             allocated = allocated1;
Packit Service a2489d
           }
Packit Service a2489d
         p[used++] = value;
Packit Service a2489d
       }
Packit Service a2489d
Packit Service a2489d
   */
Packit Service a2489d
Packit Service a2489d
XALLOC_INLINE void *
Packit Service a2489d
x2nrealloc (void *p, size_t *pn, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  size_t n = *pn;
Packit Service a2489d
Packit Service a2489d
  if (! p)
Packit Service a2489d
    {
Packit Service a2489d
      if (! n)
Packit Service a2489d
        {
Packit Service a2489d
          /* The approximate size to use for initial small allocation
Packit Service a2489d
             requests, when the invoking code specifies an old size of
Packit Service a2489d
             zero.  This is the largest "small" request for the GNU C
Packit Service a2489d
             library malloc.  */
Packit Service a2489d
          enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
Packit Service a2489d
Packit Service a2489d
          n = DEFAULT_MXFAST / s;
Packit Service a2489d
          n += !n;
Packit Service a2489d
        }
Packit Service a2489d
      if (xalloc_oversized (n, s))
Packit Service a2489d
        xalloc_die ();
Packit Service a2489d
    }
Packit Service a2489d
  else
Packit Service a2489d
    {
Packit Service a2489d
      /* Set N = floor (1.5 * N) + 1 so that progress is made even if N == 0.
Packit Service a2489d
         Check for overflow, so that N * S stays in both ptrdiff_t and
Packit Service a2489d
         size_t range.  The check may be slightly conservative, but an
Packit Service a2489d
         exact check isn't worth the trouble.  */
Packit Service a2489d
      if ((PTRDIFF_MAX < SIZE_MAX ? PTRDIFF_MAX : SIZE_MAX) / 3 * 2 / s
Packit Service a2489d
          <= n)
Packit Service a2489d
        xalloc_die ();
Packit Service a2489d
      n += n / 2 + 1;
Packit Service a2489d
    }
Packit Service a2489d
Packit Service a2489d
  *pn = n;
Packit Service a2489d
  return xrealloc (p, n * s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
Packit Service a2489d
   except it returns char *.  */
Packit Service a2489d
Packit Service a2489d
XALLOC_INLINE char *xcharalloc (size_t n)
Packit Service a2489d
                    _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
Packit Service a2489d
XALLOC_INLINE char *
Packit Service a2489d
xcharalloc (size_t n)
Packit Service a2489d
{
Packit Service a2489d
  return XNMALLOC (n, char);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
#ifdef __cplusplus
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* C++ does not allow conversions from void * to other pointer types
Packit Service a2489d
   without a cast.  Use templates to work around the problem when
Packit Service a2489d
   possible.  */
Packit Service a2489d
Packit Service a2489d
template <typename T> inline T *
Packit Service a2489d
xrealloc (T *p, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  return (T *) xrealloc ((void *) p, s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
template <typename T> inline T *
Packit Service a2489d
xnrealloc (T *p, size_t n, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  return (T *) xnrealloc ((void *) p, n, s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
template <typename T> inline T *
Packit Service a2489d
x2realloc (T *p, size_t *pn)
Packit Service a2489d
{
Packit Service a2489d
  return (T *) x2realloc ((void *) p, pn);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
template <typename T> inline T *
Packit Service a2489d
x2nrealloc (T *p, size_t *pn, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  return (T *) x2nrealloc ((void *) p, pn, s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
template <typename T> inline T *
Packit Service a2489d
xmemdup (T const *p, size_t s)
Packit Service a2489d
{
Packit Service a2489d
  return (T *) xmemdup ((void const *) p, s);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
#endif
Packit Service a2489d
Packit Service a2489d
_GL_INLINE_HEADER_END
Packit Service a2489d
Packit Service a2489d
#endif /* !XALLOC_H_ */