]>
Commit | Line | Data |
---|---|---|
b5efdb8a LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
11c3a366 TA |
20 | #include <stdint.h> |
21 | #include <string.h> | |
22 | ||
b5efdb8a | 23 | #include "alloc-util.h" |
11c3a366 | 24 | #include "macro.h" |
b5efdb8a LP |
25 | #include "util.h" |
26 | ||
27 | void* memdup(const void *p, size_t l) { | |
c165d97d | 28 | void *ret; |
b5efdb8a | 29 | |
c165d97d LP |
30 | assert(l == 0 || p); |
31 | ||
32 | ret = malloc(l); | |
33 | if (!ret) | |
34 | return NULL; | |
35 | ||
36 | memcpy(ret, p, l); | |
37 | return ret; | |
38 | } | |
39 | ||
40 | void* memdup_suffix0(const void*p, size_t l) { | |
41 | void *ret; | |
42 | ||
43 | assert(l == 0 || p); | |
44 | ||
45 | /* The same as memdup() but place a safety NUL byte after the allocated memory */ | |
b5efdb8a | 46 | |
c165d97d LP |
47 | ret = malloc(l + 1); |
48 | if (!ret) | |
b5efdb8a LP |
49 | return NULL; |
50 | ||
c165d97d LP |
51 | *((uint8_t*) mempcpy(ret, p, l)) = 0; |
52 | return ret; | |
b5efdb8a LP |
53 | } |
54 | ||
55 | void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) { | |
56 | size_t a, newalloc; | |
57 | void *q; | |
58 | ||
59 | assert(p); | |
60 | assert(allocated); | |
61 | ||
62 | if (*allocated >= need) | |
63 | return *p; | |
64 | ||
65 | newalloc = MAX(need * 2, 64u / size); | |
66 | a = newalloc * size; | |
67 | ||
68 | /* check for overflows */ | |
69 | if (a < size * need) | |
70 | return NULL; | |
71 | ||
72 | q = realloc(*p, a); | |
73 | if (!q) | |
74 | return NULL; | |
75 | ||
76 | *p = q; | |
77 | *allocated = newalloc; | |
78 | return q; | |
79 | } | |
80 | ||
81 | void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) { | |
82 | size_t prev; | |
83 | uint8_t *q; | |
84 | ||
85 | assert(p); | |
86 | assert(allocated); | |
87 | ||
88 | prev = *allocated; | |
89 | ||
90 | q = greedy_realloc(p, allocated, need, size); | |
91 | if (!q) | |
92 | return NULL; | |
93 | ||
94 | if (*allocated > prev) | |
95 | memzero(q + prev * size, (*allocated - prev) * size); | |
96 | ||
97 | return q; | |
98 | } |