comparison lib/xmalloc.c @ 262:a70a20230879

GNU shell utilities
author Jim Meyering <jim@meyering.net>
date Thu, 13 Oct 1994 02:05:39 +0000
parents 74478d22a1fe
children 516dbf487373
comparison
equal deleted inserted replaced
256:8cf333566445 262:a70a20230879
1 /* xmalloc.c -- malloc with out of memory checking 1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc. 2 Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 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) 6 the Free Software Foundation; either version 2, or (at your option)
7 any later version. 7 any later version.
33 VOID *malloc (); 33 VOID *malloc ();
34 VOID *realloc (); 34 VOID *realloc ();
35 void free (); 35 void free ();
36 #endif 36 #endif
37 37
38 #if __STDC__ && defined (HAVE_VPRINTF) 38 #ifndef EXIT_FAILURE
39 void error (int, int, char const *, ...); 39 #define EXIT_FAILURE 1
40 #endif
41
42 /* Exit value when the requested amount of memory is not available.
43 The caller may set it to some other value. */
44 int xmalloc_exit_failure = EXIT_FAILURE;
45
46 #if __STDC__ && (HAVE_VPRINTF || HAVE_DOPRNT)
47 void error (int, int, const char *, ...);
40 #else 48 #else
41 void error (); 49 void error ();
42 #endif 50 #endif
43 51
44 /* Allocate N bytes of memory dynamically, with error checking. */ 52 /* Allocate N bytes of memory dynamically, with error checking. */
49 { 57 {
50 VOID *p; 58 VOID *p;
51 59
52 p = malloc (n); 60 p = malloc (n);
53 if (p == 0) 61 if (p == 0)
54 /* Must exit with 2 for `cmp'. */ 62 error (xmalloc_exit_failure, 0, "memory exhausted");
55 error (2, 0, "memory exhausted");
56 return p; 63 return p;
57 } 64 }
58 65
59 /* Change the size of an allocated block of memory P to N bytes, 66 /* Change the size of an allocated block of memory P to N bytes,
60 with error checking. 67 with error checking.
73 free (p); 80 free (p);
74 return 0; 81 return 0;
75 } 82 }
76 p = realloc (p, n); 83 p = realloc (p, n);
77 if (p == 0) 84 if (p == 0)
78 /* Must exit with 2 for `cmp'. */ 85 error (xmalloc_exit_failure, 0, "memory exhausted");
79 error (2, 0, "memory exhausted");
80 return p; 86 return p;
81 } 87 }