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