]>
Commit | Line | Data |
---|---|---|
b5efdb8a LP |
1 | #pragma once |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <alloca.h> | |
11c3a366 | 23 | #include <stddef.h> |
b5efdb8a LP |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
26 | ||
27 | #include "macro.h" | |
28 | ||
29 | #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n))) | |
30 | ||
31 | #define new0(t, n) ((t*) calloc((n), sizeof(t))) | |
32 | ||
33 | #define newa(t, n) ((t*) alloca(sizeof(t)*(n))) | |
34 | ||
35 | #define newa0(t, n) ((t*) alloca0(sizeof(t)*(n))) | |
36 | ||
37 | #define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n))) | |
38 | ||
c165d97d LP |
39 | #define newdup_suffix0(t, p, n) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) |
40 | ||
b5efdb8a LP |
41 | #define malloc0(n) (calloc(1, (n))) |
42 | ||
43 | static inline void *mfree(void *memory) { | |
44 | free(memory); | |
45 | return NULL; | |
46 | } | |
47 | ||
3b319885 ZJS |
48 | #define free_and_replace(a, b) \ |
49 | ({ \ | |
50 | free(a); \ | |
51 | (a) = (b); \ | |
52 | (b) = NULL; \ | |
53 | 0; \ | |
54 | }) | |
55 | ||
b5efdb8a | 56 | void* memdup(const void *p, size_t l) _alloc_(2); |
c165d97d | 57 | void* memdup_suffix0(const void*p, size_t l) _alloc_(2); |
b5efdb8a LP |
58 | |
59 | static inline void freep(void *p) { | |
60 | free(*(void**) p); | |
61 | } | |
62 | ||
63 | #define _cleanup_free_ _cleanup_(freep) | |
64 | ||
dbacacaa AK |
65 | static inline bool size_multiply_overflow(size_t size, size_t need) { |
66 | return _unlikely_(need != 0 && size > (SIZE_MAX / need)); | |
67 | } | |
68 | ||
69 | _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) { | |
70 | if (size_multiply_overflow(size, need)) | |
b5efdb8a LP |
71 | return NULL; |
72 | ||
dbacacaa | 73 | return malloc(size * need); |
b5efdb8a LP |
74 | } |
75 | ||
dbacacaa AK |
76 | _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t size, size_t need) { |
77 | if (size_multiply_overflow(size, need)) | |
b5efdb8a LP |
78 | return NULL; |
79 | ||
dbacacaa | 80 | return realloc(p, size * need); |
b5efdb8a LP |
81 | } |
82 | ||
dbacacaa AK |
83 | _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) { |
84 | if (size_multiply_overflow(size, need)) | |
b5efdb8a LP |
85 | return NULL; |
86 | ||
dbacacaa | 87 | return memdup(p, size * need); |
b5efdb8a LP |
88 | } |
89 | ||
c165d97d LP |
90 | _alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) { |
91 | if (size_multiply_overflow(size, need)) | |
92 | return NULL; | |
93 | ||
94 | return memdup_suffix0(p, size * need); | |
95 | } | |
96 | ||
b5efdb8a LP |
97 | void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size); |
98 | void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); | |
99 | ||
100 | #define GREEDY_REALLOC(array, allocated, need) \ | |
101 | greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0])) | |
102 | ||
103 | #define GREEDY_REALLOC0(array, allocated, need) \ | |
104 | greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0])) | |
105 | ||
106 | #define alloca0(n) \ | |
107 | ({ \ | |
108 | char *_new_; \ | |
109 | size_t _len_ = n; \ | |
110 | _new_ = alloca(_len_); \ | |
111 | (void *) memset(_new_, 0, _len_); \ | |
112 | }) | |
113 | ||
114 | /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */ | |
115 | #define alloca_align(size, align) \ | |
116 | ({ \ | |
117 | void *_ptr_; \ | |
118 | size_t _mask_ = (align) - 1; \ | |
119 | _ptr_ = alloca((size) + _mask_); \ | |
120 | (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ | |
121 | }) | |
122 | ||
123 | #define alloca0_align(size, align) \ | |
124 | ({ \ | |
125 | void *_new_; \ | |
126 | size_t _size_ = (size); \ | |
127 | _new_ = alloca_align(_size_, (align)); \ | |
128 | (void*)memset(_new_, 0, _size_); \ | |
129 | }) |