Path: blob/main/sys/compat/linuxkpi/common/include/linux/build_bug.h
39604 views
/*-1* Copyright (c) 2017 Mark Johnston <[email protected]>2* Copyright (c) 2018 Johannes Lundberg <[email protected]>3* Copyright (c) 2021 The FreeBSD Foundation4* Copyright (c) 2021 Vladimir Kondratyev <[email protected]>5* Copyright (c) 2023 Serenity Cyber Security, LLC6*7* Portions of this software were developed by Bjoern A. Zeeb8* under sponsorship from the FreeBSD Foundation.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice unmodified, this list of conditions, and the following15* disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19*20* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES22* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.23* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,24* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT25* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,26* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY27* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF29* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#ifndef _LINUXKPI_LINUX_BUILD_BUG_H_33#define _LINUXKPI_LINUX_BUILD_BUG_H_3435#include <sys/param.h>3637#include <linux/compiler.h>3839/*40* BUILD_BUG_ON() can happen inside functions where _Static_assert() does not41* seem to work. Use old-schoold-ish CTASSERT from before commit42* a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not43* end up with an unused typedef or variable. The compiler should optimise44* it away entirely.45*/46#define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__)47#define _O__CTASSERT(x, y) _O___CTASSERT(x, y)48#define _O___CTASSERT(x, y) while (0) { \49typedef char __assert_line_ ## y[(x) ? 1 : -1]; \50__assert_line_ ## y _x __unused; \51_x[0] = '\0'; \52}5354#define BUILD_BUG() do { CTASSERT(0); } while (0)55#define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0)56#define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x)57#define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x))58#define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); }59#define BUILD_BUG_ON_ZERO(x) ((int)sizeof(struct { int:-((x) != 0); }))6061#define static_assert(x, ...) __static_assert(x, ##__VA_ARGS__, #x)62#define __static_assert(x, msg, ...) _Static_assert(x, msg)6364#endif656667