xref: /netbsd-src/external/gpl2/grep/dist/lib/xmalloc.c (revision a8fa202a6440953be7b92a8960a811bff58203f4)
1*a8fa202aSchristos /*	$NetBSD: xmalloc.c,v 1.1.1.1 2016/01/10 21:36:19 christos Exp $	*/
2*a8fa202aSchristos 
3*a8fa202aSchristos /* xmalloc.c -- malloc with out of memory checking
4*a8fa202aSchristos    Copyright (C) 1990-1999, 2000 Free Software Foundation, Inc.
5*a8fa202aSchristos 
6*a8fa202aSchristos    This program is free software; you can redistribute it and/or modify
7*a8fa202aSchristos    it under the terms of the GNU General Public License as published by
8*a8fa202aSchristos    the Free Software Foundation; either version 2, or (at your option)
9*a8fa202aSchristos    any later version.
10*a8fa202aSchristos 
11*a8fa202aSchristos    This program is distributed in the hope that it will be useful,
12*a8fa202aSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a8fa202aSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*a8fa202aSchristos    GNU General Public License for more details.
15*a8fa202aSchristos 
16*a8fa202aSchristos    You should have received a copy of the GNU General Public License
17*a8fa202aSchristos    along with this program; if not, write to the Free Software Foundation,
18*a8fa202aSchristos    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19*a8fa202aSchristos 
20*a8fa202aSchristos #if HAVE_CONFIG_H
21*a8fa202aSchristos # include <config.h>
22*a8fa202aSchristos #endif
23*a8fa202aSchristos 
24*a8fa202aSchristos #include <sys/types.h>
25*a8fa202aSchristos 
26*a8fa202aSchristos #if STDC_HEADERS
27*a8fa202aSchristos # include <stdlib.h>
28*a8fa202aSchristos #else
29*a8fa202aSchristos void *calloc ();
30*a8fa202aSchristos void *malloc ();
31*a8fa202aSchristos void *realloc ();
32*a8fa202aSchristos void free ();
33*a8fa202aSchristos #endif
34*a8fa202aSchristos 
35*a8fa202aSchristos #if ENABLE_NLS
36*a8fa202aSchristos # include <libintl.h>
37*a8fa202aSchristos # define _(Text) gettext (Text)
38*a8fa202aSchristos #else
39*a8fa202aSchristos # define textdomain(Domain)
40*a8fa202aSchristos # define _(Text) Text
41*a8fa202aSchristos #endif
42*a8fa202aSchristos #define N_(Text) Text
43*a8fa202aSchristos 
44*a8fa202aSchristos #include "error.h"
45*a8fa202aSchristos #include "xalloc.h"
46*a8fa202aSchristos 
47*a8fa202aSchristos #ifndef EXIT_FAILURE
48*a8fa202aSchristos # define EXIT_FAILURE 1
49*a8fa202aSchristos #endif
50*a8fa202aSchristos 
51*a8fa202aSchristos #ifndef HAVE_DONE_WORKING_MALLOC_CHECK
52*a8fa202aSchristos "you must run the autoconf test for a properly working malloc -- see malloc.m4"
53*a8fa202aSchristos #endif
54*a8fa202aSchristos 
55*a8fa202aSchristos #ifndef HAVE_DONE_WORKING_REALLOC_CHECK
56*a8fa202aSchristos "you must run the autoconf test for a properly working realloc --see realloc.m4"
57*a8fa202aSchristos #endif
58*a8fa202aSchristos 
59*a8fa202aSchristos /* Exit value when the requested amount of memory is not available.
60*a8fa202aSchristos    The caller may set it to some other value.  */
61*a8fa202aSchristos int xalloc_exit_failure = EXIT_FAILURE;
62*a8fa202aSchristos 
63*a8fa202aSchristos /* If non NULL, call this function when memory is exhausted. */
64*a8fa202aSchristos void (*xalloc_fail_func) PARAMS ((void)) = 0;
65*a8fa202aSchristos 
66*a8fa202aSchristos /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
67*a8fa202aSchristos    before exiting when memory is exhausted.  Goes through gettext. */
68*a8fa202aSchristos char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
69*a8fa202aSchristos 
70*a8fa202aSchristos void
xalloc_die(void)71*a8fa202aSchristos xalloc_die (void)
72*a8fa202aSchristos {
73*a8fa202aSchristos   if (xalloc_fail_func)
74*a8fa202aSchristos     (*xalloc_fail_func) ();
75*a8fa202aSchristos   error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
76*a8fa202aSchristos   /* The `noreturn' cannot be given to error, since it may return if
77*a8fa202aSchristos      its first argument is 0.  To help compilers understand the
78*a8fa202aSchristos      xalloc_die does terminate, call exit. */
79*a8fa202aSchristos   exit (EXIT_FAILURE);
80*a8fa202aSchristos }
81*a8fa202aSchristos 
82*a8fa202aSchristos /* Allocate N bytes of memory dynamically, with error checking.  */
83*a8fa202aSchristos 
84*a8fa202aSchristos void *
xmalloc(size_t n)85*a8fa202aSchristos xmalloc (size_t n)
86*a8fa202aSchristos {
87*a8fa202aSchristos   void *p;
88*a8fa202aSchristos 
89*a8fa202aSchristos   p = malloc (n);
90*a8fa202aSchristos   if (p == 0)
91*a8fa202aSchristos     xalloc_die ();
92*a8fa202aSchristos   return p;
93*a8fa202aSchristos }
94*a8fa202aSchristos 
95*a8fa202aSchristos /* Change the size of an allocated block of memory P to N bytes,
96*a8fa202aSchristos    with error checking.  */
97*a8fa202aSchristos 
98*a8fa202aSchristos void *
xrealloc(void * p,size_t n)99*a8fa202aSchristos xrealloc (void *p, size_t n)
100*a8fa202aSchristos {
101*a8fa202aSchristos   p = realloc (p, n);
102*a8fa202aSchristos   if (p == 0)
103*a8fa202aSchristos     xalloc_die ();
104*a8fa202aSchristos   return p;
105*a8fa202aSchristos }
106*a8fa202aSchristos 
107*a8fa202aSchristos /* Allocate memory for N elements of S bytes, with error checking.  */
108*a8fa202aSchristos 
109*a8fa202aSchristos void *
xcalloc(size_t n,size_t s)110*a8fa202aSchristos xcalloc (size_t n, size_t s)
111*a8fa202aSchristos {
112*a8fa202aSchristos   void *p;
113*a8fa202aSchristos 
114*a8fa202aSchristos   p = calloc (n, s);
115*a8fa202aSchristos   if (p == 0)
116*a8fa202aSchristos     xalloc_die ();
117*a8fa202aSchristos   return p;
118*a8fa202aSchristos }
119