Blame gettext-tools/libgettextpo/xmalloca.h

Packit Bot 06c835
/* Safe automatic memory allocation with out of memory checking.
Packit Bot 06c835
   Copyright (C) 2003, 2005, 2007, 2009-2015 Free Software Foundation, Inc.
Packit Bot 06c835
   Written by Bruno Haible <bruno@clisp.org>, 2003.
Packit Bot 06c835
Packit Bot 06c835
   This program is free software: you can redistribute it and/or modify
Packit Bot 06c835
   it under the terms of the GNU General Public License as published by
Packit Bot 06c835
   the Free Software Foundation; either version 3 of the License, or
Packit Bot 06c835
   (at your option) any later version.
Packit Bot 06c835
Packit Bot 06c835
   This program is distributed in the hope that it will be useful,
Packit Bot 06c835
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 06c835
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Bot 06c835
   GNU General Public License for more details.
Packit Bot 06c835
Packit Bot 06c835
   You should have received a copy of the GNU General Public License
Packit Bot 06c835
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Bot 06c835
Packit Bot 06c835
#ifndef _XMALLOCA_H
Packit Bot 06c835
#define _XMALLOCA_H
Packit Bot 06c835
Packit Bot 06c835
#include "malloca.h"
Packit Bot 06c835
#include "xalloc.h"
Packit Bot 06c835
Packit Bot 06c835
Packit Bot 06c835
#ifdef __cplusplus
Packit Bot 06c835
extern "C" {
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
Packit Bot 06c835
/* xmalloca(N) is a checking safe variant of alloca(N).  It allocates N bytes
Packit Bot 06c835
   of memory allocated on the stack, that must be freed using freea() before
Packit Bot 06c835
   the function returns.  Upon failure, it exits with an error message.  */
Packit Bot 06c835
#if HAVE_ALLOCA
Packit Bot 06c835
# define xmalloca(N) \
Packit Bot 06c835
  ((N) < 4032 - sa_increment                                        \
Packit Bot 06c835
   ? (void *) ((char *) alloca ((N) + sa_increment) + sa_increment) \
Packit Bot 06c835
   : xmmalloca (N))
Packit Bot 06c835
extern void * xmmalloca (size_t n);
Packit Bot 06c835
#else
Packit Bot 06c835
# define xmalloca(N) \
Packit Bot 06c835
  xmalloc (N)
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
/* xnmalloca(N,S) is an overflow-safe variant of xmalloca (N * S).
Packit Bot 06c835
   It allocates an array of N objects, each with S bytes of memory,
Packit Bot 06c835
   on the stack.  S must be positive and N must be nonnegative.
Packit Bot 06c835
   The array must be freed using freea() before the function returns.
Packit Bot 06c835
   Upon failure, it exits with an error message.  */
Packit Bot 06c835
#if HAVE_ALLOCA
Packit Bot 06c835
/* Rely on xmalloca (SIZE_MAX) calling xalloc_die ().  */
Packit Bot 06c835
# define xnmalloca(n, s) \
Packit Bot 06c835
    xmalloca (xalloc_oversized ((n), (s)) ? (size_t) (-1) : (n) * (s))
Packit Bot 06c835
#else
Packit Bot 06c835
# define xnmalloca(n, s) \
Packit Bot 06c835
    xnmalloc ((n), (s))
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
Packit Bot 06c835
#ifdef __cplusplus
Packit Bot 06c835
}
Packit Bot 06c835
#endif
Packit Bot 06c835
Packit Bot 06c835
Packit Bot 06c835
#endif /* _XMALLOCA_H */