tdb2: implement tdb_chainlock_read/tdb_chainunlock_read.
[ccan] / ccan / jbitset / jbitset_type.h
1 #ifndef CCAN_JBITSET_TYPE_H
2 #define CCAN_JBITSET_TYPE_H
3 #include <ccan/jbitset/jbitset.h>
4
5 /**
6  * JBIT_DEFINE_TYPE - create a set of jbit ops for a given pointer type
7  * @type: a type whose pointers will go into the bitset.
8  * @name: a name for all the functions to define (of form jbit_<name>_*)
9  *
10  * This macro defines a set of inline functions for typesafe and convenient
11  * usage of a Judy bitset for pointers.  It is assumed that a NULL pointer
12  * is never set in the bitset.
13  *
14  * Example:
15  *      JBIT_DEFINE_TYPE(char, char);
16  *      JBIT_DEFINE_TYPE(struct foo, foo);
17  *
18  *      static struct jbitset_char *jc;
19  *      struct jbitset_foo *jf;
20  *
21  *      static void add_to_bitsets(const char *p, const struct foo *f)
22  *      {
23  *              // Note, this adds the pointer, not the string!
24  *              jbit_char_set(jc, p);
25  *              jbit_foo_set(jf, f);
26  *      }
27  */
28 #define JBIT_DEFINE_TYPE(type, name)                                    \
29 struct jbitset_##name;                                                  \
30 static inline struct jbitset_##name *jbit_##name##_new(void)            \
31 {                                                                       \
32         return (struct jbitset_##name *)jbit_new();                     \
33 }                                                                       \
34 static inline void jbit_##name##_free(const struct jbitset_##name *set) \
35 {                                                                       \
36         jbit_free((const struct jbitset *)set);                         \
37 }                                                                       \
38 static inline const char *jbit_##name##_error(struct jbitset_##name *set) \
39 {                                                                       \
40         return jbit_error((struct jbitset *)set);                       \
41 }                                                                       \
42 static inline bool jbit_##name##_test(const struct jbitset_##name *set, \
43                                       const type *index)                \
44 {                                                                       \
45         return jbit_test((const struct jbitset *)set, (size_t)index);   \
46 }                                                                       \
47 static inline bool jbit_##name##_set(struct jbitset_##name *set,        \
48                                      const type *index)                 \
49 {                                                                       \
50         return jbit_set((struct jbitset *)set, (size_t)index);          \
51 }                                                                       \
52 static inline bool jbit_##name##_clear(struct jbitset_##name *set,      \
53                                        const type *index)               \
54 {                                                                       \
55         return jbit_clear((struct jbitset *)set, (size_t)index);        \
56 }                                                                       \
57 static inline size_t jbit_##name##_count(struct jbitset_##name *set)    \
58 {                                                                       \
59         return jbit_popcount((const struct jbitset *)set, 0, -1);       \
60 }                                                                       \
61 static inline type *jbit_##name##_nth(const struct jbitset_##name *set, \
62                                             size_t n)                   \
63 {                                                                       \
64         return (type *)jbit_nth((const struct jbitset *)set, n, 0);     \
65 }                                                                       \
66 static inline type *jbit_##name##_first(const struct jbitset_##name *set) \
67 {                                                                       \
68         return (type *)jbit_first((const struct jbitset *)set, 0);      \
69 }                                                                       \
70 static inline type *jbit_##name##_next(struct jbitset_##name *set,      \
71                                        const type *prev)                \
72 {                                                                       \
73         return (type *)jbit_next((const struct jbitset *)set, (size_t)prev, 0); \
74 }                                                                       \
75 static inline type *jbit_##name##_last(struct jbitset_##name *set)      \
76 {                                                                       \
77         return (type *)jbit_last((const struct jbitset *)set, 0);       \
78 }                                                                       \
79 static inline type *jbit_##name##_prev(struct jbitset_##name *set,      \
80                                        const type *prev)                \
81 {                                                                       \
82         return (type *)jbit_prev((const struct jbitset *)set, (size_t)prev, 0); \
83 }
84 #endif /* CCAN_JBITSET_TYPE_H */