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