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