Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
util_macro.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
17#ifndef ZEPHYR_INCLUDE_SYS_UTIL_MACROS_H_
18#define ZEPHYR_INCLUDE_SYS_UTIL_MACROS_H_
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
29/*
30 * Most of the eldritch implementation details for all the macrobatics
31 * below (APIs like IS_ENABLED(), COND_CODE_1(), etc.) are hidden away
32 * in this file.
33 */
34#include <sys/util_internal.h>
35
36#ifndef BIT
37#if defined(_ASMLANGUAGE)
38#define BIT(n) (1 << (n))
39#else
44#define BIT(n) (1UL << (n))
45#endif
46#endif
47
49#define BIT64(_n) (1ULL << (_n))
50
61#define WRITE_BIT(var, bit, set) \
62 ((var) = (set) ? ((var) | BIT(bit)) : ((var) & ~BIT(bit)))
63
68#define BIT_MASK(n) (BIT(n) - 1UL)
69
74#define BIT64_MASK(n) (BIT64(n) - 1ULL)
75
101#define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro)
102/* INTERNAL: the first pass above is just to expand any existing
103 * macros, we need the macro value to be e.g. a literal "1" at
104 * expansion time in the next macro, not "(1)", etc... Standard
105 * recursive expansion does not work.
106 */
107
156#define COND_CODE_1(_flag, _if_1_code, _else_code) \
157 Z_COND_CODE_1(_flag, _if_1_code, _else_code)
158
172#define COND_CODE_0(_flag, _if_0_code, _else_code) \
173 Z_COND_CODE_0(_flag, _if_0_code, _else_code)
174
200#define IF_ENABLED(_flag, _code) \
201 COND_CODE_1(_flag, _code, ())
202
239#define IS_EMPTY(a) Z_IS_EMPTY_(a, 1, 0,)
240
268#define LIST_DROP_EMPTY(...) \
269 Z_LIST_DROP_FIRST(FOR_EACH(Z_LIST_NO_EMPTIES, (), __VA_ARGS__))
270
288#define EMPTY
289
298#define IDENTITY(V) V
299
308#define GET_ARG_N(N, ...) Z_GET_ARG_##N(__VA_ARGS__)
309
318#define GET_ARGS_LESS_N(N, ...) Z_GET_ARGS_LESS_##N(__VA_ARGS__)
319
331#define UTIL_OR(a, b) COND_CODE_1(UTIL_BOOL(a), (a), (b))
332
344#define UTIL_AND(a, b) COND_CODE_1(UTIL_BOOL(a), (b), (0))
345
369#define UTIL_LISTIFY(LEN, F, ...) UTIL_CAT(Z_UTIL_LISTIFY_, LEN)(F, __VA_ARGS__)
370
392#define FOR_EACH(F, sep, ...) \
393 Z_FOR_EACH(F, sep, REVERSE_ARGS(__VA_ARGS__))
394
447#define FOR_EACH_NONEMPTY_TERM(F, term, ...) \
448 COND_CODE_0( \
449 /* are there zero non-empty arguments ? */ \
450 NUM_VA_ARGS_LESS_1(LIST_DROP_EMPTY(__VA_ARGS__, _)), \
451 /* if so, expand to nothing */ \
452 (), \
453 /* otherwise, expand to: */ \
454 (/* FOR_EACH() on nonempty elements, */ \
455 FOR_EACH(F, term, LIST_DROP_EMPTY(__VA_ARGS__)) \
456 /* plus a final terminator */ \
457 __DEBRACKET term \
458 ))
459
484#define FOR_EACH_IDX(F, sep, ...) \
485 Z_FOR_EACH_IDX(F, sep, REVERSE_ARGS(__VA_ARGS__))
486
512#define FOR_EACH_FIXED_ARG(F, sep, fixed_arg, ...) \
513 Z_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, REVERSE_ARGS(__VA_ARGS__))
514
540#define FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, ...) \
541 Z_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, REVERSE_ARGS(__VA_ARGS__))
542
547#define REVERSE_ARGS(...) \
548 Z_FOR_EACH_ENGINE(Z_FOR_EACH_EXEC, (,), Z_BYPASS, _, __VA_ARGS__)
549
556#define NUM_VA_ARGS_LESS_1(...) \
557 NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 63, 62, 61, \
558 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
559 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
560 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
561 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
562 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
563 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ~)
564
585#define MACRO_MAP_CAT(...) MACRO_MAP_CAT_(__VA_ARGS__)
586
600#define MACRO_MAP_CAT_N(N, ...) MACRO_MAP_CAT_N_(N, __VA_ARGS__)
601
606#ifdef __cplusplus
607}
608#endif
609
610#endif /* ZEPHYR_INCLUDE_SYS_UTIL_MACROS_H_ */
Misc utilities.