]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/alloc-util.h
resolve: voidify sd_event_add_signal() and sd_event_set_watchdog()
[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 static inline void freep(void *p) {
50 free(*(void**) p);
51 }
52
53 #define _cleanup_free_ _cleanup_(freep)
54
55 static inline bool size_multiply_overflow(size_t size, size_t need) {
56 return _unlikely_(need != 0 && size > (SIZE_MAX / need));
57 }
58
59 _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
60 if (size_multiply_overflow(size, need))
61 return NULL;
62
63 return malloc(size * need);
64 }
65
66 #if !HAVE_REALLOCARRAY
67 _alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) {
68 if (size_multiply_overflow(size, need))
69 return NULL;
70
71 return realloc(p, size * need);
72 }
73 #endif
74
75 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
76 if (size_multiply_overflow(size, need))
77 return NULL;
78
79 return memdup(p, size * need);
80 }
81
82 _alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
83 if (size_multiply_overflow(size, need))
84 return NULL;
85
86 return memdup_suffix0(p, size * need);
87 }
88
89 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
90 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
91
92 #define GREEDY_REALLOC(array, allocated, need) \
93 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
94
95 #define GREEDY_REALLOC0(array, allocated, need) \
96 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
97
98 #define alloca0(n) \
99 ({ \
100 char *_new_; \
101 size_t _len_ = n; \
102 _new_ = alloca(_len_); \
103 (void *) memset(_new_, 0, _len_); \
104 })
105
106 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
107 #define alloca_align(size, align) \
108 ({ \
109 void *_ptr_; \
110 size_t _mask_ = (align) - 1; \
111 _ptr_ = alloca((size) + _mask_); \
112 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
113 })
114
115 #define alloca0_align(size, align) \
116 ({ \
117 void *_new_; \
118 size_t _size_ = (size); \
119 _new_ = alloca_align(_size_, (align)); \
120 (void*)memset(_new_, 0, _size_); \
121 })
122
123 /* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
124 * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
125 #define TAKE_PTR(ptr) \
126 ({ \
127 typeof(ptr) _ptr_ = (ptr); \
128 (ptr) = NULL; \
129 _ptr_; \
130 })