chiark / gitweb /
terminal-util: helper macro for highlighting functions
[elogind.git] / src / basic / alloc-util.h
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>
23 #include <stddef.h>
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 #define free_and_replace(a, b)                  \
47         ({                                      \
48                 free(a);                        \
49                 (a) = (b);                      \
50                 (b) = NULL;                     \
51                 0;                              \
52         })
53
54 void* memdup(const void *p, size_t l) _alloc_(2);
55
56 static inline void freep(void *p) {
57         free(*(void**) p);
58 }
59
60 #define _cleanup_free_ _cleanup_(freep)
61
62 static inline bool size_multiply_overflow(size_t size, size_t need) {
63         return _unlikely_(need != 0 && size > (SIZE_MAX / need));
64 }
65
66 _malloc_  _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
67         if (size_multiply_overflow(size, need))
68                 return NULL;
69
70         return malloc(size * need);
71 }
72
73 _alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t size, size_t need) {
74         if (size_multiply_overflow(size, need))
75                 return NULL;
76
77         return realloc(p, size * need);
78 }
79
80 _alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
81         if (size_multiply_overflow(size, need))
82                 return NULL;
83
84         return memdup(p, size * need);
85 }
86
87 void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
88 void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
89
90 #define GREEDY_REALLOC(array, allocated, need)                          \
91         greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
92
93 #define GREEDY_REALLOC0(array, allocated, need)                         \
94         greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
95
96 #define alloca0(n)                                      \
97         ({                                              \
98                 char *_new_;                            \
99                 size_t _len_ = n;                       \
100                 _new_ = alloca(_len_);                  \
101                 (void *) memset(_new_, 0, _len_);       \
102         })
103
104 /* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
105 #define alloca_align(size, align)                                       \
106         ({                                                              \
107                 void *_ptr_;                                            \
108                 size_t _mask_ = (align) - 1;                            \
109                 _ptr_ = alloca((size) + _mask_);                        \
110                 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_);         \
111         })
112
113 #define alloca0_align(size, align)                                      \
114         ({                                                              \
115                 void *_new_;                                            \
116                 size_t _size_ = (size);                                 \
117                 _new_ = alloca_align(_size_, (align));                  \
118                 (void*)memset(_new_, 0, _size_);                        \
119         })