]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/alloc-util.c
meson: make user $PATH configurable
[thirdparty/systemd.git] / src / basic / alloc-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <malloc.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 #include "alloc-util.h"
8 #include "macro.h"
9 #include "memory-util.h"
10
11 void* memdup(const void *p, size_t l) {
12 void *ret;
13
14 assert(l == 0 || p);
15
16 ret = malloc(l ?: 1);
17 if (!ret)
18 return NULL;
19
20 memcpy(ret, p, l);
21 return ret;
22 }
23
24 void* memdup_suffix0(const void *p, size_t l) {
25 void *ret;
26
27 assert(l == 0 || p);
28
29 /* The same as memdup() but place a safety NUL byte after the allocated memory */
30
31 if (_unlikely_(l == SIZE_MAX)) /* prevent overflow */
32 return NULL;
33
34 ret = malloc(l + 1);
35 if (!ret)
36 return NULL;
37
38 *((uint8_t*) mempcpy(ret, p, l)) = 0;
39 return ret;
40 }
41
42 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) {
43 size_t a, newalloc;
44 void *q;
45
46 assert(p);
47 assert(allocated);
48
49 if (*allocated >= need)
50 return *p;
51
52 if (_unlikely_(need > SIZE_MAX/2)) /* Overflow check */
53 return NULL;
54
55 newalloc = need * 2;
56 if (size_multiply_overflow(newalloc, size))
57 return NULL;
58
59 a = newalloc * size;
60 if (a < 64) /* Allocate at least 64 bytes */
61 a = 64;
62
63 q = realloc(*p, a);
64 if (!q)
65 return NULL;
66
67 if (size > 0) {
68 size_t bn;
69
70 /* Adjust for the 64 byte minimum */
71 newalloc = a / size;
72
73 bn = malloc_usable_size(q) / size;
74 if (bn > newalloc) {
75 void *qq;
76
77 /* The actual size allocated is larger than what we asked for. Let's call realloc() again to
78 * take possession of the extra space. This should be cheap, since libc doesn't have to move
79 * the memory for this. */
80
81 qq = reallocarray(q, bn, size);
82 if (_likely_(qq)) {
83 *p = qq;
84 *allocated = bn;
85 return qq;
86 }
87 }
88 }
89
90 *p = q;
91 *allocated = newalloc;
92 return q;
93 }
94
95 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) {
96 size_t prev;
97 uint8_t *q;
98
99 assert(p);
100 assert(allocated);
101
102 prev = *allocated;
103
104 q = greedy_realloc(p, allocated, need, size);
105 if (!q)
106 return NULL;
107
108 if (*allocated > prev)
109 memzero(q + prev * size, (*allocated - prev) * size);
110
111 return q;
112 }