xref: /netbsd-src/external/gpl2/diffutils/dist/lib/xmalloc.c (revision f8c23a2b94243924f9b7311eb0ad24bf23d5c657)
1*f8c23a2bSchristos /*	$NetBSD: xmalloc.c,v 1.2 2016/01/13 03:39:28 christos Exp $	*/
275f6d617Schristos 
375f6d617Schristos /* xmalloc.c -- malloc with out of memory checking
475f6d617Schristos    Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc.
575f6d617Schristos 
675f6d617Schristos    This program is free software; you can redistribute it and/or modify
775f6d617Schristos    it under the terms of the GNU General Public License as published by
875f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
975f6d617Schristos    any later version.
1075f6d617Schristos 
1175f6d617Schristos    This program is distributed in the hope that it will be useful,
1275f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1375f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1475f6d617Schristos    GNU General Public License for more details.
1575f6d617Schristos 
1675f6d617Schristos    You should have received a copy of the GNU General Public License
1775f6d617Schristos    along with this program; if not, write to the Free Software Foundation,
1875f6d617Schristos    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
1975f6d617Schristos 
2075f6d617Schristos #if HAVE_CONFIG_H
2175f6d617Schristos # include <config.h>
2275f6d617Schristos #endif
2375f6d617Schristos 
2475f6d617Schristos #include <sys/types.h>
2575f6d617Schristos 
2675f6d617Schristos #if STDC_HEADERS
2775f6d617Schristos # include <stdlib.h>
2875f6d617Schristos #else
2975f6d617Schristos void *calloc ();
3075f6d617Schristos void *malloc ();
3175f6d617Schristos void *realloc ();
3275f6d617Schristos void free ();
3375f6d617Schristos #endif
3475f6d617Schristos 
3575f6d617Schristos #if ENABLE_NLS
3675f6d617Schristos # include <libintl.h>
3775f6d617Schristos # define _(Text) gettext (Text)
3875f6d617Schristos #else
3975f6d617Schristos # define textdomain(Domain)
4075f6d617Schristos # define _(Text) Text
4175f6d617Schristos #endif
4275f6d617Schristos #define N_(Text) Text
4375f6d617Schristos 
4475f6d617Schristos #include "error.h"
4575f6d617Schristos #include "exitfail.h"
4675f6d617Schristos #include "xalloc.h"
4775f6d617Schristos 
4875f6d617Schristos #ifndef EXIT_FAILURE
4975f6d617Schristos # define EXIT_FAILURE 1
5075f6d617Schristos #endif
5175f6d617Schristos 
5275f6d617Schristos #ifndef HAVE_DONE_WORKING_MALLOC_CHECK
5375f6d617Schristos "you must run the autoconf test for a properly working malloc -- see malloc.m4"
5475f6d617Schristos #endif
5575f6d617Schristos 
5675f6d617Schristos #ifndef HAVE_DONE_WORKING_REALLOC_CHECK
5775f6d617Schristos "you must run the autoconf test for a properly working realloc --see realloc.m4"
5875f6d617Schristos #endif
5975f6d617Schristos 
6075f6d617Schristos /* If non NULL, call this function when memory is exhausted. */
6175f6d617Schristos void (*xalloc_fail_func) PARAMS ((void)) = 0;
6275f6d617Schristos 
6375f6d617Schristos /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
6475f6d617Schristos    before exiting when memory is exhausted.  Goes through gettext. */
6575f6d617Schristos char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
6675f6d617Schristos 
6775f6d617Schristos void
xalloc_die(void)6875f6d617Schristos xalloc_die (void)
6975f6d617Schristos {
7075f6d617Schristos   if (xalloc_fail_func)
7175f6d617Schristos     (*xalloc_fail_func) ();
7275f6d617Schristos   error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
7375f6d617Schristos   /* The `noreturn' cannot be given to error, since it may return if
7475f6d617Schristos      its first argument is 0.  To help compilers understand the
7575f6d617Schristos      xalloc_die does terminate, call exit. */
7675f6d617Schristos   exit (EXIT_FAILURE);
7775f6d617Schristos }
7875f6d617Schristos 
7975f6d617Schristos /* Allocate N bytes of memory dynamically, with error checking.  */
8075f6d617Schristos 
8175f6d617Schristos void *
xmalloc(size_t n)8275f6d617Schristos xmalloc (size_t n)
8375f6d617Schristos {
8475f6d617Schristos   void *p;
8575f6d617Schristos 
8675f6d617Schristos   p = malloc (n);
87*f8c23a2bSchristos   if (p == 0 && n)
8875f6d617Schristos     xalloc_die ();
8975f6d617Schristos   return p;
9075f6d617Schristos }
9175f6d617Schristos 
9275f6d617Schristos /* Change the size of an allocated block of memory P to N bytes,
9375f6d617Schristos    with error checking.  */
9475f6d617Schristos 
9575f6d617Schristos void *
xrealloc(void * p,size_t n)9675f6d617Schristos xrealloc (void *p, size_t n)
9775f6d617Schristos {
9875f6d617Schristos   p = realloc (p, n);
99*f8c23a2bSchristos   if (p == 0 && n)
10075f6d617Schristos     xalloc_die ();
10175f6d617Schristos   return p;
10275f6d617Schristos }
10375f6d617Schristos 
10475f6d617Schristos /* Allocate memory for N elements of S bytes, with error checking.  */
10575f6d617Schristos 
10675f6d617Schristos void *
xcalloc(size_t n,size_t s)10775f6d617Schristos xcalloc (size_t n, size_t s)
10875f6d617Schristos {
10975f6d617Schristos   void *p;
11075f6d617Schristos 
11175f6d617Schristos   p = calloc (n, s);
112*f8c23a2bSchristos   if (p == 0 && n && s)
11375f6d617Schristos     xalloc_die ();
11475f6d617Schristos   return p;
11575f6d617Schristos }
116