[3365] | 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 | # ifdef __WIN32__ /* [RPAP - Feb 97: WIN32 Port] */
|
---|
| 20 | # include <win32cfg.h>
|
---|
| 21 | # else
|
---|
| 22 | # include <sysfuncs.h>
|
---|
| 23 | # endif
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | #if __STDC__
|
---|
| 27 | #define VOID void
|
---|
| 28 | #else
|
---|
| 29 | #define VOID char
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | #include <sys/types.h>
|
---|
| 33 |
|
---|
| 34 | #if STDC_HEADERS
|
---|
| 35 | #include <stdlib.h>
|
---|
| 36 | #else
|
---|
| 37 | VOID *malloc ();
|
---|
| 38 | VOID *realloc ();
|
---|
| 39 | void free ();
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | /* This is for other GNU distributions with internationalized messages.
|
---|
| 43 | The GNU C Library itself does not yet support such messages. */
|
---|
| 44 | #if HAVE_LIBINTL_H
|
---|
| 45 | # include <libintl.h>
|
---|
| 46 | #else
|
---|
| 47 | # define gettext(msgid) (msgid)
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
| 50 | #ifndef EXIT_FAILURE
|
---|
| 51 | #define EXIT_FAILURE 1
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 | /* Exit value when the requested amount of memory is not available.
|
---|
| 55 | The caller may set it to some other value. */
|
---|
| 56 | int xmalloc_exit_failure = EXIT_FAILURE;
|
---|
| 57 |
|
---|
| 58 | #if defined(__WIN32__)
|
---|
| 59 | void error (int, int, const char *, ...);
|
---|
| 60 | #else
|
---|
| 61 | #if __STDC__ && (HAVE_VPRINTF || HAVE_DOPRNT)
|
---|
| 62 | void error (int, int, const char *, ...);
|
---|
| 63 | #else
|
---|
| 64 | void error (int, int, char *, char *, char *, char *, char *, char *, char *, char *, char *);
|
---|
| 65 | #endif
|
---|
| 66 | #endif
|
---|
| 67 |
|
---|
| 68 | static VOID *
|
---|
| 69 | fixup_null_alloc (n)
|
---|
| 70 | size_t n;
|
---|
| 71 | {
|
---|
| 72 | VOID *p;
|
---|
| 73 |
|
---|
| 74 | p = 0;
|
---|
| 75 | if (n == 0)
|
---|
| 76 | p = malloc ((size_t) 1);
|
---|
| 77 | if (p == 0)
|
---|
| 78 | error (xmalloc_exit_failure, 0, gettext ("Memory exhausted"));
|
---|
| 79 | return p;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /* Allocate N bytes of memory dynamically, with error checking. */
|
---|
| 83 |
|
---|
| 84 | VOID *
|
---|
| 85 | xmalloc (n)
|
---|
| 86 | size_t n;
|
---|
| 87 | {
|
---|
| 88 | VOID *p;
|
---|
| 89 |
|
---|
| 90 | p = malloc (n);
|
---|
| 91 | if (p == 0)
|
---|
| 92 | p = fixup_null_alloc (n);
|
---|
| 93 | return p;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /* Change the size of an allocated block of memory P to N bytes,
|
---|
| 97 | with error checking.
|
---|
| 98 | If P is NULL, run xmalloc. */
|
---|
| 99 |
|
---|
| 100 | VOID *
|
---|
| 101 | xrealloc (p, n)
|
---|
| 102 | VOID *p;
|
---|
| 103 | size_t n;
|
---|
| 104 | {
|
---|
| 105 | if (p == 0)
|
---|
| 106 | return xmalloc (n);
|
---|
| 107 | p = realloc (p, n);
|
---|
| 108 | if (p == 0)
|
---|
| 109 | p = fixup_null_alloc (n);
|
---|
| 110 | return p;
|
---|
| 111 | }
|
---|