1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */ 2 /* Copyright (c) 2026 Imagination Technologies Ltd. */ 3 4 #ifndef PVR_CHECK_H 5 #define PVR_CHECK_H 6 7 #include <linux/build_bug.h> 8 #include <linux/overflow.h> 9 #include <linux/stddef.h> 10 11 #define OFFSET_CHECK(type, member, offset) \ 12 static_assert(offsetof(type, member) == (offset), \ 13 "offsetof(" #type ", " #member ") incorrect") 14 15 #define SIZE_CHECK(type, size) \ 16 static_assert(sizeof(type) == (size), #type " is incorrect size") 17 18 #define ALIGN_CHECK(type, align) \ 19 static_assert(__alignof__(type) <= (align), #type " has incorrect alignment") 20 21 /* 22 * Where the last member of a struct is a flexible array member, using 23 * SIZE_CHECK() is pointless. If the structure is not already padded to 24 * alignment without the flexible array member, sizeof() will not match the 25 * offset of the flexible array member and the "correct" sizeof() value is 26 * completely meaningless. 27 * 28 * In those instances, use FLEX_ARRAY_CHECK() instead to assert that the final 29 * field is a flexible array member and that it behaves as expected. 30 */ 31 #define FLEX_ARRAY_CHECK(type, member) \ 32 static_assert(flex_array_size((type *)NULL, member, 1) == \ 33 sizeof_field(type, member[0]), \ 34 #type "->" #member " is incorrect size") 35 36 #endif /* PVR_CHECK_H */ 37