1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #ifndef LIBCBOR_MEMORY_UTILS_H
9 #define LIBCBOR_MEMORY_UTILS_H
10 
11 #include <stdbool.h>
12 #include <string.h>
13 
14 /** Can a and b be multiplied without overflowing size_t? */
15 bool _cbor_safe_to_multiply(size_t a, size_t b);
16 
17 /** Overflow-proof contiguous array allocation
18  *
19  * @param item_size
20  * @param item_count
21  * @return Region of item_size * item_count bytes, or NULL if the total size
22  * overflows size_t or the underlying allocator failed
23  */
24 void* _cbor_alloc_multiple(size_t item_size, size_t item_count);
25 
26 /** Overflow-proof contiguous array reallocation
27  *
28  * This implements the OpenBSD `reallocarray` functionality.
29  *
30  * @param pointer
31  * @param item_size
32  * @param item_count
33  * @return Realloc'd of item_size * item_count bytes, or NULL if the total size
34  * overflows size_t or the underlying allocator failed
35  */
36 void* _cbor_realloc_multiple(void* pointer, size_t item_size,
37                              size_t item_count);
38 
39 #endif  // LIBCBOR_MEMORY_UTILS_H
40