1 /*- 2 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org> 3 * Copyright (c) 2018 Johannes Lundberg <johalun0@gmail.com> 4 * Copyright (c) 2021 The FreeBSD Foundation 5 * Copyright (c) 2021 Vladimir Kondratyev <wulf@FreeBSD.org> 6 * Copyright (c) 2023 Serenity Cyber Security, LLC 7 * 8 * Portions of this software were developed by Bjoern A. Zeeb 9 * under sponsorship from the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice unmodified, this list of conditions, and the following 16 * disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef _LINUXKPI_LINUX_BUILD_BUG_H_ 34 #define _LINUXKPI_LINUX_BUILD_BUG_H_ 35 36 #include <sys/param.h> 37 38 #include <linux/compiler.h> 39 40 /* 41 * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not 42 * seem to work. Use old-schoold-ish CTASSERT from before commit 43 * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not 44 * end up with an unused typedef or variable. The compiler should optimise 45 * it away entirely. 46 */ 47 #define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__) 48 #define _O__CTASSERT(x, y) _O___CTASSERT(x, y) 49 #define _O___CTASSERT(x, y) while (0) { \ 50 typedef char __assert_line_ ## y[(x) ? 1 : -1]; \ 51 __assert_line_ ## y _x __unused; \ 52 _x[0] = '\0'; \ 53 } 54 55 #define BUILD_BUG() do { CTASSERT(0); } while (0) 56 #define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0) 57 #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x) 58 #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x)) 59 #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); } 60 #define BUILD_BUG_ON_ZERO(x) ((int)sizeof(struct { int:-((x) != 0); })) 61 62 #define static_assert(x, ...) __static_assert(x, ##__VA_ARGS__, #x) 63 #define __static_assert(x, msg, ...) _Static_assert(x, msg) 64 65 #endif 66