home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / id-utils-3.2-src.tgz / tar.out / fsf / id-utils / lib / xmalloc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  99 lines

  1. /* xmalloc.c -- malloc with out of memory checking
  2.    Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include "error.h"
  22.  
  23. #if __STDC__
  24. #define VOID void
  25. #else
  26. #define VOID char
  27. #endif
  28.  
  29. #include <sys/types.h>
  30.  
  31. #if STDC_HEADERS
  32. #include <stdlib.h>
  33. #else
  34. VOID *malloc ();
  35. VOID *realloc ();
  36. void free ();
  37. #endif
  38.  
  39. /* This is for other GNU distributions with internationalized messages.
  40.    The GNU C Library itself does not yet support such messages.  */
  41. #if HAVE_LIBINTL_H
  42. # include <libintl.h>
  43. #else
  44. # define gettext(msgid) (msgid)
  45. #endif
  46.  
  47. #ifndef EXIT_FAILURE
  48. #define EXIT_FAILURE 1
  49. #endif
  50.  
  51. /* Exit value when the requested amount of memory is not available.
  52.    The caller may set it to some other value.  */
  53. int xmalloc_exit_failure = EXIT_FAILURE;
  54.  
  55. static VOID *
  56. fixup_null_alloc (n)
  57.      size_t n;
  58. {
  59.   VOID *p;
  60.  
  61.   p = 0;
  62.   if (n == 0)
  63.     p = malloc ((size_t) 1);
  64.   if (p == 0)
  65.     error (xmalloc_exit_failure, 0, gettext ("Memory exhausted"));
  66.   return p;
  67. }
  68.  
  69. /* Allocate N bytes of memory dynamically, with error checking.  */
  70.  
  71. VOID *
  72. xmalloc (n)
  73.      size_t n;
  74. {
  75.   VOID *p;
  76.  
  77.   p = malloc (n);
  78.   if (p == 0)
  79.     p = fixup_null_alloc (n);
  80.   return p;
  81. }
  82.  
  83. /* Change the size of an allocated block of memory P to N bytes,
  84.    with error checking.
  85.    If P is NULL, run xmalloc.  */
  86.  
  87. VOID *
  88. xrealloc (p, n)
  89.      VOID *p;
  90.      size_t n;
  91. {
  92.   if (p == 0)
  93.     return xmalloc (n);
  94.   p = realloc (p, n);
  95.   if (p == 0)
  96.     p = fixup_null_alloc (n);
  97.   return p;
  98. }
  99.