xref: /netbsd-src/external/gpl2/gettext/dist/gettext-runtime/gnulib-lib/xmalloc.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* xmalloc.c -- malloc with out of memory checking
2*946379e7Schristos    Copyright (C) 1990-1996, 2000-2003, 2005-2006 Free Software Foundation, Inc.
3*946379e7Schristos 
4*946379e7Schristos    This program is free software; you can redistribute it and/or modify
5*946379e7Schristos    it under the terms of the GNU General Public License as published by
6*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
7*946379e7Schristos    any later version.
8*946379e7Schristos 
9*946379e7Schristos    This program is distributed in the hope that it will be useful,
10*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*946379e7Schristos    GNU General Public License for more details.
13*946379e7Schristos 
14*946379e7Schristos    You should have received a copy of the GNU General Public License
15*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
16*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*946379e7Schristos 
18*946379e7Schristos #include <config.h>
19*946379e7Schristos 
20*946379e7Schristos /* Specification.  */
21*946379e7Schristos #include "xalloc.h"
22*946379e7Schristos 
23*946379e7Schristos #include <stdlib.h>
24*946379e7Schristos 
25*946379e7Schristos #include "error.h"
26*946379e7Schristos #include "exit.h"
27*946379e7Schristos #include "gettext.h"
28*946379e7Schristos 
29*946379e7Schristos #define _(str) gettext (str)
30*946379e7Schristos 
31*946379e7Schristos 
32*946379e7Schristos /* Exit value when the requested amount of memory is not available.
33*946379e7Schristos    The caller may set it to some other value.  */
34*946379e7Schristos int xmalloc_exit_failure = EXIT_FAILURE;
35*946379e7Schristos 
36*946379e7Schristos void
xalloc_die()37*946379e7Schristos xalloc_die ()
38*946379e7Schristos {
39*946379e7Schristos   error (xmalloc_exit_failure, 0, _("memory exhausted"));
40*946379e7Schristos   /* The `noreturn' cannot be given to error, since it may return if
41*946379e7Schristos      its first argument is 0.  To help compilers understand the
42*946379e7Schristos      xalloc_die does terminate, call exit. */
43*946379e7Schristos   exit (EXIT_FAILURE);
44*946379e7Schristos }
45*946379e7Schristos 
46*946379e7Schristos static void *
fixup_null_alloc(size_t n)47*946379e7Schristos fixup_null_alloc (size_t n)
48*946379e7Schristos {
49*946379e7Schristos   void *p;
50*946379e7Schristos 
51*946379e7Schristos   p = 0;
52*946379e7Schristos   if (n == 0)
53*946379e7Schristos     p = malloc ((size_t) 1);
54*946379e7Schristos   if (p == NULL)
55*946379e7Schristos     xalloc_die ();
56*946379e7Schristos   return p;
57*946379e7Schristos }
58*946379e7Schristos 
59*946379e7Schristos /* Allocate N bytes of memory dynamically, with error checking.  */
60*946379e7Schristos 
61*946379e7Schristos void *
xmalloc(size_t n)62*946379e7Schristos xmalloc (size_t n)
63*946379e7Schristos {
64*946379e7Schristos   void *p;
65*946379e7Schristos 
66*946379e7Schristos   p = malloc (n);
67*946379e7Schristos   if (p == NULL)
68*946379e7Schristos     p = fixup_null_alloc (n);
69*946379e7Schristos   return p;
70*946379e7Schristos }
71*946379e7Schristos 
72*946379e7Schristos /* Allocate SIZE bytes of memory dynamically, with error checking,
73*946379e7Schristos    and zero it.  */
74*946379e7Schristos 
75*946379e7Schristos void *
xzalloc(size_t size)76*946379e7Schristos xzalloc (size_t size)
77*946379e7Schristos {
78*946379e7Schristos   void *p;
79*946379e7Schristos 
80*946379e7Schristos   p = xmalloc (size);
81*946379e7Schristos   memset (p, 0, size);
82*946379e7Schristos   return p;
83*946379e7Schristos }
84*946379e7Schristos 
85*946379e7Schristos /* Allocate memory for N elements of S bytes, with error checking,
86*946379e7Schristos    and zero it.  */
87*946379e7Schristos 
88*946379e7Schristos void *
xcalloc(size_t n,size_t s)89*946379e7Schristos xcalloc (size_t n, size_t s)
90*946379e7Schristos {
91*946379e7Schristos   void *p;
92*946379e7Schristos 
93*946379e7Schristos   p = calloc (n, s);
94*946379e7Schristos   if (p == NULL)
95*946379e7Schristos     p = fixup_null_alloc (n);
96*946379e7Schristos   return p;
97*946379e7Schristos }
98*946379e7Schristos 
99*946379e7Schristos /* Change the size of an allocated block of memory P to N bytes,
100*946379e7Schristos    with error checking.
101*946379e7Schristos    If P is NULL, run xmalloc.  */
102*946379e7Schristos 
103*946379e7Schristos void *
xrealloc(void * p,size_t n)104*946379e7Schristos xrealloc (void *p, size_t n)
105*946379e7Schristos {
106*946379e7Schristos   if (p == NULL)
107*946379e7Schristos     return xmalloc (n);
108*946379e7Schristos   p = realloc (p, n);
109*946379e7Schristos   if (p == NULL)
110*946379e7Schristos     p = fixup_null_alloc (n);
111*946379e7Schristos   return p;
112*946379e7Schristos }
113