PowerPC: remove branch prediction from rint implementation
[glibc.git] / locale / programs / xmalloc.c
blob33ff5502af3362d3496853fef88c0d9759fd20bf
1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990,91,92,93,94,95,96,97,2004,2005,2012
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, see <http://www.gnu.org/licenses/>. */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #define VOID void
25 #include <sys/types.h>
27 #if STDC_HEADERS || _LIBC
28 #include <stdlib.h>
29 static VOID *fixup_null_alloc (size_t n) __THROW;
30 VOID *xmalloc (size_t n) __THROW;
31 VOID *xcalloc (size_t n, size_t s) __THROW;
32 VOID *xrealloc (VOID *p, size_t n) __THROW;
33 #else
34 VOID *calloc ();
35 VOID *malloc ();
36 VOID *realloc ();
37 void free ();
38 #endif
40 #include <libintl.h>
41 #include "error.h"
43 #ifndef _
44 # define _(str) gettext (str)
45 #endif
47 #ifndef EXIT_FAILURE
48 #define EXIT_FAILURE 4
49 #endif
51 /* Exit value when the requested amount of memory is not available.
52 The caller may set it to some other value. */
53 int xmalloc_exit_failure = EXIT_FAILURE;
55 static VOID *
56 fixup_null_alloc (n)
57 size_t n;
59 VOID *p;
61 p = 0;
62 if (n == 0)
63 p = malloc ((size_t) 1);
64 if (p == 0)
65 error (xmalloc_exit_failure, 0, _("memory exhausted"));
66 return p;
69 /* Allocate N bytes of memory dynamically, with error checking. */
71 VOID *
72 xmalloc (n)
73 size_t n;
75 VOID *p;
77 p = malloc (n);
78 if (p == 0)
79 p = fixup_null_alloc (n);
80 return p;
83 /* Allocate memory for N elements of S bytes, with error checking. */
85 VOID *
86 xcalloc (n, s)
87 size_t n, s;
89 VOID *p;
91 p = calloc (n, s);
92 if (p == 0)
93 p = fixup_null_alloc (n);
94 return p;
97 /* Change the size of an allocated block of memory P to N bytes,
98 with error checking.
99 If P is NULL, run xmalloc. */
101 VOID *
102 xrealloc (p, n)
103 VOID *p;
104 size_t n;
106 if (p == 0)
107 return xmalloc (n);
108 p = realloc (p, n);
109 if (p == 0)
110 p = fixup_null_alloc (n);
111 return p;