Debian Codegraph @

src/alloc.c

raw symbols SCIP
1 /* Miscellaneous allocations functions for GNU Make.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
3 This file is part of GNU Make.
4
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "makeint.h"
18
19 /* Like malloc but get fatal error if memory is exhausted. */
20 /* Don't bother if we're using dmalloc; it provides these for us. */
21
22 #ifndef HAVE_DMALLOC_H
23
24 #undef xmalloc
25 #undef xcalloc
26 #undef xrealloc
27 #undef xstrdup
28
29 void *
30 xmalloc (size_t size)
31 {
32 /* Make sure we don't allocate 0, for pre-ISO implementations. */
33 void *result = malloc (size ? size : 1);
34 if (result == 0)
35 OUT_OF_MEM();
36 return result;
37 }
38
39
40 void *
41 xcalloc (size_t size)
42 {
43 /* Make sure we don't allocate 0, for pre-ISO implementations. */
44 void *result = calloc (size ? size : 1, 1);
45 if (result == 0)
46 OUT_OF_MEM();
47 return result;
48 }
49
50
51 void *
52 xrealloc (void *ptr, size_t size)
53 {
54 void *result;
55
56 /* Some older implementations of realloc() don't conform to ISO. */
57 if (! size)
58 size = 1;
59 result = ptr ? realloc (ptr, size) : malloc (size);
60 if (result == 0)
61 OUT_OF_MEM();
62 return result;
63 }
64
65
66 char *
67 xstrdup (const char *ptr)
68 {
69 char *result;
70
71 #ifdef HAVE_STRDUP
72 result = strdup (ptr);
73 #else
74 result = malloc (strlen (ptr) + 1);
75 #endif
76
77 if (result == 0)
78 OUT_OF_MEM();
79
80 #ifdef HAVE_STRDUP
81 return result;
82 #else
83 return strcpy (result, ptr);
84 #endif
85 }
86
87 #endif /* HAVE_DMALLOC_H */
88
89 char *
90 xstrndup (const char *str, size_t length)
91 {
92 char *result;
93
94 #ifdef HAVE_STRNDUP
95 result = strndup (str, length);
96 if (result == 0)
97 OUT_OF_MEM();
98 #else
99 result = xmalloc (length + 1);
100 if (length > 0)
101 strncpy (result, str, length);
102 result[length] = '\0';
103 #endif
104
105 return result;
106 }