From: Arran Cudbard-Bell Date: Wed, 13 Oct 2021 01:37:59 +0000 (-0500) Subject: Dedup const code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=786b680793902ef10d5190f47cfab23cd7c5bab4;p=thirdparty%2Ffreeradius-server.git Dedup const code --- diff --git a/src/include/build.h b/src/include/build.h index 4c969abc511..1536068603b 100644 --- a/src/include/build.h +++ b/src/include/build.h @@ -176,6 +176,32 @@ do { \ # define NDEBUG_LOCATION_NONNULL(_num) (_num) #endif +/** Check if a given variable is the _const or not + * + * @param[in] _type The base type of the variable (should not be marked const) + * @param[in] _var to check. + */ +#define IS_CONST(_type, _var) \ + _Generic((_var), \ + _type: false, \ + const _type: true \ + ) + +/** Check if a given variable is the const or unconst version of a type + * + * Expands to _var if _var matches type, otherwise throws a compiler error. + * + * Useful for creating typesafe wrapper macros around functions which take + * void *s. + * + * @param[in] _type The base type of the variable (should not be marked const) + * @param[in] _var to check. + */ +#define IS_TYPE(_type, _var) \ + _Generic((_var), \ + _type: _var, \ + const _type: _var \ + ) /* * Mark variables as unused */ diff --git a/src/lib/util/dcursor.h b/src/lib/util/dcursor.h index 26c60457df6..470a0e5446a 100644 --- a/src/lib/util/dcursor.h +++ b/src/lib/util/dcursor.h @@ -664,18 +664,15 @@ static inline void fr_dcursor_free_list(fr_dcursor_t *cursor) * in the current list. * - The first item returned by the iterator. */ -#define fr_dcursor_iter_mod_init(_cursor, _head, _iter, _iter_uctx, _insert, _remove, _mod_uctx) \ +#define fr_dcursor_iter_mod_init(_cursor, _list, _iter, _iter_uctx, _insert, _remove, _mod_uctx) \ _fr_dcursor_init(_cursor, \ - _head, \ + _list, \ _iter, \ _iter_uctx, \ _insert, \ _remove, \ _mod_uctx, \ - _Generic((_head), \ - fr_dlist_head_t * : false, \ - fr_dlist_head_t const * : true \ - )) + IS_CONST(fr_dlist_head_t *, _head)) /** Initialise a cursor with a custom iterator * @@ -696,10 +693,7 @@ static inline void fr_dcursor_free_list(fr_dcursor_t *cursor) NULL, \ NULL, \ NULL, \ - _Generic((_head), \ - fr_dlist_head_t * : false, \ - fr_dlist_head_t const * : true \ - )) + IS_CONST(fr_dlist_head_t *, _head)) /** Initialise a cursor * @@ -717,10 +711,7 @@ static inline void fr_dcursor_free_list(fr_dcursor_t *cursor) NULL, \ NULL, \ NULL, \ - _Generic((_head), \ - fr_dlist_head_t * : false, \ - fr_dlist_head_t const * : true \ - )) + IS_CONST(fr_dlist_head_t *, _head)) /** Setup a cursor to iterate over attribute items in dlists * diff --git a/src/lib/util/pair.h b/src/lib/util/pair.h index e91ed63817d..187bb942ed6 100644 --- a/src/lib/util/pair.h +++ b/src/lib/util/pair.h @@ -317,10 +317,7 @@ fr_pair_list_t *fr_pair_children(fr_pair_t *head) CC_HINT(nonnull); _list, \ _iter, \ _uctx, \ - _Generic((_list), \ - fr_pair_list_t * : false, \ - fr_pair_list_t const * : true \ - )) + IS_CONST(fr_pair_list_t *, _list)) fr_pair_t *_fr_pair_dcursor_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dcursor_iter_t iter, void const *uctx, bool is_const) CC_HINT(nonnull); @@ -340,10 +337,7 @@ fr_pair_t *_fr_pair_dcursor_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const #define fr_pair_dcursor_init(_cursor, _list) \ _fr_pair_dcursor_init(_cursor, \ _list, \ - _Generic((_list), \ - fr_pair_list_t * : false, \ - fr_pair_list_t const * : true \ - )) + IS_CONST(fr_pair_list_t *, _list)) fr_pair_t *_fr_pair_dcursor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, bool is_const) CC_HINT(nonnull); @@ -360,10 +354,7 @@ fr_pair_t *_fr_pair_dcursor_init(fr_dcursor_t *cursor, fr_pair_list_t const *lis _fr_pair_dcursor_by_da_init(_cursor, \ _list, \ _da, \ - _Generic((_list), \ - fr_pair_list_t * : false, \ - fr_pair_list_t const * : true \ - )) + IS_CONST(fr_pair_list_t *, _list)) fr_pair_t *_fr_pair_dcursor_by_da_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dict_attr_t const *da, bool is_const) CC_HINT(nonnull); @@ -381,10 +372,7 @@ fr_pair_t *_fr_pair_dcursor_by_da_init(fr_dcursor_t *cursor, _fr_pair_dcursor_by_ancestor_init(_cursor, \ _list, \ _da, \ - _Generic((_list), \ - fr_pair_list_t * : false, \ - fr_pair_list_t const * : true \ - )) + IS_CONST(fr_pair_list_t *, _list)) fr_pair_t *_fr_pair_dcursor_by_ancestor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dict_attr_t const *da, bool is_const) CC_HINT(nonnull); diff --git a/src/lib/util/sbuff.h b/src/lib/util/sbuff.h index d2b29d1412b..147718b613f 100644 --- a/src/lib/util/sbuff.h +++ b/src/lib/util/sbuff.h @@ -450,10 +450,7 @@ _Generic((_sbuff_or_marker), \ char const * : (char const *)(_len_or_end) \ ), \ .p_i = _start, \ - .is_const = _Generic((_start), \ - char * : false, \ - char const * : true \ - ) \ + .is_const = IS_CONST(char *, _start) \ }) /** Creates a compound literal to pass into functions which accept a sbuff @@ -477,10 +474,7 @@ _Generic((_sbuff_or_marker), \ char const * : (char const *)(_len_or_end) \ ), \ .p_i = _start, \ - .is_const = _Generic((_start), \ - char * : false, \ - char const * : true \ - ) \ + .is_const = IS_CONST(char *, _start) \ }) @@ -530,10 +524,7 @@ _Generic((_len_or_end), \ char * : (char const *)(_len_or_end), \ char const * : (char const *)(_len_or_end) \ ), \ -_Generic((_start), \ - char * : false, \ - char const * : true \ -)) +IS_CONST(char *, _start)) /** Initialise a special sbuff which automatically reads in more data as the buffer is exhausted *