|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Igalia S.L. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 17 | + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 | + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#ifndef wpe_alloc_private_h |
| 28 | +#define wpe_alloc_private_h |
| 29 | + |
| 30 | +#include <stdlib.h> |
| 31 | + |
| 32 | +#if defined(__has_attribute) && __has_attribute(noreturn) |
| 33 | +#define WPE_NORETURN __attribute__((noreturn)) |
| 34 | +#else |
| 35 | +#define WPE_NORETURN |
| 36 | +#endif /* __has_attribute(noreturn) */ |
| 37 | + |
| 38 | +#if defined(__has_attribute) && __has_attribute(alloc_size) |
| 39 | +#define WPE_ALLOCSIZE(...) __attribute__((alloc_size(__VA_ARGS__))) |
| 40 | +#else |
| 41 | +#define WPE_ALLOCSIZE(...) |
| 42 | +#endif /* __has_attribute(alloc_size) */ |
| 43 | + |
| 44 | +#if defined(__has_attribute) && __has_attribute(malloc) |
| 45 | +#define WPE_MALLOC __attribute__((malloc)) |
| 46 | +#else |
| 47 | +#define WPE_MALLOC |
| 48 | +#endif /* __has_attribute(malloc) */ |
| 49 | + |
| 50 | +#if defined(__has_attribute) && __has_attribute(warn_unused_result) |
| 51 | +#define WPE_USERESULT __attribute__((warn_unused_result)) |
| 52 | +#else |
| 53 | +#define WPE_USERESULT |
| 54 | +#endif /* __has_attribute(warn_unused_result) */ |
| 55 | + |
| 56 | +#ifdef __cplusplus |
| 57 | +extern "C" |
| 58 | +#endif /* __cplusplus */ |
| 59 | + WPE_NORETURN void |
| 60 | + wpe_alloc_fail(const char* file, unsigned line, size_t amount); |
| 61 | + |
| 62 | +WPE_ALLOCSIZE(1, 2) |
| 63 | +WPE_MALLOC WPE_USERESULT static inline void* |
| 64 | +wpe_calloc_impl(size_t nmemb, size_t size, const char* file, unsigned line) |
| 65 | +{ |
| 66 | + void* p = calloc(nmemb, size); |
| 67 | + if (p) |
| 68 | + return p; |
| 69 | + |
| 70 | + wpe_alloc_fail(file, line, nmemb * size); |
| 71 | +} |
| 72 | + |
| 73 | +WPE_ALLOCSIZE(2) |
| 74 | +WPE_USERESULT static inline void* |
| 75 | +wpe_realloc_impl(void* p, size_t size, const char* file, unsigned line) |
| 76 | +{ |
| 77 | + if ((p = realloc(p, size))) |
| 78 | + return p; |
| 79 | + |
| 80 | + wpe_alloc_fail(file, line, size); |
| 81 | +} |
| 82 | + |
| 83 | +WPE_ALLOCSIZE(1) |
| 84 | +WPE_MALLOC WPE_USERESULT static inline void* |
| 85 | +wpe_malloc_impl(size_t size, const char* file, unsigned line) |
| 86 | +{ |
| 87 | + void* p = malloc(size); |
| 88 | + if (p) |
| 89 | + return p; |
| 90 | + |
| 91 | + wpe_alloc_fail(file, line, size); |
| 92 | +} |
| 93 | + |
| 94 | +#define wpe_calloc(nmemb, size) wpe_calloc_impl((nmemb), (size), __FILE__, __LINE__) |
| 95 | +#define wpe_realloc(p, size) wpe_realloc_impl((p), (size), __FILE__, __LINE__) |
| 96 | +#define wpe_malloc(size) wpe_malloc_impl((size), __FILE__, __LINE__) |
| 97 | +#define wpe_free free |
| 98 | + |
| 99 | +/* Prevent usage of unwrapped functions from this point onwards. */ |
| 100 | +#pragma GCC poison malloc |
| 101 | +#pragma GCC poison realloc |
| 102 | +#pragma GCC poison calloc |
| 103 | +#pragma GCC poison free |
| 104 | + |
| 105 | +#undef WPE_ALLOCSIZE |
| 106 | +#undef WPE_MALLOC |
| 107 | +#undef WPE_NORETURN |
| 108 | +#undef WPE_USERESULT |
| 109 | + |
| 110 | +#endif /* wpe_alloc_private_h */ |
0 commit comments