Back to index

glibc  2.9
xmalloc.c
Go to the documentation of this file.
00001 /* xmalloc.c -- malloc with out of memory checking
00002    Copyright (C) 1990,91,92,93,94,95,96,97,2004,2005
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published
00008    by the Free Software Foundation; version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 #if __STDC__
00025 #define VOID void
00026 #else
00027 #define VOID char
00028 #endif
00029 
00030 #include <sys/types.h>
00031 
00032 #if STDC_HEADERS || _LIBC
00033 #include <stdlib.h>
00034 static VOID *fixup_null_alloc (size_t n) __THROW;
00035 VOID *xmalloc (size_t n) __THROW;
00036 VOID *xcalloc (size_t n, size_t s) __THROW;
00037 VOID *xrealloc (VOID *p, size_t n) __THROW;
00038 #else
00039 VOID *calloc ();
00040 VOID *malloc ();
00041 VOID *realloc ();
00042 void free ();
00043 #endif
00044 
00045 #include <libintl.h>
00046 #include "error.h"
00047 
00048 #ifndef _
00049 # define _(str) gettext (str)
00050 #endif
00051 
00052 #ifndef EXIT_FAILURE
00053 #define EXIT_FAILURE 4
00054 #endif
00055 
00056 /* Exit value when the requested amount of memory is not available.
00057    The caller may set it to some other value.  */
00058 int xmalloc_exit_failure = EXIT_FAILURE;
00059 
00060 static VOID *
00061 fixup_null_alloc (n)
00062      size_t n;
00063 {
00064   VOID *p;
00065 
00066   p = 0;
00067   if (n == 0)
00068     p = malloc ((size_t) 1);
00069   if (p == 0)
00070     error (xmalloc_exit_failure, 0, _("memory exhausted"));
00071   return p;
00072 }
00073 
00074 /* Allocate N bytes of memory dynamically, with error checking.  */
00075 
00076 VOID *
00077 xmalloc (n)
00078      size_t n;
00079 {
00080   VOID *p;
00081 
00082   p = malloc (n);
00083   if (p == 0)
00084     p = fixup_null_alloc (n);
00085   return p;
00086 }
00087 
00088 /* Allocate memory for N elements of S bytes, with error checking.  */
00089 
00090 VOID *
00091 xcalloc (n, s)
00092      size_t n, s;
00093 {
00094   VOID *p;
00095 
00096   p = calloc (n, s);
00097   if (p == 0)
00098     p = fixup_null_alloc (n);
00099   return p;
00100 }
00101 
00102 /* Change the size of an allocated block of memory P to N bytes,
00103    with error checking.
00104    If P is NULL, run xmalloc.  */
00105 
00106 VOID *
00107 xrealloc (p, n)
00108      VOID *p;
00109      size_t n;
00110 {
00111   if (p == 0)
00112     return xmalloc (n);
00113   p = realloc (p, n);
00114   if (p == 0)
00115     p = fixup_null_alloc (n);
00116   return p;
00117 }