* sysdeps/i386/tls.h (THREAD_GSCOPE_RESET_FLAG): Use explicit
[glibc.git] / locale / programs / xmalloc.c
bloba95dc5a40f300b6b6f0144d75779d1b737337196
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 version 2 as
8 published by the Free Software Foundation.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #if __STDC__
24 #define VOID void
25 #else
26 #define VOID char
27 #endif
29 #include <sys/types.h>
31 #if STDC_HEADERS || _LIBC
32 #include <stdlib.h>
33 static VOID *fixup_null_alloc (size_t n) __THROW;
34 VOID *xmalloc (size_t n) __THROW;
35 VOID *xcalloc (size_t n, size_t s) __THROW;
36 VOID *xrealloc (VOID *p, size_t n) __THROW;
37 #else
38 VOID *calloc ();
39 VOID *malloc ();
40 VOID *realloc ();
41 void free ();
42 #endif
44 #include <libintl.h>
45 #include "error.h"
47 #ifndef _
48 # define _(str) gettext (str)
49 #endif
51 #ifndef EXIT_FAILURE
52 #define EXIT_FAILURE 4
53 #endif
55 /* Exit value when the requested amount of memory is not available.
56 The caller may set it to some other value. */
57 int xmalloc_exit_failure = EXIT_FAILURE;
59 static VOID *
60 fixup_null_alloc (n)
61 size_t n;
63 VOID *p;
65 p = 0;
66 if (n == 0)
67 p = malloc ((size_t) 1);
68 if (p == 0)
69 error (xmalloc_exit_failure, 0, _("memory exhausted"));
70 return p;
73 /* Allocate N bytes of memory dynamically, with error checking. */
75 VOID *
76 xmalloc (n)
77 size_t n;
79 VOID *p;
81 p = malloc (n);
82 if (p == 0)
83 p = fixup_null_alloc (n);
84 return p;
87 /* Allocate memory for N elements of S bytes, with error checking. */
89 VOID *
90 xcalloc (n, s)
91 size_t n, s;
93 VOID *p;
95 p = calloc (n, s);
96 if (p == 0)
97 p = fixup_null_alloc (n);
98 return p;
101 /* Change the size of an allocated block of memory P to N bytes,
102 with error checking.
103 If P is NULL, run xmalloc. */
105 VOID *
106 xrealloc (p, n)
107 VOID *p;
108 size_t n;
110 if (p == 0)
111 return xmalloc (n);
112 p = realloc (p, n);
113 if (p == 0)
114 p = fixup_null_alloc (n);
115 return p;