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