]> git.ozlabs.org Git - ccan/blobdiff - ccan/sparse_bsearch/sparse_bsearch.h
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / sparse_bsearch / sparse_bsearch.h
index 5317dbf849841a2a05a35f9c0a1ccb1edf3a8c9e..a673371433735d9ed9a1a170386b027a4a88cbcf 100644 (file)
@@ -1,3 +1,4 @@
+/* Licensed under LGPLv2+ - see LICENSE file for details */
 #ifndef SPARSE_BSEARCH_H
 #define SPARSE_BSEARCH_H
 #include <stdbool.h>
  * @validfn: whether this element is valid.
  *
  * Binary search of a sorted array, which may have some invalid entries.
+ * Note that cmpfn and validfn take const pointers.
  *
  * Example:
- *     static bool val_valid(unsigned int *val)
+ *     static bool val_valid(const unsigned int *val)
  *     {
  *             return *val != 0;
  *     }
@@ -27,7 +29,7 @@
  *     static unsigned int values[] = { 1, 7, 11, 1235, 99999 };
  *
  *     // Return true if this value is in set, and remove it.
- *     bool remove_from_values(unsigned int val)
+ *     static bool remove_from_values(unsigned int val)
  *     {
  *             unsigned int *p;
  *             p = sparse_bsearch(&val, values, 5, val_cmp, val_valid);
 #define sparse_bsearch(key, base, nmemb, cmpfn, validfn)               \
        _sparse_bsearch((key)+check_types_match((key), &(base)[0]),     \
                        (base), (nmemb), sizeof((base)[0]),             \
-                       typesafe_cb_cmp(int, (cmpfn), (base)),          \
-                       typesafe_cb_const(bool, (validfn), (base)))
+                       typesafe_cb_cast(int (*)(const void *, const void *), \
+                                        int (*)(const __typeof__(*(base)) *, \
+                                                const __typeof__(*(base)) *), \
+                                        (cmpfn)),                      \
+                       typesafe_cb_cast(bool (*)(const void *),        \
+                                        bool (*)(const __typeof__(*(base)) *), \
+                                        (validfn)))
 
 void *_sparse_bsearch(const void *key, const void *base,
                      size_t nmemb, size_t size,