]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Dedup const code
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 13 Oct 2021 01:37:59 +0000 (20:37 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 13 Oct 2021 01:37:59 +0000 (20:37 -0500)
src/include/build.h
src/lib/util/dcursor.h
src/lib/util/pair.h
src/lib/util/sbuff.h

index 4c969abc51190060fab252bfdc171b87472d5d84..1536068603b3ce8ed27108935df7a4e2107447a2 100644 (file)
@@ -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
  */
index 26c60457df64ff699535797615f647913c09cb7b..470a0e5446ab8b1cd91921f7c69778a699af07b2 100644 (file)
@@ -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
  *
index e91ed63817d589e1b2001f85e4bf6434e18260a2..187bb942ed6187852a01e8c8fe7b35b4ee97792f 100644 (file)
@@ -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);
index d2b29d1412b10c111816523986aa3249a001c395..147718b613f426c8d7c0011346c5eca5cf5ddab1 100644 (file)
@@ -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
  *