]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/alloc-util.h
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / basic / alloc-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <alloca.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "macro.h"
10
11 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
12
13 #define new0(t, n) ((t*) calloc((n), sizeof(t)))
14
15 #define newa(t, n) \
16 ({ \
17 assert(!size_multiply_overflow(sizeof(t), n)); \
18 (t*) alloca(sizeof(t)*(n)); \
19 })
20
21 #define newa0(t, n) \
22 ({ \
23 assert(!size_multiply_overflow(sizeof(t), n)); \
24 (t*) alloca0(sizeof(t)*(n)); \
25 })
26
27 #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n)))
28
29 #define newdup_suffix0(t, p, n) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n)))
30
31 #define malloc0(n) (calloc(1, (n)))
32
33 static inline void *mfree(void *memory) {
34 free(memory);
35 return NULL;
36 }
37
38 #define free_and_replace(a, b) \
39 ({ \
40 free(a); \
41 (a) = (b); \
42 (b) = NULL; \
43 0; \
44 })
45
46 void* memdup(const void *p, size_t l) _alloc_(2);
47 void* memdup_suffix0(const void *p, size_t l) _alloc_(2);
48
49 #define memdupa(p, l) \
50 ({ \
51 void *_q_; \
52 _q_ = alloca(l); \
53 memcpy(_q_, p, l); \
54 })
55
56 #define memdupa_suffix0(p, l) \
57 ({ \
58 void *_q_; \
59 _q_ = alloca(l + 1); \
60 ((uint8_t*) _q_)[l] = 0; \
61 memcpy(_q_, p, l); \
62 })
63
64 static inline void freep(void *p) {
65 free(*(void**) p);
66 }
67
68 #define _cleanup_free_ _cleanup_(freep)
69
70 static inline bool size_multiply_overflow(size_t size, size_t need) {
71 return _unlikely_(need != 0 && size > (SIZE_MAX / need));
72 }
73
74 _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
75 if (size_multiply_overflow(size, need))
76 return NULL;
77
78 return malloc(size * need);
79 }
80
81 #if !HAVE_REALLOCARRAY
82 _alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) {
83 if (size_multiply_overflow(size, need))
84 return NULL;
85
86 return realloc(p, size * need);
87 }
88 #endif
89
90 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
91 if (size_multiply_overflow(size, need))
92 return NULL;
93
94 return memdup(p, size * need);
95 }
96
97 _alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
98 if (size_multiply_overflow(size, need))
99 return NULL;
100
101 return memdup_suffix0(p, size * need);
102 }
103
104 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
105 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
106
107 #define GREEDY_REALLOC(array, allocated, need) \
108 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
109
110 #define GREEDY_REALLOC0(array, allocated, need) \
111 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
112
113 #define alloca0(n) \
114 ({ \
115 char *_new_; \
116 size_t _len_ = n; \
117 _new_ = alloca(_len_); \
118 (void *) memset(_new_, 0, _len_); \
119 })
120
121 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
122 #define alloca_align(size, align) \
123 ({ \
124 void *_ptr_; \
125 size_t _mask_ = (align) - 1; \
126 _ptr_ = alloca((size) + _mask_); \
127 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
128 })
129
130 #define alloca0_align(size, align) \
131 ({ \
132 void *_new_; \
133 size_t _size_ = (size); \
134 _new_ = alloca_align(_size_, (align)); \
135 (void*)memset(_new_, 0, _size_); \
136 })
137
138 /* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
139 * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
140 #define TAKE_PTR(ptr) \
141 ({ \
142 typeof(ptr) _ptr_ = (ptr); \
143 (ptr) = NULL; \
144 _ptr_; \
145 })