Zephyr Project API  3.3.0
A Scalable Open Source RTOS
util_internal.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 * Copyright (c) 2020, Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
15#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_
16#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_
17
18#include "util_loops.h"
19
20/* IS_ENABLED() helpers */
21
22/* This is called from IS_ENABLED(), and sticks on a "_XXXX" prefix,
23 * it will now be "_XXXX1" if config_macro is "1", or just "_XXXX" if it's
24 * undefined.
25 * ENABLED: Z_IS_ENABLED2(_XXXX1)
26 * DISABLED Z_IS_ENABLED2(_XXXX)
27 */
28#define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro)
29
30/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string
31 * with a trailing comma), so it has the effect of making this a
32 * two-argument tuple to the preprocessor only in the case where the
33 * value is defined to "1"
34 * ENABLED: _YYYY, <--- note comma!
35 * DISABLED: _XXXX
36 */
37#define _XXXX1 _YYYY,
38
39/* Then we append an extra argument to fool the gcc preprocessor into
40 * accepting it as a varargs macro.
41 * arg1 arg2 arg3
42 * ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0)
43 * DISABLED Z_IS_ENABLED3(_XXXX 1, 0)
44 */
45#define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args 1, 0)
46
47/* And our second argument is thus now cooked to be 1 in the case
48 * where the value is defined to 1, and 0 if not:
49 */
50#define Z_IS_ENABLED3(ignore_this, val, ...) val
51
52/* Implementation of IS_EQ(). Returns 1 if _0 and _1 are the same integer from
53 * 0 to 255, 0 otherwise.
54 */
55#define Z_IS_EQ(_0, _1) Z_HAS_COMMA(Z_CAT4(Z_IS_, _0, _EQ_, _1)())
56
57/* Used internally by COND_CODE_1 and COND_CODE_0. */
58#define Z_COND_CODE_1(_flag, _if_1_code, _else_code) \
59 __COND_CODE(_XXXX##_flag, _if_1_code, _else_code)
60#define Z_COND_CODE_0(_flag, _if_0_code, _else_code) \
61 __COND_CODE(_ZZZZ##_flag, _if_0_code, _else_code)
62#define _ZZZZ0 _YYYY,
63#define __COND_CODE(one_or_two_args, _if_code, _else_code) \
64 __GET_ARG2_DEBRACKET(one_or_two_args _if_code, _else_code)
65
66/* Gets second argument and removes brackets around that argument. It
67 * is expected that the parameter is provided in brackets/parentheses.
68 */
69#define __GET_ARG2_DEBRACKET(ignore_this, val, ...) __DEBRACKET val
70
71/* Used to remove brackets from around a single argument. */
72#define __DEBRACKET(...) __VA_ARGS__
73
74/* Used by IS_EMPTY() */
75/* reference: https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/ */
76#define Z_HAS_COMMA(...) \
77 NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, \
78 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
81#define Z_TRIGGER_PARENTHESIS_(...) ,
82#define Z_IS_EMPTY_(...) \
83 Z_IS_EMPTY__( \
84 Z_HAS_COMMA(__VA_ARGS__), \
85 Z_HAS_COMMA(Z_TRIGGER_PARENTHESIS_ __VA_ARGS__), \
86 Z_HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
87 Z_HAS_COMMA(Z_TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)))
88#define Z_CAT4(_0, _1, _2, _3) _0 ## _1 ## _2 ## _3
89#define Z_CAT5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
90#define Z_IS_EMPTY__(_0, _1, _2, _3) \
91 Z_HAS_COMMA(Z_CAT5(Z_IS_EMPTY_CASE_, _0, _1, _2, _3))
92#define Z_IS_EMPTY_CASE_0001 ,
93
94/* Used by LIST_DROP_EMPTY() */
95/* Adding ',' after each element would add empty element at the end of
96 * list, which is hard to remove, so instead precede each element with ',',
97 * this way first element is empty, and this one is easy to drop.
98 */
99#define Z_LIST_ADD_ELEM(e) EMPTY, e
100#define Z_LIST_DROP_FIRST(...) GET_ARGS_LESS_N(1, __VA_ARGS__)
101#define Z_LIST_NO_EMPTIES(e) \
102 COND_CODE_1(IS_EMPTY(e), (), (Z_LIST_ADD_ELEM(e)))
103
104#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__)
105#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
106#define UTIL_CHECK_N(x, n, ...) n
107#define UTIL_CHECK(...) UTIL_CHECK_N(__VA_ARGS__, 0,)
108#define UTIL_NOT(x) UTIL_CHECK(UTIL_PRIMITIVE_CAT(UTIL_NOT_, x))
109#define UTIL_NOT_0 ~, 1,
110#define UTIL_COMPL(b) UTIL_PRIMITIVE_CAT(UTIL_COMPL_, b)
111#define UTIL_COMPL_0 1
112#define UTIL_COMPL_1 0
113#define UTIL_BOOL(x) UTIL_COMPL(UTIL_NOT(x))
114
115#define UTIL_EVAL(...) __VA_ARGS__
116#define UTIL_EXPAND(...) __VA_ARGS__
117#define UTIL_REPEAT(...) UTIL_LISTIFY(__VA_ARGS__)
118
119/* Implementation details for NUM_VA_ARGS_LESS_1 */
120#define NUM_VA_ARGS_LESS_1_IMPL( \
121 _ignored, \
122 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
123 _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
124 _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
125 _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
126 _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
127 _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
128 _61, _62, N, ...) N
129
130/* Used by MACRO_MAP_CAT */
131#define MACRO_MAP_CAT_(...) \
132 /* To make sure it works also for 2 arguments in total */ \
133 MACRO_MAP_CAT_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), __VA_ARGS__)
134#define MACRO_MAP_CAT_N_(N, ...) UTIL_CAT(MACRO_MC_, N)(__VA_ARGS__,)
135#define MACRO_MC_0(...)
136#define MACRO_MC_1(m, a, ...) m(a)
137#define MACRO_MC_2(m, a, ...) UTIL_CAT(m(a), MACRO_MC_1(m, __VA_ARGS__,))
138#define MACRO_MC_3(m, a, ...) UTIL_CAT(m(a), MACRO_MC_2(m, __VA_ARGS__,))
139#define MACRO_MC_4(m, a, ...) UTIL_CAT(m(a), MACRO_MC_3(m, __VA_ARGS__,))
140#define MACRO_MC_5(m, a, ...) UTIL_CAT(m(a), MACRO_MC_4(m, __VA_ARGS__,))
141#define MACRO_MC_6(m, a, ...) UTIL_CAT(m(a), MACRO_MC_5(m, __VA_ARGS__,))
142#define MACRO_MC_7(m, a, ...) UTIL_CAT(m(a), MACRO_MC_6(m, __VA_ARGS__,))
143#define MACRO_MC_8(m, a, ...) UTIL_CAT(m(a), MACRO_MC_7(m, __VA_ARGS__,))
144#define MACRO_MC_9(m, a, ...) UTIL_CAT(m(a), MACRO_MC_8(m, __VA_ARGS__,))
145#define MACRO_MC_10(m, a, ...) UTIL_CAT(m(a), MACRO_MC_9(m, __VA_ARGS__,))
146#define MACRO_MC_11(m, a, ...) UTIL_CAT(m(a), MACRO_MC_10(m, __VA_ARGS__,))
147#define MACRO_MC_12(m, a, ...) UTIL_CAT(m(a), MACRO_MC_11(m, __VA_ARGS__,))
148#define MACRO_MC_13(m, a, ...) UTIL_CAT(m(a), MACRO_MC_12(m, __VA_ARGS__,))
149#define MACRO_MC_14(m, a, ...) UTIL_CAT(m(a), MACRO_MC_13(m, __VA_ARGS__,))
150#define MACRO_MC_15(m, a, ...) UTIL_CAT(m(a), MACRO_MC_14(m, __VA_ARGS__,))
151
152/* Used by Z_IS_EQ */
153#define Z_IS_0_EQ_0(...) \,
154#define Z_IS_1_EQ_1(...) \,
155#define Z_IS_2_EQ_2(...) \,
156#define Z_IS_3_EQ_3(...) \,
157#define Z_IS_4_EQ_4(...) \,
158#define Z_IS_5_EQ_5(...) \,
159#define Z_IS_6_EQ_6(...) \,
160#define Z_IS_7_EQ_7(...) \,
161#define Z_IS_8_EQ_8(...) \,
162#define Z_IS_9_EQ_9(...) \,
163#define Z_IS_10_EQ_10(...) \,
164#define Z_IS_11_EQ_11(...) \,
165#define Z_IS_12_EQ_12(...) \,
166#define Z_IS_13_EQ_13(...) \,
167#define Z_IS_14_EQ_14(...) \,
168#define Z_IS_15_EQ_15(...) \,
169#define Z_IS_16_EQ_16(...) \,
170#define Z_IS_17_EQ_17(...) \,
171#define Z_IS_18_EQ_18(...) \,
172#define Z_IS_19_EQ_19(...) \,
173#define Z_IS_20_EQ_20(...) \,
174#define Z_IS_21_EQ_21(...) \,
175#define Z_IS_22_EQ_22(...) \,
176#define Z_IS_23_EQ_23(...) \,
177#define Z_IS_24_EQ_24(...) \,
178#define Z_IS_25_EQ_25(...) \,
179#define Z_IS_26_EQ_26(...) \,
180#define Z_IS_27_EQ_27(...) \,
181#define Z_IS_28_EQ_28(...) \,
182#define Z_IS_29_EQ_29(...) \,
183#define Z_IS_30_EQ_30(...) \,
184#define Z_IS_31_EQ_31(...) \,
185#define Z_IS_32_EQ_32(...) \,
186#define Z_IS_33_EQ_33(...) \,
187#define Z_IS_34_EQ_34(...) \,
188#define Z_IS_35_EQ_35(...) \,
189#define Z_IS_36_EQ_36(...) \,
190#define Z_IS_37_EQ_37(...) \,
191#define Z_IS_38_EQ_38(...) \,
192#define Z_IS_39_EQ_39(...) \,
193#define Z_IS_40_EQ_40(...) \,
194#define Z_IS_41_EQ_41(...) \,
195#define Z_IS_42_EQ_42(...) \,
196#define Z_IS_43_EQ_43(...) \,
197#define Z_IS_44_EQ_44(...) \,
198#define Z_IS_45_EQ_45(...) \,
199#define Z_IS_46_EQ_46(...) \,
200#define Z_IS_47_EQ_47(...) \,
201#define Z_IS_48_EQ_48(...) \,
202#define Z_IS_49_EQ_49(...) \,
203#define Z_IS_50_EQ_50(...) \,
204#define Z_IS_51_EQ_51(...) \,
205#define Z_IS_52_EQ_52(...) \,
206#define Z_IS_53_EQ_53(...) \,
207#define Z_IS_54_EQ_54(...) \,
208#define Z_IS_55_EQ_55(...) \,
209#define Z_IS_56_EQ_56(...) \,
210#define Z_IS_57_EQ_57(...) \,
211#define Z_IS_58_EQ_58(...) \,
212#define Z_IS_59_EQ_59(...) \,
213#define Z_IS_60_EQ_60(...) \,
214#define Z_IS_61_EQ_61(...) \,
215#define Z_IS_62_EQ_62(...) \,
216#define Z_IS_63_EQ_63(...) \,
217#define Z_IS_64_EQ_64(...) \,
218#define Z_IS_65_EQ_65(...) \,
219#define Z_IS_66_EQ_66(...) \,
220#define Z_IS_67_EQ_67(...) \,
221#define Z_IS_68_EQ_68(...) \,
222#define Z_IS_69_EQ_69(...) \,
223#define Z_IS_70_EQ_70(...) \,
224#define Z_IS_71_EQ_71(...) \,
225#define Z_IS_72_EQ_72(...) \,
226#define Z_IS_73_EQ_73(...) \,
227#define Z_IS_74_EQ_74(...) \,
228#define Z_IS_75_EQ_75(...) \,
229#define Z_IS_76_EQ_76(...) \,
230#define Z_IS_77_EQ_77(...) \,
231#define Z_IS_78_EQ_78(...) \,
232#define Z_IS_79_EQ_79(...) \,
233#define Z_IS_80_EQ_80(...) \,
234#define Z_IS_81_EQ_81(...) \,
235#define Z_IS_82_EQ_82(...) \,
236#define Z_IS_83_EQ_83(...) \,
237#define Z_IS_84_EQ_84(...) \,
238#define Z_IS_85_EQ_85(...) \,
239#define Z_IS_86_EQ_86(...) \,
240#define Z_IS_87_EQ_87(...) \,
241#define Z_IS_88_EQ_88(...) \,
242#define Z_IS_89_EQ_89(...) \,
243#define Z_IS_90_EQ_90(...) \,
244#define Z_IS_91_EQ_91(...) \,
245#define Z_IS_92_EQ_92(...) \,
246#define Z_IS_93_EQ_93(...) \,
247#define Z_IS_94_EQ_94(...) \,
248#define Z_IS_95_EQ_95(...) \,
249#define Z_IS_96_EQ_96(...) \,
250#define Z_IS_97_EQ_97(...) \,
251#define Z_IS_98_EQ_98(...) \,
252#define Z_IS_99_EQ_99(...) \,
253#define Z_IS_100_EQ_100(...) \,
254#define Z_IS_101_EQ_101(...) \,
255#define Z_IS_102_EQ_102(...) \,
256#define Z_IS_103_EQ_103(...) \,
257#define Z_IS_104_EQ_104(...) \,
258#define Z_IS_105_EQ_105(...) \,
259#define Z_IS_106_EQ_106(...) \,
260#define Z_IS_107_EQ_107(...) \,
261#define Z_IS_108_EQ_108(...) \,
262#define Z_IS_109_EQ_109(...) \,
263#define Z_IS_110_EQ_110(...) \,
264#define Z_IS_111_EQ_111(...) \,
265#define Z_IS_112_EQ_112(...) \,
266#define Z_IS_113_EQ_113(...) \,
267#define Z_IS_114_EQ_114(...) \,
268#define Z_IS_115_EQ_115(...) \,
269#define Z_IS_116_EQ_116(...) \,
270#define Z_IS_117_EQ_117(...) \,
271#define Z_IS_118_EQ_118(...) \,
272#define Z_IS_119_EQ_119(...) \,
273#define Z_IS_120_EQ_120(...) \,
274#define Z_IS_121_EQ_121(...) \,
275#define Z_IS_122_EQ_122(...) \,
276#define Z_IS_123_EQ_123(...) \,
277#define Z_IS_124_EQ_124(...) \,
278#define Z_IS_125_EQ_125(...) \,
279#define Z_IS_126_EQ_126(...) \,
280#define Z_IS_127_EQ_127(...) \,
281#define Z_IS_128_EQ_128(...) \,
282#define Z_IS_129_EQ_129(...) \,
283#define Z_IS_130_EQ_130(...) \,
284#define Z_IS_131_EQ_131(...) \,
285#define Z_IS_132_EQ_132(...) \,
286#define Z_IS_133_EQ_133(...) \,
287#define Z_IS_134_EQ_134(...) \,
288#define Z_IS_135_EQ_135(...) \,
289#define Z_IS_136_EQ_136(...) \,
290#define Z_IS_137_EQ_137(...) \,
291#define Z_IS_138_EQ_138(...) \,
292#define Z_IS_139_EQ_139(...) \,
293#define Z_IS_140_EQ_140(...) \,
294#define Z_IS_141_EQ_141(...) \,
295#define Z_IS_142_EQ_142(...) \,
296#define Z_IS_143_EQ_143(...) \,
297#define Z_IS_144_EQ_144(...) \,
298#define Z_IS_145_EQ_145(...) \,
299#define Z_IS_146_EQ_146(...) \,
300#define Z_IS_147_EQ_147(...) \,
301#define Z_IS_148_EQ_148(...) \,
302#define Z_IS_149_EQ_149(...) \,
303#define Z_IS_150_EQ_150(...) \,
304#define Z_IS_151_EQ_151(...) \,
305#define Z_IS_152_EQ_152(...) \,
306#define Z_IS_153_EQ_153(...) \,
307#define Z_IS_154_EQ_154(...) \,
308#define Z_IS_155_EQ_155(...) \,
309#define Z_IS_156_EQ_156(...) \,
310#define Z_IS_157_EQ_157(...) \,
311#define Z_IS_158_EQ_158(...) \,
312#define Z_IS_159_EQ_159(...) \,
313#define Z_IS_160_EQ_160(...) \,
314#define Z_IS_161_EQ_161(...) \,
315#define Z_IS_162_EQ_162(...) \,
316#define Z_IS_163_EQ_163(...) \,
317#define Z_IS_164_EQ_164(...) \,
318#define Z_IS_165_EQ_165(...) \,
319#define Z_IS_166_EQ_166(...) \,
320#define Z_IS_167_EQ_167(...) \,
321#define Z_IS_168_EQ_168(...) \,
322#define Z_IS_169_EQ_169(...) \,
323#define Z_IS_170_EQ_170(...) \,
324#define Z_IS_171_EQ_171(...) \,
325#define Z_IS_172_EQ_172(...) \,
326#define Z_IS_173_EQ_173(...) \,
327#define Z_IS_174_EQ_174(...) \,
328#define Z_IS_175_EQ_175(...) \,
329#define Z_IS_176_EQ_176(...) \,
330#define Z_IS_177_EQ_177(...) \,
331#define Z_IS_178_EQ_178(...) \,
332#define Z_IS_179_EQ_179(...) \,
333#define Z_IS_180_EQ_180(...) \,
334#define Z_IS_181_EQ_181(...) \,
335#define Z_IS_182_EQ_182(...) \,
336#define Z_IS_183_EQ_183(...) \,
337#define Z_IS_184_EQ_184(...) \,
338#define Z_IS_185_EQ_185(...) \,
339#define Z_IS_186_EQ_186(...) \,
340#define Z_IS_187_EQ_187(...) \,
341#define Z_IS_188_EQ_188(...) \,
342#define Z_IS_189_EQ_189(...) \,
343#define Z_IS_190_EQ_190(...) \,
344#define Z_IS_191_EQ_191(...) \,
345#define Z_IS_192_EQ_192(...) \,
346#define Z_IS_193_EQ_193(...) \,
347#define Z_IS_194_EQ_194(...) \,
348#define Z_IS_195_EQ_195(...) \,
349#define Z_IS_196_EQ_196(...) \,
350#define Z_IS_197_EQ_197(...) \,
351#define Z_IS_198_EQ_198(...) \,
352#define Z_IS_199_EQ_199(...) \,
353#define Z_IS_200_EQ_200(...) \,
354#define Z_IS_201_EQ_201(...) \,
355#define Z_IS_202_EQ_202(...) \,
356#define Z_IS_203_EQ_203(...) \,
357#define Z_IS_204_EQ_204(...) \,
358#define Z_IS_205_EQ_205(...) \,
359#define Z_IS_206_EQ_206(...) \,
360#define Z_IS_207_EQ_207(...) \,
361#define Z_IS_208_EQ_208(...) \,
362#define Z_IS_209_EQ_209(...) \,
363#define Z_IS_210_EQ_210(...) \,
364#define Z_IS_211_EQ_211(...) \,
365#define Z_IS_212_EQ_212(...) \,
366#define Z_IS_213_EQ_213(...) \,
367#define Z_IS_214_EQ_214(...) \,
368#define Z_IS_215_EQ_215(...) \,
369#define Z_IS_216_EQ_216(...) \,
370#define Z_IS_217_EQ_217(...) \,
371#define Z_IS_218_EQ_218(...) \,
372#define Z_IS_219_EQ_219(...) \,
373#define Z_IS_220_EQ_220(...) \,
374#define Z_IS_221_EQ_221(...) \,
375#define Z_IS_222_EQ_222(...) \,
376#define Z_IS_223_EQ_223(...) \,
377#define Z_IS_224_EQ_224(...) \,
378#define Z_IS_225_EQ_225(...) \,
379#define Z_IS_226_EQ_226(...) \,
380#define Z_IS_227_EQ_227(...) \,
381#define Z_IS_228_EQ_228(...) \,
382#define Z_IS_229_EQ_229(...) \,
383#define Z_IS_230_EQ_230(...) \,
384#define Z_IS_231_EQ_231(...) \,
385#define Z_IS_232_EQ_232(...) \,
386#define Z_IS_233_EQ_233(...) \,
387#define Z_IS_234_EQ_234(...) \,
388#define Z_IS_235_EQ_235(...) \,
389#define Z_IS_236_EQ_236(...) \,
390#define Z_IS_237_EQ_237(...) \,
391#define Z_IS_238_EQ_238(...) \,
392#define Z_IS_239_EQ_239(...) \,
393#define Z_IS_240_EQ_240(...) \,
394#define Z_IS_241_EQ_241(...) \,
395#define Z_IS_242_EQ_242(...) \,
396#define Z_IS_243_EQ_243(...) \,
397#define Z_IS_244_EQ_244(...) \,
398#define Z_IS_245_EQ_245(...) \,
399#define Z_IS_246_EQ_246(...) \,
400#define Z_IS_247_EQ_247(...) \,
401#define Z_IS_248_EQ_248(...) \,
402#define Z_IS_249_EQ_249(...) \,
403#define Z_IS_250_EQ_250(...) \,
404#define Z_IS_251_EQ_251(...) \,
405#define Z_IS_252_EQ_252(...) \,
406#define Z_IS_253_EQ_253(...) \,
407#define Z_IS_254_EQ_254(...) \,
408#define Z_IS_255_EQ_255(...) \,
409
410#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */
Internals for looping macros.