/src/core/libntech/libutils/alloc.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright 2023 Northern.tech AS |
3 | | |
4 | | This file is part of CFEngine 3 - written and maintained by Northern.tech AS. |
5 | | |
6 | | Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | you may not use this file except in compliance with the License. |
8 | | You may obtain a copy of the License at |
9 | | |
10 | | http://www.apache.org/licenses/LICENSE-2.0 |
11 | | |
12 | | Unless required by applicable law or agreed to in writing, software |
13 | | distributed under the License is distributed on an "AS IS" BASIS, |
14 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | | See the License for the specific language governing permissions and |
16 | | limitations under the License. |
17 | | |
18 | | To the extent this program is licensed as part of the Enterprise |
19 | | versions of CFEngine, the applicable Commercial Open Source License |
20 | | (COSL) may apply to this file if you as a licensee so wish it. See |
21 | | included file COSL.txt. |
22 | | */ |
23 | | |
24 | | #ifndef CFENGINE_ALLOC_H |
25 | | #define CFENGINE_ALLOC_H |
26 | | |
27 | | #include <stdlib.h> // size_t |
28 | | #include <stdarg.h> // va_list |
29 | | #include <assert.h> // assert() |
30 | | |
31 | | #include <compiler.h> |
32 | | |
33 | | void *xcalloc(size_t nmemb, size_t size); |
34 | | void *xmalloc(size_t size); |
35 | | void *xrealloc(void *ptr, size_t size); |
36 | | char *xstrdup(const char *str); |
37 | | char *xstrndup(const char *str, size_t n); |
38 | | void *xmemdup(const void *mem, size_t size); |
39 | | int xasprintf(char **strp, const char *fmt, ...) FUNC_ATTR_PRINTF(2, 3); |
40 | | int xvasprintf(char **strp, const char *fmt, va_list ap) FUNC_ATTR_PRINTF(2, 0); |
41 | | |
42 | | static inline void free_array_items(void **array, size_t n_items) |
43 | 0 | { |
44 | 0 | assert(array != NULL); |
45 | 0 | for (size_t i = 0; i < n_items; i++) |
46 | 0 | { |
47 | 0 | free(array[i]); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | | #define DESTROY_AND_NULL(destroy, ptr) { destroy(ptr); ptr = NULL; } |
52 | | #define FREE_AND_NULL(ptr) { DESTROY_AND_NULL(free, ptr); } |
53 | | |
54 | | /* |
55 | | * Prevent any code from using un-wrapped allocators. |
56 | | * |
57 | | * Use x* equivalents instead. |
58 | | */ |
59 | | |
60 | | /** |
61 | | * Currently regular malloc() calls are allowed for mission-critical code that |
62 | | * can somehow recover, like cf-serverd dropping connections or cf-execd |
63 | | * postponing its scheduled actions. |
64 | | * |
65 | | * @note for 99% of the cases (libpromises, cf-agent etc) use xmalloc() and |
66 | | * friends. |
67 | | **/ |
68 | | #if 0 |
69 | | |
70 | | # undef malloc |
71 | | # undef calloc |
72 | | # undef realloc |
73 | | # undef strdup |
74 | | # undef strndup |
75 | | # undef memdup |
76 | | # undef asprintf |
77 | | # undef vasprintf |
78 | | # define malloc __error_unchecked_malloc |
79 | | # define calloc __error_unchecked_calloc |
80 | | # define realloc __error_unchecked_realloc |
81 | | # define strdup __error_unchecked_strdup |
82 | | # define strndup __error_unchecked_strndup |
83 | | # define memdup __error_unchecked_memdup |
84 | | # define asprintf __error_unchecked_asprintf |
85 | | # define vasprintf __error_unchecked_vasprintf |
86 | | |
87 | | void __error_unchecked_malloc(void); |
88 | | void __error_unchecked_calloc(void); |
89 | | void __error_unchecked_realloc(void); |
90 | | void __error_unchecked_strdup(void); |
91 | | void __error_unchecked_strndup(void); |
92 | | void __error_unchecked_memdup(void); |
93 | | void __error_unchecked_asprintf(void); |
94 | | void __error_unchecked_vasprintf(void); |
95 | | |
96 | | #endif |
97 | | |
98 | | #endif |