-#define typesafe_cb_postargs(rettype, fn, arg, ...) \
- cast_if_type(cast_if_type(cast_if_type((fn), \
- rettype (*)(const typeof(arg), \
- __VA_ARGS__), \
- rettype (*)(void *, \
- __VA_ARGS__)), \
- rettype (*)(volatile typeof(arg), \
- __VA_ARGS__), \
- rettype (*)(void *, __VA_ARGS__)), \
- rettype (*)(typeof(arg), __VA_ARGS__), \
- rettype (*)(void *, __VA_ARGS__))
-
+#define typesafe_cb_postargs(rtype, fn, arg, ...) \
+ cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn), \
+ rtype (*)(typeof(arg), __VA_ARGS__))
+/**
+ * typesafe_cb_cmp - cast a compare function if it matches the arg
+ * @rtype: the return type of the callback function
+ * @fn: the callback function to cast
+ * @arg: the (pointer) argument(s) to hand to the compare function.
+ *
+ * If a callback function takes two matching-type arguments, this macro does
+ * appropriate casts to a function which takes two const void * arguments if
+ * the callback provided takes two a const pointers to @arg.
+ *
+ * It is assumed that @arg is of pointer type: usually @arg is passed
+ * or assigned to a void * elsewhere anyway. Note also that the type
+ * arg points to must be defined.
+ *
+ * Example:
+ * void _my_qsort(void *base, size_t nmemb, size_t size,
+ * int (*cmp)(const void *, const void *));
+ * #define my_qsort(base, nmemb, cmpfn) \
+ * _my_qsort((base), (nmemb), sizeof(*(base)), \
+ * typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
+ */
+#define typesafe_cb_cmp(rtype, cmpfn, arg) \
+ cast_if_type(rtype (*)(const void *, const void *), (cmpfn), \
+ rtype (*)(const typeof(*arg)*, const typeof(*arg)*))
+