xref: /freebsd-src/sys/compat/linuxkpi/common/include/linux/overflow.h (revision 3208d4ad2b8320a24af4a8293f7fd3ef9c35caa6)
1*3208d4adSVladimir Kondratyev /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2*3208d4adSVladimir Kondratyev #ifndef _LINUXKPI_LINUX_OVERFLOW_H
3*3208d4adSVladimir Kondratyev #define _LINUXKPI_LINUX_OVERFLOW_H
4*3208d4adSVladimir Kondratyev 
5*3208d4adSVladimir Kondratyev #include <linux/compiler.h>
6*3208d4adSVladimir Kondratyev #include <linux/limits.h>
7*3208d4adSVladimir Kondratyev #ifdef __linux__
8*3208d4adSVladimir Kondratyev #include <linux/const.h>
9*3208d4adSVladimir Kondratyev #endif
10*3208d4adSVladimir Kondratyev 
11*3208d4adSVladimir Kondratyev /*
12*3208d4adSVladimir Kondratyev  * We need to compute the minimum and maximum values representable in a given
13*3208d4adSVladimir Kondratyev  * type. These macros may also be useful elsewhere. It would seem more obvious
14*3208d4adSVladimir Kondratyev  * to do something like:
158287045dSEmmanuel Vadot  *
16*3208d4adSVladimir Kondratyev  * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
17*3208d4adSVladimir Kondratyev  * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
188287045dSEmmanuel Vadot  *
19*3208d4adSVladimir Kondratyev  * Unfortunately, the middle expressions, strictly speaking, have
20*3208d4adSVladimir Kondratyev  * undefined behaviour, and at least some versions of gcc warn about
21*3208d4adSVladimir Kondratyev  * the type_max expression (but not if -fsanitize=undefined is in
22*3208d4adSVladimir Kondratyev  * effect; in that case, the warning is deferred to runtime...).
238287045dSEmmanuel Vadot  *
24*3208d4adSVladimir Kondratyev  * The slightly excessive casting in type_min is to make sure the
25*3208d4adSVladimir Kondratyev  * macros also produce sensible values for the exotic type _Bool. [The
26*3208d4adSVladimir Kondratyev  * overflow checkers only almost work for _Bool, but that's
27*3208d4adSVladimir Kondratyev  * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
28*3208d4adSVladimir Kondratyev  * _Bools. Besides, the gcc builtins don't allow _Bool* as third
29*3208d4adSVladimir Kondratyev  * argument.]
30*3208d4adSVladimir Kondratyev  *
31*3208d4adSVladimir Kondratyev  * Idea stolen from
32*3208d4adSVladimir Kondratyev  * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
33*3208d4adSVladimir Kondratyev  * credit to Christian Biere.
348287045dSEmmanuel Vadot  */
35*3208d4adSVladimir Kondratyev #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
36*3208d4adSVladimir Kondratyev #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
37*3208d4adSVladimir Kondratyev #define type_min(T) ((T)((T)-type_max(T)-(T)1))
388287045dSEmmanuel Vadot 
39*3208d4adSVladimir Kondratyev /*
40*3208d4adSVladimir Kondratyev  * Avoids triggering -Wtype-limits compilation warning,
41*3208d4adSVladimir Kondratyev  * while using unsigned data types to check a < 0.
42*3208d4adSVladimir Kondratyev  */
43*3208d4adSVladimir Kondratyev #define is_non_negative(a) ((a) > 0 || (a) == 0)
44*3208d4adSVladimir Kondratyev #define is_negative(a) (!(is_non_negative(a)))
458287045dSEmmanuel Vadot 
46*3208d4adSVladimir Kondratyev /*
47*3208d4adSVladimir Kondratyev  * Allows for effectively applying __must_check to a macro so we can have
48*3208d4adSVladimir Kondratyev  * both the type-agnostic benefits of the macros while also being able to
49*3208d4adSVladimir Kondratyev  * enforce that the return value is, in fact, checked.
50*3208d4adSVladimir Kondratyev  */
__must_check_overflow(bool overflow)51*3208d4adSVladimir Kondratyev static inline bool __must_check __must_check_overflow(bool overflow)
52d2890eeeSHans Petter Selasky {
53*3208d4adSVladimir Kondratyev 	return unlikely(overflow);
54d2890eeeSHans Petter Selasky }
558287045dSEmmanuel Vadot 
56*3208d4adSVladimir Kondratyev /**
57*3208d4adSVladimir Kondratyev  * check_add_overflow() - Calculate addition with overflow checking
58*3208d4adSVladimir Kondratyev  * @a: first addend
59*3208d4adSVladimir Kondratyev  * @b: second addend
60*3208d4adSVladimir Kondratyev  * @d: pointer to store sum
61*3208d4adSVladimir Kondratyev  *
62*3208d4adSVladimir Kondratyev  * Returns 0 on success.
63*3208d4adSVladimir Kondratyev  *
64*3208d4adSVladimir Kondratyev  * *@d holds the results of the attempted addition, but is not considered
65*3208d4adSVladimir Kondratyev  * "safe for use" on a non-zero return value, which indicates that the
66*3208d4adSVladimir Kondratyev  * sum has overflowed or been truncated.
67*3208d4adSVladimir Kondratyev  */
68*3208d4adSVladimir Kondratyev #define check_add_overflow(a, b, d)	\
69*3208d4adSVladimir Kondratyev 	__must_check_overflow(__builtin_add_overflow(a, b, d))
70*3208d4adSVladimir Kondratyev 
71*3208d4adSVladimir Kondratyev /**
72*3208d4adSVladimir Kondratyev  * check_sub_overflow() - Calculate subtraction with overflow checking
73*3208d4adSVladimir Kondratyev  * @a: minuend; value to subtract from
74*3208d4adSVladimir Kondratyev  * @b: subtrahend; value to subtract from @a
75*3208d4adSVladimir Kondratyev  * @d: pointer to store difference
76*3208d4adSVladimir Kondratyev  *
77*3208d4adSVladimir Kondratyev  * Returns 0 on success.
78*3208d4adSVladimir Kondratyev  *
79*3208d4adSVladimir Kondratyev  * *@d holds the results of the attempted subtraction, but is not considered
80*3208d4adSVladimir Kondratyev  * "safe for use" on a non-zero return value, which indicates that the
81*3208d4adSVladimir Kondratyev  * difference has underflowed or been truncated.
82*3208d4adSVladimir Kondratyev  */
83*3208d4adSVladimir Kondratyev #define check_sub_overflow(a, b, d)	\
84*3208d4adSVladimir Kondratyev 	__must_check_overflow(__builtin_sub_overflow(a, b, d))
85*3208d4adSVladimir Kondratyev 
86*3208d4adSVladimir Kondratyev /**
87*3208d4adSVladimir Kondratyev  * check_mul_overflow() - Calculate multiplication with overflow checking
88*3208d4adSVladimir Kondratyev  * @a: first factor
89*3208d4adSVladimir Kondratyev  * @b: second factor
90*3208d4adSVladimir Kondratyev  * @d: pointer to store product
91*3208d4adSVladimir Kondratyev  *
92*3208d4adSVladimir Kondratyev  * Returns 0 on success.
93*3208d4adSVladimir Kondratyev  *
94*3208d4adSVladimir Kondratyev  * *@d holds the results of the attempted multiplication, but is not
95*3208d4adSVladimir Kondratyev  * considered "safe for use" on a non-zero return value, which indicates
96*3208d4adSVladimir Kondratyev  * that the product has overflowed or been truncated.
97*3208d4adSVladimir Kondratyev  */
98*3208d4adSVladimir Kondratyev #define check_mul_overflow(a, b, d)	\
99*3208d4adSVladimir Kondratyev 	__must_check_overflow(__builtin_mul_overflow(a, b, d))
100*3208d4adSVladimir Kondratyev 
101*3208d4adSVladimir Kondratyev /**
102*3208d4adSVladimir Kondratyev  * check_shl_overflow() - Calculate a left-shifted value and check overflow
103*3208d4adSVladimir Kondratyev  * @a: Value to be shifted
104*3208d4adSVladimir Kondratyev  * @s: How many bits left to shift
105*3208d4adSVladimir Kondratyev  * @d: Pointer to where to store the result
106*3208d4adSVladimir Kondratyev  *
107*3208d4adSVladimir Kondratyev  * Computes *@d = (@a << @s)
108*3208d4adSVladimir Kondratyev  *
109*3208d4adSVladimir Kondratyev  * Returns true if '*@d' cannot hold the result or when '@a << @s' doesn't
110*3208d4adSVladimir Kondratyev  * make sense. Example conditions:
111*3208d4adSVladimir Kondratyev  *
112*3208d4adSVladimir Kondratyev  * - '@a << @s' causes bits to be lost when stored in *@d.
113*3208d4adSVladimir Kondratyev  * - '@s' is garbage (e.g. negative) or so large that the result of
114*3208d4adSVladimir Kondratyev  *   '@a << @s' is guaranteed to be 0.
115*3208d4adSVladimir Kondratyev  * - '@a' is negative.
116*3208d4adSVladimir Kondratyev  * - '@a << @s' sets the sign bit, if any, in '*@d'.
117*3208d4adSVladimir Kondratyev  *
118*3208d4adSVladimir Kondratyev  * '*@d' will hold the results of the attempted shift, but is not
119*3208d4adSVladimir Kondratyev  * considered "safe for use" if true is returned.
120*3208d4adSVladimir Kondratyev  */
121*3208d4adSVladimir Kondratyev #define check_shl_overflow(a, s, d) __must_check_overflow(({		\
122*3208d4adSVladimir Kondratyev 	typeof(a) _a = a;						\
123*3208d4adSVladimir Kondratyev 	typeof(s) _s = s;						\
124*3208d4adSVladimir Kondratyev 	typeof(d) _d = d;						\
125*3208d4adSVladimir Kondratyev 	u64 _a_full = _a;						\
126*3208d4adSVladimir Kondratyev 	unsigned int _to_shift =					\
127*3208d4adSVladimir Kondratyev 		is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0;	\
128*3208d4adSVladimir Kondratyev 	*_d = (_a_full << _to_shift);					\
129*3208d4adSVladimir Kondratyev 	(_to_shift != _s || is_negative(*_d) || is_negative(_a) ||	\
130*3208d4adSVladimir Kondratyev 	(*_d >> _to_shift) != _a);					\
131*3208d4adSVladimir Kondratyev }))
132*3208d4adSVladimir Kondratyev 
133*3208d4adSVladimir Kondratyev #define __overflows_type_constexpr(x, T) (			\
134*3208d4adSVladimir Kondratyev 	is_unsigned_type(typeof(x)) ?				\
135*3208d4adSVladimir Kondratyev 		(x) > type_max(typeof(T)) :			\
136*3208d4adSVladimir Kondratyev 	is_unsigned_type(typeof(T)) ?				\
137*3208d4adSVladimir Kondratyev 		(x) < 0 || (x) > type_max(typeof(T)) :		\
138*3208d4adSVladimir Kondratyev 	(x) < type_min(typeof(T)) || (x) > type_max(typeof(T)))
139*3208d4adSVladimir Kondratyev 
140*3208d4adSVladimir Kondratyev #define __overflows_type(x, T)		({	\
141*3208d4adSVladimir Kondratyev 	typeof(T) v = 0;			\
142*3208d4adSVladimir Kondratyev 	check_add_overflow((x), v, &v);		\
143*3208d4adSVladimir Kondratyev })
144*3208d4adSVladimir Kondratyev 
145*3208d4adSVladimir Kondratyev /**
146*3208d4adSVladimir Kondratyev  * overflows_type - helper for checking the overflows between value, variables,
147*3208d4adSVladimir Kondratyev  *		    or data type
148*3208d4adSVladimir Kondratyev  *
149*3208d4adSVladimir Kondratyev  * @n: source constant value or variable to be checked
150*3208d4adSVladimir Kondratyev  * @T: destination variable or data type proposed to store @x
151*3208d4adSVladimir Kondratyev  *
152*3208d4adSVladimir Kondratyev  * Compares the @x expression for whether or not it can safely fit in
153*3208d4adSVladimir Kondratyev  * the storage of the type in @T. @x and @T can have different types.
154*3208d4adSVladimir Kondratyev  * If @x is a constant expression, this will also resolve to a constant
155*3208d4adSVladimir Kondratyev  * expression.
156*3208d4adSVladimir Kondratyev  *
157*3208d4adSVladimir Kondratyev  * Returns: true if overflow can occur, false otherwise.
158*3208d4adSVladimir Kondratyev  */
159*3208d4adSVladimir Kondratyev #define overflows_type(n, T)					\
160*3208d4adSVladimir Kondratyev 	__builtin_choose_expr(__is_constexpr(n),		\
161*3208d4adSVladimir Kondratyev 			      __overflows_type_constexpr(n, T),	\
162*3208d4adSVladimir Kondratyev 			      __overflows_type(n, T))
163*3208d4adSVladimir Kondratyev 
164*3208d4adSVladimir Kondratyev /**
165*3208d4adSVladimir Kondratyev  * castable_to_type - like __same_type(), but also allows for casted literals
166*3208d4adSVladimir Kondratyev  *
167*3208d4adSVladimir Kondratyev  * @n: variable or constant value
168*3208d4adSVladimir Kondratyev  * @T: variable or data type
169*3208d4adSVladimir Kondratyev  *
170*3208d4adSVladimir Kondratyev  * Unlike the __same_type() macro, this allows a constant value as the
171*3208d4adSVladimir Kondratyev  * first argument. If this value would not overflow into an assignment
172*3208d4adSVladimir Kondratyev  * of the second argument's type, it returns true. Otherwise, this falls
173*3208d4adSVladimir Kondratyev  * back to __same_type().
174*3208d4adSVladimir Kondratyev  */
175*3208d4adSVladimir Kondratyev #define castable_to_type(n, T)						\
176*3208d4adSVladimir Kondratyev 	__builtin_choose_expr(__is_constexpr(n),			\
177*3208d4adSVladimir Kondratyev 			      !__overflows_type_constexpr(n, T),	\
178*3208d4adSVladimir Kondratyev 			      __same_type(n, T))
179*3208d4adSVladimir Kondratyev 
180*3208d4adSVladimir Kondratyev /**
181*3208d4adSVladimir Kondratyev  * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
182*3208d4adSVladimir Kondratyev  * @factor1: first factor
183*3208d4adSVladimir Kondratyev  * @factor2: second factor
184*3208d4adSVladimir Kondratyev  *
185*3208d4adSVladimir Kondratyev  * Returns: calculate @factor1 * @factor2, both promoted to size_t,
186*3208d4adSVladimir Kondratyev  * with any overflow causing the return value to be SIZE_MAX. The
187*3208d4adSVladimir Kondratyev  * lvalue must be size_t to avoid implicit type conversion.
188*3208d4adSVladimir Kondratyev  */
size_mul(size_t factor1,size_t factor2)189*3208d4adSVladimir Kondratyev static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
190*3208d4adSVladimir Kondratyev {
191*3208d4adSVladimir Kondratyev 	size_t bytes;
192*3208d4adSVladimir Kondratyev 
193*3208d4adSVladimir Kondratyev 	if (check_mul_overflow(factor1, factor2, &bytes))
194*3208d4adSVladimir Kondratyev 		return SIZE_MAX;
195*3208d4adSVladimir Kondratyev 
196*3208d4adSVladimir Kondratyev 	return bytes;
197*3208d4adSVladimir Kondratyev }
198*3208d4adSVladimir Kondratyev 
199*3208d4adSVladimir Kondratyev /**
200*3208d4adSVladimir Kondratyev  * size_add() - Calculate size_t addition with saturation at SIZE_MAX
201*3208d4adSVladimir Kondratyev  * @addend1: first addend
202*3208d4adSVladimir Kondratyev  * @addend2: second addend
203*3208d4adSVladimir Kondratyev  *
204*3208d4adSVladimir Kondratyev  * Returns: calculate @addend1 + @addend2, both promoted to size_t,
205*3208d4adSVladimir Kondratyev  * with any overflow causing the return value to be SIZE_MAX. The
206*3208d4adSVladimir Kondratyev  * lvalue must be size_t to avoid implicit type conversion.
207*3208d4adSVladimir Kondratyev  */
size_add(size_t addend1,size_t addend2)208*3208d4adSVladimir Kondratyev static inline size_t __must_check size_add(size_t addend1, size_t addend2)
209*3208d4adSVladimir Kondratyev {
210*3208d4adSVladimir Kondratyev 	size_t bytes;
211*3208d4adSVladimir Kondratyev 
212*3208d4adSVladimir Kondratyev 	if (check_add_overflow(addend1, addend2, &bytes))
213*3208d4adSVladimir Kondratyev 		return SIZE_MAX;
214*3208d4adSVladimir Kondratyev 
215*3208d4adSVladimir Kondratyev 	return bytes;
216*3208d4adSVladimir Kondratyev }
217*3208d4adSVladimir Kondratyev 
218*3208d4adSVladimir Kondratyev /**
219*3208d4adSVladimir Kondratyev  * size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX
220*3208d4adSVladimir Kondratyev  * @minuend: value to subtract from
221*3208d4adSVladimir Kondratyev  * @subtrahend: value to subtract from @minuend
222*3208d4adSVladimir Kondratyev  *
223*3208d4adSVladimir Kondratyev  * Returns: calculate @minuend - @subtrahend, both promoted to size_t,
224*3208d4adSVladimir Kondratyev  * with any overflow causing the return value to be SIZE_MAX. For
225*3208d4adSVladimir Kondratyev  * composition with the size_add() and size_mul() helpers, neither
226*3208d4adSVladimir Kondratyev  * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX).
227*3208d4adSVladimir Kondratyev  * The lvalue must be size_t to avoid implicit type conversion.
228*3208d4adSVladimir Kondratyev  */
size_sub(size_t minuend,size_t subtrahend)229*3208d4adSVladimir Kondratyev static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
230*3208d4adSVladimir Kondratyev {
231*3208d4adSVladimir Kondratyev 	size_t bytes;
232*3208d4adSVladimir Kondratyev 
233*3208d4adSVladimir Kondratyev 	if (minuend == SIZE_MAX || subtrahend == SIZE_MAX ||
234*3208d4adSVladimir Kondratyev 	    check_sub_overflow(minuend, subtrahend, &bytes))
235*3208d4adSVladimir Kondratyev 		return SIZE_MAX;
236*3208d4adSVladimir Kondratyev 
237*3208d4adSVladimir Kondratyev 	return bytes;
238*3208d4adSVladimir Kondratyev }
239*3208d4adSVladimir Kondratyev 
240*3208d4adSVladimir Kondratyev /**
241*3208d4adSVladimir Kondratyev  * array_size() - Calculate size of 2-dimensional array.
242*3208d4adSVladimir Kondratyev  * @a: dimension one
243*3208d4adSVladimir Kondratyev  * @b: dimension two
244*3208d4adSVladimir Kondratyev  *
245*3208d4adSVladimir Kondratyev  * Calculates size of 2-dimensional array: @a * @b.
246*3208d4adSVladimir Kondratyev  *
247*3208d4adSVladimir Kondratyev  * Returns: number of bytes needed to represent the array or SIZE_MAX on
248*3208d4adSVladimir Kondratyev  * overflow.
249*3208d4adSVladimir Kondratyev  */
250*3208d4adSVladimir Kondratyev #define array_size(a, b)	size_mul(a, b)
251*3208d4adSVladimir Kondratyev 
252*3208d4adSVladimir Kondratyev /**
253*3208d4adSVladimir Kondratyev  * array3_size() - Calculate size of 3-dimensional array.
254*3208d4adSVladimir Kondratyev  * @a: dimension one
255*3208d4adSVladimir Kondratyev  * @b: dimension two
256*3208d4adSVladimir Kondratyev  * @c: dimension three
257*3208d4adSVladimir Kondratyev  *
258*3208d4adSVladimir Kondratyev  * Calculates size of 3-dimensional array: @a * @b * @c.
259*3208d4adSVladimir Kondratyev  *
260*3208d4adSVladimir Kondratyev  * Returns: number of bytes needed to represent the array or SIZE_MAX on
261*3208d4adSVladimir Kondratyev  * overflow.
262*3208d4adSVladimir Kondratyev  */
263*3208d4adSVladimir Kondratyev #define array3_size(a, b, c)	size_mul(size_mul(a, b), c)
264*3208d4adSVladimir Kondratyev 
265*3208d4adSVladimir Kondratyev /**
266*3208d4adSVladimir Kondratyev  * flex_array_size() - Calculate size of a flexible array member
267*3208d4adSVladimir Kondratyev  *                     within an enclosing structure.
268*3208d4adSVladimir Kondratyev  * @p: Pointer to the structure.
269*3208d4adSVladimir Kondratyev  * @member: Name of the flexible array member.
270*3208d4adSVladimir Kondratyev  * @count: Number of elements in the array.
271*3208d4adSVladimir Kondratyev  *
272*3208d4adSVladimir Kondratyev  * Calculates size of a flexible array of @count number of @member
273*3208d4adSVladimir Kondratyev  * elements, at the end of structure @p.
274*3208d4adSVladimir Kondratyev  *
275*3208d4adSVladimir Kondratyev  * Return: number of bytes needed or SIZE_MAX on overflow.
276*3208d4adSVladimir Kondratyev  */
277*3208d4adSVladimir Kondratyev #define flex_array_size(p, member, count)				\
278*3208d4adSVladimir Kondratyev 	__builtin_choose_expr(__is_constexpr(count),			\
279*3208d4adSVladimir Kondratyev 		(count) * sizeof(*(p)->member) + __must_be_array((p)->member),	\
280*3208d4adSVladimir Kondratyev 		size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))
281*3208d4adSVladimir Kondratyev 
282*3208d4adSVladimir Kondratyev /**
283*3208d4adSVladimir Kondratyev  * struct_size() - Calculate size of structure with trailing flexible array.
284*3208d4adSVladimir Kondratyev  * @p: Pointer to the structure.
285*3208d4adSVladimir Kondratyev  * @member: Name of the array member.
286*3208d4adSVladimir Kondratyev  * @count: Number of elements in the array.
287*3208d4adSVladimir Kondratyev  *
288*3208d4adSVladimir Kondratyev  * Calculates size of memory needed for structure of @p followed by an
289*3208d4adSVladimir Kondratyev  * array of @count number of @member elements.
290*3208d4adSVladimir Kondratyev  *
291*3208d4adSVladimir Kondratyev  * Return: number of bytes needed or SIZE_MAX on overflow.
292*3208d4adSVladimir Kondratyev  */
293*3208d4adSVladimir Kondratyev #define struct_size(p, member, count)					\
294*3208d4adSVladimir Kondratyev 	__builtin_choose_expr(__is_constexpr(count),			\
295*3208d4adSVladimir Kondratyev 		sizeof(*(p)) + flex_array_size(p, member, count),	\
296*3208d4adSVladimir Kondratyev 		size_add(sizeof(*(p)), flex_array_size(p, member, count)))
297*3208d4adSVladimir Kondratyev 
298*3208d4adSVladimir Kondratyev /**
299*3208d4adSVladimir Kondratyev  * struct_size_t() - Calculate size of structure with trailing flexible array
300*3208d4adSVladimir Kondratyev  * @type: structure type name.
301*3208d4adSVladimir Kondratyev  * @member: Name of the array member.
302*3208d4adSVladimir Kondratyev  * @count: Number of elements in the array.
303*3208d4adSVladimir Kondratyev  *
304*3208d4adSVladimir Kondratyev  * Calculates size of memory needed for structure @type followed by an
305*3208d4adSVladimir Kondratyev  * array of @count number of @member elements. Prefer using struct_size()
306*3208d4adSVladimir Kondratyev  * when possible instead, to keep calculations associated with a specific
307*3208d4adSVladimir Kondratyev  * instance variable of type @type.
308*3208d4adSVladimir Kondratyev  *
309*3208d4adSVladimir Kondratyev  * Return: number of bytes needed or SIZE_MAX on overflow.
310*3208d4adSVladimir Kondratyev  */
311*3208d4adSVladimir Kondratyev #define struct_size_t(type, member, count)					\
312*3208d4adSVladimir Kondratyev 	struct_size((type *)NULL, member, count)
313*3208d4adSVladimir Kondratyev 
314*3208d4adSVladimir Kondratyev /**
315*3208d4adSVladimir Kondratyev  * _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
316*3208d4adSVladimir Kondratyev  * Enables caller macro to pass (different) initializer.
317*3208d4adSVladimir Kondratyev  *
318*3208d4adSVladimir Kondratyev  * @type: structure type name, including "struct" keyword.
319*3208d4adSVladimir Kondratyev  * @name: Name for a variable to define.
320*3208d4adSVladimir Kondratyev  * @member: Name of the array member.
321*3208d4adSVladimir Kondratyev  * @count: Number of elements in the array; must be compile-time const.
322*3208d4adSVladimir Kondratyev  * @initializer: initializer expression (could be empty for no init).
323*3208d4adSVladimir Kondratyev  */
324*3208d4adSVladimir Kondratyev #define _DEFINE_FLEX(type, name, member, count, initializer)			\
325*3208d4adSVladimir Kondratyev 	_Static_assert(__builtin_constant_p(count),				\
326*3208d4adSVladimir Kondratyev 		       "onstack flex array members require compile-time const count"); \
327*3208d4adSVladimir Kondratyev 	union {									\
328*3208d4adSVladimir Kondratyev 		u8 bytes[struct_size_t(type, member, count)];			\
329*3208d4adSVladimir Kondratyev 		type obj;							\
330*3208d4adSVladimir Kondratyev 	} name##_u initializer;							\
331*3208d4adSVladimir Kondratyev 	type *name = (type *)&name##_u
332*3208d4adSVladimir Kondratyev 
333*3208d4adSVladimir Kondratyev /**
334*3208d4adSVladimir Kondratyev  * DEFINE_FLEX() - Define an on-stack instance of structure with a trailing
335*3208d4adSVladimir Kondratyev  * flexible array member.
336*3208d4adSVladimir Kondratyev  *
337*3208d4adSVladimir Kondratyev  * @type: structure type name, including "struct" keyword.
338*3208d4adSVladimir Kondratyev  * @name: Name for a variable to define.
339*3208d4adSVladimir Kondratyev  * @member: Name of the array member.
340*3208d4adSVladimir Kondratyev  * @count: Number of elements in the array; must be compile-time const.
341*3208d4adSVladimir Kondratyev  *
342*3208d4adSVladimir Kondratyev  * Define a zeroed, on-stack, instance of @type structure with a trailing
343*3208d4adSVladimir Kondratyev  * flexible array member.
344*3208d4adSVladimir Kondratyev  * Use __struct_size(@name) to get compile-time size of it afterwards.
345*3208d4adSVladimir Kondratyev  */
346*3208d4adSVladimir Kondratyev #define DEFINE_FLEX(type, name, member, count)			\
347*3208d4adSVladimir Kondratyev 	_DEFINE_FLEX(type, name, member, count, = {})
348*3208d4adSVladimir Kondratyev 
349*3208d4adSVladimir Kondratyev #endif /* _LINUXKPI_LINUX_OVERFLOW_H */
350