line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright 2013 MongoDB, Inc. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
|
|
|
|
* you may not use this file except in compliance with the License. |
6
|
|
|
|
|
|
|
* You may obtain a copy of the License at |
7
|
|
|
|
|
|
|
* |
8
|
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
|
|
|
|
* |
10
|
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software |
11
|
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
|
|
|
|
* See the License for the specific language governing permissions and |
14
|
|
|
|
|
|
|
* limitations under the License. |
15
|
|
|
|
|
|
|
*/ |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#include |
19
|
|
|
|
|
|
|
#include |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#include "bson-atomic.h" |
22
|
|
|
|
|
|
|
#include "bson-config.h" |
23
|
|
|
|
|
|
|
#include "bson-memory.h" |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
static bson_mem_vtable_t gMemVtable = { |
27
|
|
|
|
|
|
|
malloc, |
28
|
|
|
|
|
|
|
calloc, |
29
|
|
|
|
|
|
|
#ifdef __APPLE__ |
30
|
|
|
|
|
|
|
reallocf, |
31
|
|
|
|
|
|
|
#else |
32
|
|
|
|
|
|
|
realloc, |
33
|
|
|
|
|
|
|
#endif |
34
|
|
|
|
|
|
|
free, |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
/* |
39
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
40
|
|
|
|
|
|
|
* |
41
|
|
|
|
|
|
|
* bson_malloc -- |
42
|
|
|
|
|
|
|
* |
43
|
|
|
|
|
|
|
* Allocates @num_bytes of memory and returns a pointer to it. If |
44
|
|
|
|
|
|
|
* malloc failed to allocate the memory, abort() is called. |
45
|
|
|
|
|
|
|
* |
46
|
|
|
|
|
|
|
* Libbson does not try to handle OOM conditions as it is beyond the |
47
|
|
|
|
|
|
|
* scope of this library to handle so appropriately. |
48
|
|
|
|
|
|
|
* |
49
|
|
|
|
|
|
|
* Parameters: |
50
|
|
|
|
|
|
|
* @num_bytes: The number of bytes to allocate. |
51
|
|
|
|
|
|
|
* |
52
|
|
|
|
|
|
|
* Returns: |
53
|
|
|
|
|
|
|
* A pointer if successful; otherwise abort() is called and this |
54
|
|
|
|
|
|
|
* function will never return. |
55
|
|
|
|
|
|
|
* |
56
|
|
|
|
|
|
|
* Side effects: |
57
|
|
|
|
|
|
|
* None. |
58
|
|
|
|
|
|
|
* |
59
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
60
|
|
|
|
|
|
|
*/ |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
void * |
63
|
2151
|
|
|
|
|
|
bson_malloc (size_t num_bytes) /* IN */ |
64
|
|
|
|
|
|
|
{ |
65
|
|
|
|
|
|
|
void *mem; |
66
|
|
|
|
|
|
|
|
67
|
2151
|
50
|
|
|
|
|
if (!(mem = gMemVtable.malloc (num_bytes))) { |
68
|
0
|
|
|
|
|
|
abort (); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2151
|
|
|
|
|
|
return mem; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
/* |
76
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
77
|
|
|
|
|
|
|
* |
78
|
|
|
|
|
|
|
* bson_malloc0 -- |
79
|
|
|
|
|
|
|
* |
80
|
|
|
|
|
|
|
* Like bson_malloc() except the memory is zeroed first. This is |
81
|
|
|
|
|
|
|
* similar to calloc() except that abort() is called in case of |
82
|
|
|
|
|
|
|
* failure to allocate memory. |
83
|
|
|
|
|
|
|
* |
84
|
|
|
|
|
|
|
* Parameters: |
85
|
|
|
|
|
|
|
* @num_bytes: The number of bytes to allocate. |
86
|
|
|
|
|
|
|
* |
87
|
|
|
|
|
|
|
* Returns: |
88
|
|
|
|
|
|
|
* A pointer if successful; otherwise abort() is called and this |
89
|
|
|
|
|
|
|
* function will never return. |
90
|
|
|
|
|
|
|
* |
91
|
|
|
|
|
|
|
* Side effects: |
92
|
|
|
|
|
|
|
* None. |
93
|
|
|
|
|
|
|
* |
94
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
95
|
|
|
|
|
|
|
*/ |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
void * |
98
|
0
|
|
|
|
|
|
bson_malloc0 (size_t num_bytes) /* IN */ |
99
|
|
|
|
|
|
|
{ |
100
|
0
|
|
|
|
|
|
void *mem = NULL; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
if (BSON_LIKELY (num_bytes)) { |
103
|
0
|
0
|
|
|
|
|
if (BSON_UNLIKELY (!(mem = gMemVtable.calloc (1, num_bytes)))) { |
104
|
0
|
|
|
|
|
|
abort (); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
return mem; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
/* |
113
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
114
|
|
|
|
|
|
|
* |
115
|
|
|
|
|
|
|
* bson_realloc -- |
116
|
|
|
|
|
|
|
* |
117
|
|
|
|
|
|
|
* This function behaves similar to realloc() except that if there is |
118
|
|
|
|
|
|
|
* a failure abort() is called. |
119
|
|
|
|
|
|
|
* |
120
|
|
|
|
|
|
|
* Parameters: |
121
|
|
|
|
|
|
|
* @mem: The memory to realloc, or NULL. |
122
|
|
|
|
|
|
|
* @num_bytes: The size of the new allocation or 0 to free. |
123
|
|
|
|
|
|
|
* |
124
|
|
|
|
|
|
|
* Returns: |
125
|
|
|
|
|
|
|
* The new allocation if successful; otherwise abort() is called and |
126
|
|
|
|
|
|
|
* this function never returns. |
127
|
|
|
|
|
|
|
* |
128
|
|
|
|
|
|
|
* Side effects: |
129
|
|
|
|
|
|
|
* None. |
130
|
|
|
|
|
|
|
* |
131
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
132
|
|
|
|
|
|
|
*/ |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
void * |
135
|
24
|
|
|
|
|
|
bson_realloc (void *mem, /* IN */ |
136
|
|
|
|
|
|
|
size_t num_bytes) /* IN */ |
137
|
|
|
|
|
|
|
{ |
138
|
|
|
|
|
|
|
/* |
139
|
|
|
|
|
|
|
* Not all platforms are guaranteed to free() the memory if a call to |
140
|
|
|
|
|
|
|
* realloc() with a size of zero occurs. Windows, Linux, and FreeBSD do, |
141
|
|
|
|
|
|
|
* however, OS X does not. |
142
|
|
|
|
|
|
|
*/ |
143
|
24
|
50
|
|
|
|
|
if (BSON_UNLIKELY (num_bytes == 0)) { |
144
|
0
|
|
|
|
|
|
gMemVtable.free (mem); |
145
|
0
|
|
|
|
|
|
return NULL; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
24
|
|
|
|
|
|
mem = gMemVtable.realloc (mem, num_bytes); |
149
|
|
|
|
|
|
|
|
150
|
24
|
50
|
|
|
|
|
if (BSON_UNLIKELY (!mem)) { |
151
|
0
|
|
|
|
|
|
abort (); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
24
|
|
|
|
|
|
return mem; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
/* |
159
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
160
|
|
|
|
|
|
|
* |
161
|
|
|
|
|
|
|
* bson_realloc_ctx -- |
162
|
|
|
|
|
|
|
* |
163
|
|
|
|
|
|
|
* This wraps bson_realloc and provides a compatible api for similar |
164
|
|
|
|
|
|
|
* functions with a context |
165
|
|
|
|
|
|
|
* |
166
|
|
|
|
|
|
|
* Parameters: |
167
|
|
|
|
|
|
|
* @mem: The memory to realloc, or NULL. |
168
|
|
|
|
|
|
|
* @num_bytes: The size of the new allocation or 0 to free. |
169
|
|
|
|
|
|
|
* @ctx: Ignored |
170
|
|
|
|
|
|
|
* |
171
|
|
|
|
|
|
|
* Returns: |
172
|
|
|
|
|
|
|
* The new allocation if successful; otherwise abort() is called and |
173
|
|
|
|
|
|
|
* this function never returns. |
174
|
|
|
|
|
|
|
* |
175
|
|
|
|
|
|
|
* Side effects: |
176
|
|
|
|
|
|
|
* None. |
177
|
|
|
|
|
|
|
* |
178
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
179
|
|
|
|
|
|
|
*/ |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
void * |
183
|
24
|
|
|
|
|
|
bson_realloc_ctx (void *mem, /* IN */ |
184
|
|
|
|
|
|
|
size_t num_bytes, /* IN */ |
185
|
|
|
|
|
|
|
void *ctx) /* IN */ |
186
|
|
|
|
|
|
|
{ |
187
|
24
|
|
|
|
|
|
return bson_realloc (mem, num_bytes); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
/* |
192
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
193
|
|
|
|
|
|
|
* |
194
|
|
|
|
|
|
|
* bson_free -- |
195
|
|
|
|
|
|
|
* |
196
|
|
|
|
|
|
|
* Frees @mem using the underlying allocator. |
197
|
|
|
|
|
|
|
* |
198
|
|
|
|
|
|
|
* Currently, this only calls free() directly, but that is subject to |
199
|
|
|
|
|
|
|
* change. |
200
|
|
|
|
|
|
|
* |
201
|
|
|
|
|
|
|
* Parameters: |
202
|
|
|
|
|
|
|
* @mem: An allocation to free. |
203
|
|
|
|
|
|
|
* |
204
|
|
|
|
|
|
|
* Returns: |
205
|
|
|
|
|
|
|
* None. |
206
|
|
|
|
|
|
|
* |
207
|
|
|
|
|
|
|
* Side effects: |
208
|
|
|
|
|
|
|
* None. |
209
|
|
|
|
|
|
|
* |
210
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
211
|
|
|
|
|
|
|
*/ |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
void |
214
|
2134
|
|
|
|
|
|
bson_free (void *mem) /* IN */ |
215
|
|
|
|
|
|
|
{ |
216
|
2134
|
|
|
|
|
|
gMemVtable.free (mem); |
217
|
2134
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
/* |
221
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
222
|
|
|
|
|
|
|
* |
223
|
|
|
|
|
|
|
* bson_zero_free -- |
224
|
|
|
|
|
|
|
* |
225
|
|
|
|
|
|
|
* Frees @mem using the underlying allocator. @size bytes of @mem will |
226
|
|
|
|
|
|
|
* be zeroed before freeing the memory. This is useful in scenarios |
227
|
|
|
|
|
|
|
* where @mem contains passwords or other sensitive information. |
228
|
|
|
|
|
|
|
* |
229
|
|
|
|
|
|
|
* Parameters: |
230
|
|
|
|
|
|
|
* @mem: An allocation to free. |
231
|
|
|
|
|
|
|
* @size: The number of bytes in @mem. |
232
|
|
|
|
|
|
|
* |
233
|
|
|
|
|
|
|
* Returns: |
234
|
|
|
|
|
|
|
* None. |
235
|
|
|
|
|
|
|
* |
236
|
|
|
|
|
|
|
* Side effects: |
237
|
|
|
|
|
|
|
* None. |
238
|
|
|
|
|
|
|
* |
239
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
240
|
|
|
|
|
|
|
*/ |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
void |
243
|
0
|
|
|
|
|
|
bson_zero_free (void *mem, /* IN */ |
244
|
|
|
|
|
|
|
size_t size) /* IN */ |
245
|
|
|
|
|
|
|
{ |
246
|
0
|
0
|
|
|
|
|
if (BSON_LIKELY (mem)) { |
247
|
0
|
|
|
|
|
|
memset (mem, 0, size); |
248
|
0
|
|
|
|
|
|
gMemVtable.free (mem); |
249
|
|
|
|
|
|
|
} |
250
|
0
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
/* |
254
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
255
|
|
|
|
|
|
|
* |
256
|
|
|
|
|
|
|
* bson_mem_set_vtable -- |
257
|
|
|
|
|
|
|
* |
258
|
|
|
|
|
|
|
* This function will change our allocationt vtable. |
259
|
|
|
|
|
|
|
* |
260
|
|
|
|
|
|
|
* It is imperitive that this is called at the beginning of the |
261
|
|
|
|
|
|
|
* process before any memory has been allocated by the default |
262
|
|
|
|
|
|
|
* allocator. |
263
|
|
|
|
|
|
|
* |
264
|
|
|
|
|
|
|
* Returns: |
265
|
|
|
|
|
|
|
* None. |
266
|
|
|
|
|
|
|
* |
267
|
|
|
|
|
|
|
* Side effects: |
268
|
|
|
|
|
|
|
* None. |
269
|
|
|
|
|
|
|
* |
270
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
271
|
|
|
|
|
|
|
*/ |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
void |
274
|
0
|
|
|
|
|
|
bson_mem_set_vtable (const bson_mem_vtable_t *vtable) |
275
|
|
|
|
|
|
|
{ |
276
|
0
|
0
|
|
|
|
|
BSON_ASSERT (vtable); |
277
|
|
|
|
|
|
|
|
278
|
0
|
0
|
|
|
|
|
if (!vtable->malloc || |
|
|
0
|
|
|
|
|
|
279
|
0
|
0
|
|
|
|
|
!vtable->calloc || |
280
|
0
|
0
|
|
|
|
|
!vtable->realloc || |
281
|
0
|
|
|
|
|
|
!vtable->free) { |
282
|
0
|
|
|
|
|
|
fprintf (stderr, "Failure to install BSON vtable, " |
283
|
|
|
|
|
|
|
"missing functions.\n"); |
284
|
0
|
|
|
|
|
|
return; |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
|
287
|
0
|
|
|
|
|
|
gMemVtable = *vtable; |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
void |
291
|
0
|
|
|
|
|
|
bson_mem_restore_vtable (void) |
292
|
|
|
|
|
|
|
{ |
293
|
0
|
|
|
|
|
|
bson_mem_vtable_t vtable = { |
294
|
|
|
|
|
|
|
malloc, |
295
|
|
|
|
|
|
|
calloc, |
296
|
|
|
|
|
|
|
#ifdef __APPLE__ |
297
|
|
|
|
|
|
|
reallocf, |
298
|
|
|
|
|
|
|
#else |
299
|
|
|
|
|
|
|
realloc, |
300
|
|
|
|
|
|
|
#endif |
301
|
|
|
|
|
|
|
free, |
302
|
|
|
|
|
|
|
}; |
303
|
|
|
|
|
|
|
|
304
|
0
|
|
|
|
|
|
bson_mem_set_vtable(&vtable); |
305
|
0
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
|