]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/alloc-util.h
string-util: add strlen_ptr() helper
[thirdparty/systemd.git] / src / basic / alloc-util.h
CommitLineData
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
41static inline void *mfree(void *memory) {
42 free(memory);
43 return NULL;
44}
45
3b319885
ZJS
46#define free_and_replace(a, b) \
47 ({ \
48 free(a); \
49 (a) = (b); \
50 (b) = NULL; \
51 0; \
52 })
53
b5efdb8a
LP
54void* memdup(const void *p, size_t l) _alloc_(2);
55
56static inline void freep(void *p) {
57 free(*(void**) p);
58}
59
60#define _cleanup_free_ _cleanup_(freep)
61
dbacacaa
AK
62static 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))
b5efdb8a
LP
68 return NULL;
69
dbacacaa 70 return malloc(size * need);
b5efdb8a
LP
71}
72
dbacacaa
AK
73_alloc_(2, 3) static inline void *realloc_multiply(void *p, size_t size, size_t need) {
74 if (size_multiply_overflow(size, need))
b5efdb8a
LP
75 return NULL;
76
dbacacaa 77 return realloc(p, size * need);
b5efdb8a
LP
78}
79
dbacacaa
AK
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))
b5efdb8a
LP
82 return NULL;
83
dbacacaa 84 return memdup(p, size * need);
b5efdb8a
LP
85}
86
87void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
88void* 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 })