1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/*-------------------------------------------------------------------------
* Copyright (C) 1997 National Center for Supercomputing Applications.
* All rights reserved.
*
*-------------------------------------------------------------------------
*
* Created: H5MM.c
* Jul 10 1997
* Robb Matzke <matzke@llnl.gov>
*
* Purpose: Memory management functions.
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
#include <H5private.h>
#include <H5MMprivate.h>
/*-------------------------------------------------------------------------
* Function: H5MM_xmalloc
*
* Purpose: Just like malloc(3) except it aborts on an error.
*
* Return: Success: Ptr to new memory.
*
* Failure: abort()
*
* Programmer: Robb Matzke
* matzke@llnl.gov
* Jul 10 1997
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
void *
H5MM_xmalloc (size_t size)
{
void *mem = HDmalloc (size);
assert (mem);
return mem;
}
/*-------------------------------------------------------------------------
* Function: H5MM_xcalloc
*
* Purpose: Just like calloc(3) except it aborts on an error.
*
* Return: Success: Ptr to memory.
*
* Failure: abort()
*
* Programmer: Robb Matzke
* matzke@llnl.gov
* Jul 10 1997
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
void *
H5MM_xcalloc (size_t n, size_t size)
{
void *mem = HDcalloc (n, size);
assert (mem);
return mem;
}
/*-------------------------------------------------------------------------
* Function: H5MM_xrealloc
*
* Purpose: Just like the POSIX version of realloc(3) exept it aborts
* on an error. Specifically, the following calls are
* equivalent
*
* H5MM_xrealloc (NULL, size) <==> H5MM_xmalloc (size)
* H5MM_xrealloc (ptr, 0) <==> H5MM_xfree (ptr)
* H5MM_xrealloc (NULL, 0) <==> NULL
*
* Return: Success: Ptr to new memory or NULL if the memory
* was freed.
*
* Failure: abort()
*
* Programmer: Robb Matzke
* matzke@llnl.gov
* Jul 10 1997
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
void *
H5MM_xrealloc (void *mem, size_t size)
{
if (!mem) {
if (0==size) return NULL;
mem = H5MM_xmalloc (size);
} else if (0==size) {
mem = H5MM_xfree (mem);
} else {
mem = HDrealloc (mem, size);
assert (mem);
}
return mem;
}
/*-------------------------------------------------------------------------
* Function: H5MM_strdup
*
* Purpose: Duplicates a string. If the string to be duplicated is the
* null pointer, then return null. If the string to be duplicated
* is the empty string then return a new empty string.
*
* Return: Success: Ptr to a new string (or null if no string).
*
* Failure: abort()
*
* Programmer: Robb Matzke
* matzke@llnl.gov
* Jul 10 1997
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
char *
H5MM_xstrdup (const char *s)
{
char *mem;
if (!s) return NULL;
mem = H5MM_xmalloc (HDstrlen (s)+1);
HDstrcpy (mem, s);
return mem;
}
/*-------------------------------------------------------------------------
* Function: H5MM_xfree
*
* Purpose: Just like free(3) except null pointers are allowed as
* arguments, and the return value (always NULL) can be
* assigned to the pointer whose memory was just freed:
*
* thing = H5MM_xfree (thing);
*
* Return: Success: NULL
*
* Failure: never fails
*
* Programmer: Robb Matzke
* matzke@llnl.gov
* Jul 10 1997
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
void *
H5MM_xfree (const void *mem)
{
/*
* free(3) takes a non-const pointer as an argument even though
* conceptually the argument could be a constant because by time
* free() mucks with it's contents, it should already be free :-)
* Instead of passing a const arg to free, which generates a
* compiler warning, we cast it to a non-const arg first. With
* gcc, this results in a warning only if -Wcast-qual is turned on.
*/
void *non_const_mem = mem;
if (mem) HDfree (non_const_mem);
return NULL;
}
|