(fmemopen): Add libc_hidden_def.
[glibc/history.git] / locale / programs / xmalloc.c
bloba2eeda79b88afee480519d1d2202c6d641def789
1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990,91,92,93,94,95,96,97,2004,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #if __STDC__
25 #define VOID void
26 #else
27 #define VOID char
28 #endif
30 #include <sys/types.h>
32 #if STDC_HEADERS || _LIBC
33 #include <stdlib.h>
34 static VOID *fixup_null_alloc (size_t n) __THROW;
35 VOID *xmalloc (size_t n) __THROW;
36 VOID *xcalloc (size_t n, size_t s) __THROW;
37 VOID *xrealloc (VOID *p, size_t n) __THROW;
38 #else
39 VOID *calloc ();
40 VOID *malloc ();
41 VOID *realloc ();
42 void free ();
43 #endif
45 #include <libintl.h>
46 #include "error.h"
48 #ifndef _
49 # define _(str) gettext (str)
50 #endif
52 #ifndef EXIT_FAILURE
53 #define EXIT_FAILURE 4
54 #endif
56 /* Exit value when the requested amount of memory is not available.
57 The caller may set it to some other value. */
58 int xmalloc_exit_failure = EXIT_FAILURE;
60 static VOID *
61 fixup_null_alloc (n)
62 size_t n;
64 VOID *p;
66 p = 0;
67 if (n == 0)
68 p = malloc ((size_t) 1);
69 if (p == 0)
70 error (xmalloc_exit_failure, 0, _("memory exhausted"));
71 return p;
74 /* Allocate N bytes of memory dynamically, with error checking. */
76 VOID *
77 xmalloc (n)
78 size_t n;
80 VOID *p;
82 p = malloc (n);
83 if (p == 0)
84 p = fixup_null_alloc (n);
85 return p;
88 /* Allocate memory for N elements of S bytes, with error checking. */
90 VOID *
91 xcalloc (n, s)
92 size_t n, s;
94 VOID *p;
96 p = calloc (n, s);
97 if (p == 0)
98 p = fixup_null_alloc (n);
99 return p;
102 /* Change the size of an allocated block of memory P to N bytes,
103 with error checking.
104 If P is NULL, run xmalloc. */
106 VOID *
107 xrealloc (p, n)
108 VOID *p;
109 size_t n;
111 if (p == 0)
112 return xmalloc (n);
113 p = realloc (p, n);
114 if (p == 0)
115 p = fixup_null_alloc (n);
116 return p;