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