1131554Stjr/* xmalloc.c -- malloc with out of memory checking
2131554Stjr   Copyright (C) 1990-1999, 2000 Free Software Foundation, Inc.
3131554Stjr
4131554Stjr   This program is free software; you can redistribute it and/or modify
5131554Stjr   it under the terms of the GNU General Public License as published by
6131554Stjr   the Free Software Foundation; either version 2, or (at your option)
7131554Stjr   any later version.
8131554Stjr
9131554Stjr   This program is distributed in the hope that it will be useful,
10131554Stjr   but WITHOUT ANY WARRANTY; without even the implied warranty of
11131554Stjr   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12131554Stjr   GNU General Public License for more details.
13131554Stjr
14131554Stjr   You should have received a copy of the GNU General Public License
15131554Stjr   along with this program; if not, write to the Free Software Foundation,
16131554Stjr   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17131554Stjr
18131554Stjr#if HAVE_CONFIG_H
19131554Stjr# include <config.h>
20131554Stjr#endif
21131554Stjr
22131554Stjr#include <sys/types.h>
23131554Stjr
24131554Stjr#if STDC_HEADERS
25131554Stjr# include <stdlib.h>
26131554Stjr#else
27131554Stjrvoid *calloc ();
28131554Stjrvoid *malloc ();
29131554Stjrvoid *realloc ();
30131554Stjrvoid free ();
31131554Stjr#endif
32131554Stjr
33131554Stjr#if ENABLE_NLS
34131554Stjr# include <libintl.h>
35131554Stjr# define _(Text) gettext (Text)
36131554Stjr#else
37131554Stjr# define textdomain(Domain)
38131554Stjr# define _(Text) Text
39131554Stjr#endif
40131554Stjr#define N_(Text) Text
41131554Stjr
42131554Stjr#include "error.h"
43131554Stjr#include "xalloc.h"
44131554Stjr
45131554Stjr#ifndef EXIT_FAILURE
46131554Stjr# define EXIT_FAILURE 1
47131554Stjr#endif
48131554Stjr
49131554Stjr#ifndef HAVE_DONE_WORKING_MALLOC_CHECK
50131554Stjr"you must run the autoconf test for a properly working malloc -- see malloc.m4"
51131554Stjr#endif
52131554Stjr
53131554Stjr#ifndef HAVE_DONE_WORKING_REALLOC_CHECK
54131554Stjr"you must run the autoconf test for a properly working realloc --see realloc.m4"
55131554Stjr#endif
56131554Stjr
57131554Stjr/* Exit value when the requested amount of memory is not available.
58131554Stjr   The caller may set it to some other value.  */
59131554Stjrint xalloc_exit_failure = EXIT_FAILURE;
60131554Stjr
61131554Stjr/* If non NULL, call this function when memory is exhausted. */
62131554Stjrvoid (*xalloc_fail_func) PARAMS ((void)) = 0;
63131554Stjr
64131554Stjr/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
65131554Stjr   before exiting when memory is exhausted.  Goes through gettext. */
66131554Stjrchar const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
67131554Stjr
68131554Stjrvoid
69131554Stjrxalloc_die (void)
70131554Stjr{
71131554Stjr  if (xalloc_fail_func)
72131554Stjr    (*xalloc_fail_func) ();
73131554Stjr  error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
74131554Stjr  /* The `noreturn' cannot be given to error, since it may return if
75131554Stjr     its first argument is 0.  To help compilers understand the
76131554Stjr     xalloc_die does terminate, call exit. */
77131554Stjr  exit (EXIT_FAILURE);
78131554Stjr}
79131554Stjr
80131554Stjr/* Allocate N bytes of memory dynamically, with error checking.  */
81131554Stjr
82131554Stjrvoid *
83131554Stjrxmalloc (size_t n)
84131554Stjr{
85131554Stjr  void *p;
86131554Stjr
87131554Stjr  p = malloc (n);
88131554Stjr  if (p == 0)
89131554Stjr    xalloc_die ();
90131554Stjr  return p;
91131554Stjr}
92131554Stjr
93131554Stjr/* Change the size of an allocated block of memory P to N bytes,
94131554Stjr   with error checking.  */
95131554Stjr
96131554Stjrvoid *
97131554Stjrxrealloc (void *p, size_t n)
98131554Stjr{
99131554Stjr  p = realloc (p, n);
100131554Stjr  if (p == 0)
101131554Stjr    xalloc_die ();
102131554Stjr  return p;
103131554Stjr}
104131554Stjr
105131554Stjr/* Allocate memory for N elements of S bytes, with error checking.  */
106131554Stjr
107131554Stjrvoid *
108131554Stjrxcalloc (size_t n, size_t s)
109131554Stjr{
110131554Stjr  void *p;
111131554Stjr
112131554Stjr  p = calloc (n, s);
113131554Stjr  if (p == 0)
114131554Stjr    xalloc_die ();
115131554Stjr  return p;
116131554Stjr}
117