Debian Codegraph @

crt/aws-c-common/source/allocator.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/assert.h>
7 #include <aws/common/common.h>
8 #include <aws/common/logging.h>
9 #include <aws/common/math.h>
10
11 #include <stdarg.h>
12 #include <stdlib.h>
13
14 #ifdef _WIN32
15 # include <windows.h>
16 #endif
17
18 #ifdef __MACH__
19 # include <CoreFoundation/CoreFoundation.h>
20 #endif
21
22 /* turn off unused named parameter warning on msvc.*/
23 #ifdef _MSC_VER
24 # pragma warning(push)
25 # pragma warning(disable : 4100)
26 #endif
27
28 #ifndef PAGE_SIZE
29 # define PAGE_SIZE (4 * 1024)
30 #endif
31
32 bool aws_allocator_is_valid(const struct aws_allocator *alloc) {
33 /* An allocator must define mem_acquire and mem_release. All other fields are optional */
34 return alloc && AWS_OBJECT_PTR_IS_READABLE(alloc) && alloc->mem_acquire && alloc->mem_release;
35 }
36
37 static void *s_aligned_malloc(struct aws_allocator *allocator, size_t size) {
38 size_t alignment = 0;
39 if (allocator->impl != NULL) {
40 alignment = (size_t)allocator->impl;
41 } else {
42 /**
43 * For implicit alignment.
44 * larger allocations should be aligned so that AVX and friends can avoid
45 * the extra preamble during unaligned versions of memcpy/memset on big buffers
46 * This will also accelerate hardware CRC and SHA on ARM chips
47 *
48 * 64 byte alignment for > page allocations on 64 bit systems
49 * 32 byte alignment for > page allocations on 32 bit systems
50 * 16 byte alignment for <= page allocations on 64 bit systems
51 * 8 byte alignment for <= page allocations on 32 bit systems
52 *
53 * We use PAGE_SIZE as the boundary because we are not aware of any allocations of
54 * this size or greater that are not data buffers.
55 *
56 * Unless there is a customized alignment size.
57 */
58 alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 8 : 2);
59 }
60 #if !defined(_WIN32)
61 void *result = NULL;
62 int err = posix_memalign(&result, alignment, size);
63 (void)err;
64 AWS_PANIC_OOM(result, "posix_memalign failed to allocate memory");
65 return result;
66 #else
67 void *mem = _aligned_malloc(size, alignment);
68 AWS_FATAL_POSTCONDITION(mem && "_aligned_malloc failed to allocate memory");
69 return mem;
70 #endif
71 }
72
73 static void s_aligned_free(struct aws_allocator *allocator, void *ptr) {
74 (void)allocator;
75 #if !defined(_WIN32)
76 free(ptr);
77 #else
78 _aligned_free(ptr);
79 #endif
80 }
81
82 static void *s_aligned_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
83 (void)allocator;
84 (void)oldsize;
85 AWS_FATAL_PRECONDITION(newsize);
86
87 #if !defined(_WIN32)
88 if (newsize <= oldsize) {
89 return ptr;
90 }
91
92 /* newsize is > oldsize, need more memory */
93 void *new_mem = s_aligned_malloc(allocator, newsize);
94 AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in s_aligned_malloc");
95
96 if (ptr) {
97 memcpy(new_mem, ptr, oldsize);
98 s_aligned_free(allocator, ptr);
99 }
100
101 return new_mem;
102 #else
103 const size_t alignment = sizeof(void *) * (newsize > (size_t)PAGE_SIZE ? 8 : 2);
104 void *new_mem = _aligned_realloc(ptr, newsize, alignment);
105 AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in _aligned_realloc");
106 return new_mem;
107 #endif
108 }
109
110 static void *s_aligned_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
111 void *mem = s_aligned_malloc(allocator, num * size);
112 AWS_PANIC_OOM(mem, "Unhandled OOM encountered in s_aligned_calloc");
113 memset(mem, 0, num * size);
114 return mem;
115 }
116
117 static void *s_non_aligned_malloc(struct aws_allocator *allocator, size_t size) {
118 (void)allocator;
119 void *result = malloc(size);
120 AWS_PANIC_OOM(result, "malloc failed to allocate memory");
121 return result;
122 }
123
124 static void s_non_aligned_free(struct aws_allocator *allocator, void *ptr) {
125 (void)allocator;
126 free(ptr);
127 }
128
129 static void *s_non_aligned_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
130 (void)allocator;
131 (void)oldsize;
132 AWS_FATAL_PRECONDITION(newsize);
133
134 if (newsize <= oldsize) {
135 return ptr;
136 }
137
138 /* newsize is > oldsize, need more memory */
139 void *new_mem = s_non_aligned_malloc(allocator, newsize);
140 AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in s_non_aligned_realloc");
141
142 if (ptr) {
143 memcpy(new_mem, ptr, oldsize);
144 s_non_aligned_free(allocator, ptr);
145 }
146
147 return new_mem;
148 }
149
150 static void *s_non_aligned_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
151 (void)allocator;
152 void *mem = calloc(num, size);
153 AWS_PANIC_OOM(mem, "Unhandled OOM encountered in s_non_aligned_calloc");
154 return mem;
155 }
156
157 static struct aws_allocator s_default_allocator = {
158 .mem_acquire = s_non_aligned_malloc,
159 .mem_release = s_non_aligned_free,
160 .mem_realloc = s_non_aligned_realloc,
161 .mem_calloc = s_non_aligned_calloc,
162 };
163
164 struct aws_allocator *aws_default_allocator(void) {
165 return &s_default_allocator;
166 }
167
168 static struct aws_allocator s_implicit_aligned_allocator = {
169 .mem_acquire = s_aligned_malloc,
170 .mem_release = s_aligned_free,
171 .mem_realloc = s_aligned_realloc,
172 .mem_calloc = s_aligned_calloc,
173 };
174
175 struct aws_allocator *aws_aligned_allocator(void) {
176 return &s_implicit_aligned_allocator;
177 }
178
179 struct aws_allocator *aws_explicit_aligned_allocator_new(size_t customized_alignment) {
180 if (customized_alignment == 0 || (customized_alignment & (customized_alignment - 1)) != 0 ||
181 customized_alignment % sizeof(void *) != 0) {
182 /**
183 * the alignment must be a power of two and a multiple of sizeof(void *) and non-zero.
184 */
185 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
186 return NULL;
187 }
188 struct aws_allocator *aligned_alloc = aws_mem_calloc(aws_default_allocator(), 1, sizeof(struct aws_allocator));
189 *aligned_alloc = s_implicit_aligned_allocator;
190 aligned_alloc->impl = (void *)customized_alignment;
191 return aligned_alloc;
192 }
193
194 void aws_explicit_aligned_allocator_destroy(struct aws_allocator *aligned_alloc) {
195 if (!aligned_alloc) {
196 return;
197 }
198 aws_mem_release(aws_default_allocator(), aligned_alloc);
199 }
200
201 void *aws_mem_acquire(struct aws_allocator *allocator, size_t size) {
202 AWS_FATAL_PRECONDITION(allocator != NULL);
203 AWS_FATAL_PRECONDITION(allocator->mem_acquire != NULL);
204 /* Protect against https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations */
205 AWS_FATAL_PRECONDITION(size != 0);
206
207 void *mem = allocator->mem_acquire(allocator, size);
208 AWS_PANIC_OOM(mem, "Unhandled OOM encountered in aws_mem_acquire with allocator");
209
210 return mem;
211 }
212
213 void *aws_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
214 AWS_FATAL_PRECONDITION(allocator != NULL);
215 AWS_FATAL_PRECONDITION(allocator->mem_calloc || allocator->mem_acquire);
216 /* Protect against https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations */
217 AWS_FATAL_PRECONDITION(num != 0 && size != 0);
218
219 /* Defensive check: never use calloc with size * num that would overflow
220 * https://wiki.sei.cmu.edu/confluence/display/c/MEM07-C.+Ensure+that+the+arguments+to+calloc%28%29%2C+when+multiplied%2C+do+not+wrap
221 */
222 size_t required_bytes = 0;
223 AWS_FATAL_POSTCONDITION(!aws_mul_size_checked(num, size, &required_bytes), "calloc computed size > SIZE_MAX");
224
225 /* If there is a defined calloc, use it */
226 if (allocator->mem_calloc) {
227 void *mem = allocator->mem_calloc(allocator, num, size);
228 AWS_PANIC_OOM(mem, "Unhandled OOM encountered in aws_mem_acquire with allocator");
229 return mem;
230 }
231
232 /* Otherwise, emulate calloc */
233 void *mem = allocator->mem_acquire(allocator, required_bytes);
234 AWS_PANIC_OOM(mem, "Unhandled OOM encountered in aws_mem_acquire with allocator");
235
236 memset(mem, 0, required_bytes);
237 return mem;
238 }
239
240 #define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1))
241
242 void *aws_mem_acquire_many(struct aws_allocator *allocator, size_t count, ...) {
243
244 enum { S_ALIGNMENT = sizeof(intmax_t) };
245
246 va_list args_size;
247 va_start(args_size, count);
248 va_list args_allocs;
249 va_copy(args_allocs, args_size);
250
251 size_t total_size = 0;
252 for (size_t i = 0; i < count; ++i) {
253
254 /* Ignore the pointer argument for now */
255 va_arg(args_size, void **);
256
257 size_t alloc_size = va_arg(args_size, size_t);
258 total_size += AWS_ALIGN_ROUND_UP(alloc_size, S_ALIGNMENT);
259 }
260 va_end(args_size);
261
262 void *allocation = NULL;
263
264 if (total_size > 0) {
265
266 allocation = aws_mem_acquire(allocator, total_size);
267 AWS_PANIC_OOM(allocation, "Unhandled OOM encountered in aws_mem_acquire with allocator");
268
269 uint8_t *current_ptr = allocation;
270
271 for (size_t i = 0; i < count; ++i) {
272
273 void **out_ptr = va_arg(args_allocs, void **);
274
275 size_t alloc_size = va_arg(args_allocs, size_t);
276 alloc_size = AWS_ALIGN_ROUND_UP(alloc_size, S_ALIGNMENT);
277
278 *out_ptr = current_ptr;
279 current_ptr += alloc_size;
280 }
281 }
282
283 va_end(args_allocs);
284 return allocation;
285 }
286
287 #undef AWS_ALIGN_ROUND_UP
288
289 void aws_mem_release(struct aws_allocator *allocator, void *ptr) {
290 AWS_FATAL_PRECONDITION(allocator != NULL);
291 AWS_FATAL_PRECONDITION(allocator->mem_release != NULL);
292
293 if (ptr != NULL) {
294 allocator->mem_release(allocator, ptr);
295 }
296 }
297
298 int aws_mem_realloc(struct aws_allocator *allocator, void **ptr, size_t oldsize, size_t newsize) {
299 AWS_FATAL_PRECONDITION(allocator != NULL);
300 AWS_FATAL_PRECONDITION(allocator->mem_realloc || allocator->mem_acquire);
301 AWS_FATAL_PRECONDITION(allocator->mem_release);
302
303 /* Protect against https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations */
304 if (newsize == 0) {
305 aws_mem_release(allocator, *ptr);
306 *ptr = NULL;
307 return AWS_OP_SUCCESS;
308 }
309
310 if (allocator->mem_realloc) {
311 void *newptr = allocator->mem_realloc(allocator, *ptr, oldsize, newsize);
312 AWS_PANIC_OOM(newptr, "Unhandled OOM encountered in aws_mem_acquire with allocator");
313
314 *ptr = newptr;
315 return AWS_OP_SUCCESS;
316 }
317
318 /* Since the allocator doesn't support realloc, we'll need to emulate it (inefficiently). */
319 if (oldsize >= newsize) {
320 return AWS_OP_SUCCESS;
321 }
322
323 void *newptr = allocator->mem_acquire(allocator, newsize);
324 AWS_PANIC_OOM(newptr, "Unhandled OOM encountered in aws_mem_acquire with allocator");
325
326 memcpy(newptr, *ptr, oldsize);
327 memset((uint8_t *)newptr + oldsize, 0, newsize - oldsize);
328
329 aws_mem_release(allocator, *ptr);
330
331 *ptr = newptr;
332
333 return AWS_OP_SUCCESS;
334 }
335
336 /* Wraps a CFAllocator around aws_allocator. For Mac only. */
337 #ifdef __MACH__
338
339 static CFStringRef s_cf_allocator_description = CFSTR("CFAllocator wrapping aws_allocator.");
340
341 /* note we don't have a standard specification stating sizeof(size_t) == sizeof(void *) so we have some extra casts */
342 static void *s_cf_allocator_allocate(CFIndex alloc_size, CFOptionFlags hint, void *info) {
343 (void)hint;
344
345 struct aws_allocator *allocator = info;
346
347 void *mem = aws_mem_acquire(allocator, (size_t)alloc_size + sizeof(size_t));
348
349 size_t allocation_size = (size_t)alloc_size + sizeof(size_t);
350 memcpy(mem, &allocation_size, sizeof(size_t));
351 return (void *)((uint8_t *)mem + sizeof(size_t));
352 }
353
354 static void s_cf_allocator_deallocate(void *ptr, void *info) {
355 struct aws_allocator *allocator = info;
356
357 void *original_allocation = (uint8_t *)ptr - sizeof(size_t);
358
359 aws_mem_release(allocator, original_allocation);
360 }
361
362 static void *s_cf_allocator_reallocate(void *ptr, CFIndex new_size, CFOptionFlags hint, void *info) {
363 (void)hint;
364
365 struct aws_allocator *allocator = info;
366 AWS_ASSERT(allocator->mem_realloc);
367
368 void *original_allocation = (uint8_t *)ptr - sizeof(size_t);
369 size_t original_size = 0;
370 memcpy(&original_size, original_allocation, sizeof(size_t));
371
372 aws_mem_realloc(allocator, &original_allocation, original_size, (size_t)new_size);
373 AWS_FATAL_ASSERT(original_allocation);
374 size_t new_allocation_size = (size_t)new_size;
375 memcpy(original_allocation, &new_allocation_size, sizeof(size_t));
376
377 return (void *)((uint8_t *)original_allocation + sizeof(size_t));
378 }
379
380 static CFStringRef s_cf_allocator_copy_description(const void *info) {
381 (void)info;
382
383 return s_cf_allocator_description;
384 }
385
386 static CFIndex s_cf_allocator_preferred_size(CFIndex size, CFOptionFlags hint, void *info) {
387 (void)hint;
388 (void)info;
389
390 return (CFIndex)(size + sizeof(size_t));
391 }
392
393 CFAllocatorRef aws_wrapped_cf_allocator_new(struct aws_allocator *allocator) {
394 CFAllocatorRef cf_allocator = NULL;
395
396 CFAllocatorReallocateCallBack reallocate_callback = NULL;
397
398 if (allocator->mem_realloc) {
399 reallocate_callback = s_cf_allocator_reallocate;
400 }
401
402 CFAllocatorContext context = {
403 .allocate = s_cf_allocator_allocate,
404 .copyDescription = s_cf_allocator_copy_description,
405 .deallocate = s_cf_allocator_deallocate,
406 .reallocate = reallocate_callback,
407 .info = allocator,
408 .preferredSize = s_cf_allocator_preferred_size,
409 .release = NULL,
410 .retain = NULL,
411 .version = 0,
412 };
413
414 cf_allocator = CFAllocatorCreate(NULL, &context);
415
416 AWS_FATAL_ASSERT(cf_allocator && "creation of cf allocator failed!");
417
418 return cf_allocator;
419 }
420
421 void aws_wrapped_cf_allocator_destroy(CFAllocatorRef allocator) {
422 CFRelease(allocator);
423 }
424
425 #endif /*__MACH__ */