Debian Codegraph @

src/basic/alloc-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <malloc.h>
4
5 #include "alloc-util.h"
6
7 void* memdup(const void *p, size_t l) {
8 void *ret;
9
10 assert(l == 0 || p);
11
12 ret = malloc(l ?: 1);
13 if (!ret)
14 return NULL;
15
16 return memcpy_safe(ret, p, l);
17 }
18
19 void* memdup_suffix0(const void *p, size_t l) {
20 void *ret;
21
22 assert(l == 0 || p);
23
24 /* The same as memdup() but place a safety NUL byte after the allocated memory */
25
26 if (_unlikely_(l == SIZE_MAX)) /* prevent overflow */
27 return NULL;
28
29 ret = malloc(l + 1);
30 if (!ret)
31 return NULL;
32
33 ((uint8_t*) ret)[l] = 0;
34 return memcpy_safe(ret, p, l);
35 }
36
37 size_t malloc_sizeof_safe(void **xp) {
38 POINTER_MAY_BE_NULL(xp);
39
40 if (_unlikely_(!xp || !*xp))
41 return 0;
42
43 size_t sz = malloc_usable_size(*xp);
44 *xp = expand_to_usable(*xp, sz);
45 /* GCC doesn't see the _returns_nonnull_ when built with ubsan, so yet another hint to make it doubly
46 * clear that expand_to_usable won't return NULL.
47 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79265 */
48 if (!*xp)
49 assert_not_reached();
50 return sz;
51 }
52
53 void* expand_to_usable(void *ptr, size_t newsize _unused_) {
54 return ptr;
55 }
56
57 void* realloc0(void *p, size_t new_size) {
58 size_t old_size;
59 void *q;
60
61 /* Like realloc(), but initializes anything appended to zero */
62
63 old_size = MALLOC_SIZEOF_SAFE(p);
64
65 q = realloc(p, new_size);
66 if (!q)
67 return NULL;
68
69 new_size = MALLOC_SIZEOF_SAFE(q); /* Update with actually allocated space */
70
71 if (new_size > old_size)
72 memset((uint8_t*) q + old_size, 0, new_size - old_size);
73
74 return q;
75 }
76
77 void* greedy_realloc(
78 void **p,
79 size_t need,
80 size_t size) {
81
82 size_t newalloc;
83 void *q;
84
85 assert(p);
86
87 /* We use malloc_usable_size() for determining the current allocated size. On all systems we care
88 * about this should be safe to rely on. Should there ever arise the need to avoid relying on this we
89 * can instead locally fall back to realloc() on every call, rounded up to the next exponent of 2 or
90 * so. */
91
92 if (*p && (size == 0 || (MALLOC_SIZEOF_SAFE(*p) / size >= need)))
93 return *p;
94
95 if (_unlikely_(need > SIZE_MAX/2)) /* Overflow check */
96 return NULL;
97 newalloc = need * 2;
98
99 if (!MUL_ASSIGN_SAFE(&newalloc, size))
100 return NULL;
101
102 if (newalloc < 64) /* Allocate at least 64 bytes */
103 newalloc = 64;
104
105 q = realloc(*p, newalloc);
106 if (!q)
107 return NULL;
108
109 return *p = q;
110 }
111
112 void* greedy_realloc0(
113 void **p,
114 size_t need,
115 size_t size) {
116
117 size_t before, after;
118 uint8_t *q;
119
120 assert(p);
121
122 before = MALLOC_SIZEOF_SAFE(*p); /* malloc_usable_size() will return 0 on NULL input, as per docs */
123
124 q = greedy_realloc(p, need, size);
125 if (!q)
126 return NULL;
127
128 after = MALLOC_SIZEOF_SAFE(q);
129
130 if (size == 0) /* avoid division by zero */
131 before = 0;
132 else
133 before = (before / size) * size; /* Round down */
134
135 if (after > before)
136 memzero(q + before, after - before);
137
138 return q;
139 }
140
141 void* greedy_realloc_append(
142 void **p,
143 size_t *n_p,
144 const void *from,
145 size_t n_from,
146 size_t size) {
147
148 uint8_t *q;
149
150 assert(p);
151 assert(n_p);
152 assert(from || n_from == 0);
153
154 if (n_from > SIZE_MAX - *n_p)
155 return NULL;
156
157 q = greedy_realloc(p, *n_p + n_from, size);
158 if (!q)
159 return NULL;
160
161 memcpy_safe(q + *n_p * size, from, n_from * size);
162
163 *n_p += n_from;
164
165 return q;
166 }
167
168 void free_many(void **p, size_t n) {
169 assert(p || n == 0);
170
171 FOREACH_ARRAY(i, p, n)
172 *i = mfree(*i);
173 }