Blame lib/xalloc.h

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