source: trunk/gsdl/packages/mg-1.3d/lib/xmalloc.c@ 26

Last change on this file since 26 was 26, checked in by rjmcnab, 25 years ago

Fixed a few problems with the windows build

  • 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 (n)
66 size_t n;
67{
68 VOID *p;
69
70 p = 0;
71 if (n == 0)
72 p = malloc ((size_t) 1);
73 if (p == 0)
74 error (xmalloc_exit_failure, 0, gettext ("Memory exhausted"));
75 return p;
76}
77
78/* Allocate N bytes of memory dynamically, with error checking. */
79
80VOID *
81xmalloc (n)
82 size_t n;
83{
84 VOID *p;
85
86 p = malloc (n);
87 if (p == 0)
88 p = fixup_null_alloc (n);
89 return p;
90}
91
92/* Change the size of an allocated block of memory P to N bytes,
93 with error checking.
94 If P is NULL, run xmalloc. */
95
96VOID *
97xrealloc (p, n)
98 VOID *p;
99 size_t n;
100{
101 if (p == 0)
102 return xmalloc (n);
103 p = realloc (p, n);
104 if (p == 0)
105 p = fixup_null_alloc (n);
106 return p;
107}
Note: See TracBrowser for help on using the repository browser.