]>
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 | ||
39 | #define malloc0(n) (calloc(1, (n))) | |
40 | ||
41 | static inline void *mfree(void *memory) { | |
42 | free(memory); | |
43 | return NULL; | |
44 | } | |
45 | ||
46 | void* memdup(const void *p, size_t l) _alloc_(2); | |
47 | ||
48 | static inline void freep(void *p) { | |
49 | free(*(void**) p); | |
50 | } | |
51 | ||
52 | #define _cleanup_free_ _cleanup_(freep) | |
53 | ||
54 | _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) { | |
55 | if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) | |
56 | return NULL; | |
57 | ||
58 | return malloc(a * b); | |
59 | } | |
60 | ||
61 | _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t a, size_t b) { | |
62 | if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) | |
63 | return NULL; | |
64 | ||
65 | return realloc(p, a * b); | |
66 | } | |
67 | ||
68 | _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t a, size_t b) { | |
69 | if (_unlikely_(b != 0 && a > ((size_t) -1) / b)) | |
70 | return NULL; | |
71 | ||
72 | return memdup(p, a * b); | |
73 | } | |
74 | ||
75 | void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size); | |
76 | void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); | |
77 | ||
78 | #define GREEDY_REALLOC(array, allocated, need) \ | |
79 | greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0])) | |
80 | ||
81 | #define GREEDY_REALLOC0(array, allocated, need) \ | |
82 | greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0])) | |
83 | ||
84 | #define alloca0(n) \ | |
85 | ({ \ | |
86 | char *_new_; \ | |
87 | size_t _len_ = n; \ | |
88 | _new_ = alloca(_len_); \ | |
89 | (void *) memset(_new_, 0, _len_); \ | |
90 | }) | |
91 | ||
92 | /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */ | |
93 | #define alloca_align(size, align) \ | |
94 | ({ \ | |
95 | void *_ptr_; \ | |
96 | size_t _mask_ = (align) - 1; \ | |
97 | _ptr_ = alloca((size) + _mask_); \ | |
98 | (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \ | |
99 | }) | |
100 | ||
101 | #define alloca0_align(size, align) \ | |
102 | ({ \ | |
103 | void *_new_; \ | |
104 | size_t _size_ = (size); \ | |
105 | _new_ = alloca_align(_size_, (align)); \ | |
106 | (void*)memset(_new_, 0, _size_); \ | |
107 | }) |