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