source: trunk/gsdl/src/mgpp/lib/xmalloc.c@ 855

Last change on this file since 855 was 855, checked in by sjboddie, 24 years ago

Rodgers new C++ mg

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.3 KB
Line 
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
37VOID *malloc ();
38VOID *realloc ();
39void 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. */
56int xmalloc_exit_failure = EXIT_FAILURE;
57
58#if __STDC__ && (HAVE_VPRINTF || HAVE_DOPRNT)
59void error (int, int, const char *, ...);
60#else
61void error ();
62#endif
63
64static VOID *
65fixup_null_alloc (size_t n)
66{
67 VOID *p;
68
69 p = 0;
70 if (n == 0)
71 p = malloc ((size_t) 1);
72 if (p == 0)
73 error (xmalloc_exit_failure, 0, gettext ("Memory exhausted"));
74 return p;
75}
76
77/* Allocate N bytes of memory dynamically, with error checking. */
78
79VOID *
80xmalloc (size_t n)
81{
82 VOID *p;
83
84 p = malloc (n);
85 if (p == 0)
86 p = fixup_null_alloc (n);
87 return p;
88}
89
90/* Change the size of an allocated block of memory P to N bytes,
91 with error checking.
92 If P is NULL, run xmalloc. */
93
94VOID *
95xrealloc (VOID *p, size_t n)
96{
97 if (p == 0)
98 return xmalloc (n);
99 p = realloc (p, n);
100 if (p == 0)
101 p = fixup_null_alloc (n);
102 return p;
103}
Note: See TracBrowser for help on using the repository browser.