1 #ifndef CCAN_JMAP_TYPE_H
2 #define CCAN_JMAP_TYPE_H
3 #include <ccan/jmap/jmap.h>
6 * JMAP_DEFINE_UINTIDX_TYPE - create a set of jmap ops for integer->ptr map
7 * @type: a type whose pointers will be values in the map.
8 * @name: a name for all the functions to define (of form jmap_<name>_*)
10 * It's easiest if NULL values aren't placed in the map: jmap_@name_get will
11 * return NULL if an index isn't valid.
13 * The following wrapper functions are defined; each one is the same as
14 * the jmap.h generic equivalent except where noted:
16 * // Creating, errors and freeing.
17 * struct jmap_@name *jmap_@name_new(void);
18 * void jmap_@name_free(const struct jmap_@name *map);
19 * const char *jmap_@name_error(struct jmap_@name *map);
21 * // Add, set, delete, test and get.
22 * bool jmap_@name_add(struct jmap_@name *map,
23 * unsigned long idx, const type *value);
24 * bool jmap_@name_set(const struct jmap_@name *map,
25 * unsigned long idx, const type *value);
26 * bool jmap_@name_del(struct jmap_@name *map, unsigned long idx);
27 * bool jmap_@name_test(const struct jmap_@name *map, unsigned long idx);
28 * type *jmap_@name_get(const struct jmap_@name *map, unsigned long idx);
30 * // Counting and iteration.
31 * unsigned long jmap_@name_popcount(const struct jmap_@name *map,
32 * unsigned long start,
33 * unsigned long end_incl);
34 * unsigned long jmap_@name_nth(const struct jmap_@name *map,
35 * unsigned long n, unsigned long invalid);
36 * unsigned long jmap_@name_first(const struct jmap_@name *map,
37 * unsigned long invalid);
38 * unsigned long jmap_@name_next(const struct jmap_@name *map,
40 * unsigned long invalid);
41 * unsigned long jmap_@name_last(const struct jmap_@name *map,
42 * unsigned long invalid);
43 * unsigned long jmap_@name_prev(const struct jmap_@name *map,
45 * unsigned long invalid);
47 * // Get pointers to values to use.
48 * type **jmap_@name_getval(const struct jmap_@name *map,
50 * void jmap_@name_putval(struct jmap_@name *map, type ***p);
51 * type **jmap_@name_nthval(struct jmap_@name *map,
52 * unsigned long n, unsigned long *idx);
53 * type **jmap_@name_firstval(const struct jmap_@name *map,
54 * unsigned long *idx);
55 * type **jmap_@name_nextval(const struct jmap_@name *map,
56 * unsigned long *idx);
57 * type **jmap_@name_lastval(const struct jmap_@name *map,
58 * unsigned long *idx);
59 * type **jmap_@name_prevval(const struct jmap_@name *map,
60 * unsigned long *idx);
62 #define JMAP_DEFINE_UINTIDX_TYPE(type, name) \
64 static inline struct jmap_##name *jmap_##name##_new(void) \
66 return (struct jmap_##name *)jmap_new(); \
68 static inline void jmap_##name##_free(const struct jmap_##name *map) \
70 jmap_free((const struct jmap *)map); \
72 static inline const char *jmap_##name##_error(struct jmap_##name *map) \
74 return jmap_error((struct jmap *)map); \
76 static inline bool jmap_##name##_add(struct jmap_##name *map, \
77 unsigned long idx, const type *value) \
79 return jmap_add((struct jmap *)map, idx, (unsigned long)value); \
81 static inline bool jmap_##name##_set(const struct jmap_##name *map, \
82 unsigned long idx, const type *value) \
84 return jmap_set((const struct jmap *)map, idx, (unsigned long)value); \
86 static inline bool jmap_##name##_del(struct jmap_##name *map, \
89 return jmap_del((struct jmap *)map, idx); \
91 static inline bool jmap_##name##_test(const struct jmap_##name *map, \
94 return jmap_test((const struct jmap *)map, (unsigned long)idx); \
96 static inline type *jmap_##name##_get(const struct jmap_##name *map, \
99 return (type *)jmap_get((const struct jmap *)map, idx, 0); \
101 static inline unsigned long \
102 jmap_##name##_popcount(const struct jmap_##name *map, \
103 unsigned long start, unsigned long end_incl) \
105 return jmap_popcount((const struct jmap *)map, start, end_incl); \
107 static inline unsigned long jmap_##name##_nth(const struct jmap_##name *map, \
109 unsigned long invalid) \
111 return jmap_nth((const struct jmap *)map, n, invalid); \
113 static inline unsigned long \
114 jmap_##name##_first(const struct jmap_##name *map, \
115 unsigned long invalid) \
117 return jmap_first((const struct jmap *)map, invalid); \
119 static inline unsigned long \
120 jmap_##name##_next(const struct jmap_##name *map, \
121 unsigned long prev, unsigned long invalid) \
123 return jmap_next((const struct jmap *)map, prev, invalid); \
125 static inline unsigned long \
126 jmap_##name##_last(const struct jmap_##name *map, \
127 unsigned long invalid) \
129 return jmap_last((const struct jmap *)map, invalid); \
131 static inline unsigned long \
132 jmap_##name##_prev(const struct jmap_##name *map, \
133 unsigned long prev, unsigned long invalid) \
135 return jmap_prev((const struct jmap *)map, prev, invalid); \
137 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
140 return (type **)jmap_getval((struct jmap *)map, idx); \
142 static inline void jmap_##name##_putval(struct jmap_##name *map, \
145 return jmap_putval((struct jmap *)map, (unsigned long **)p); \
147 static inline type **jmap_##name##_nthval(struct jmap_##name *map, \
149 unsigned long *idx) \
151 return (type **)jmap_nthval((struct jmap *)map, n, idx); \
153 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
154 unsigned long *idx) \
156 return (type **)jmap_firstval((const struct jmap *)map, idx); \
158 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
159 unsigned long *idx) \
161 return (type **)jmap_nextval((const struct jmap *)map, idx); \
163 static inline type **jmap_##name##_lastval(const struct jmap_##name *map, \
164 unsigned long *idx) \
166 return (type **)jmap_lastval((const struct jmap *)map, idx); \
168 static inline type **jmap_##name##_prevval(const struct jmap_##name *map, \
169 unsigned long *idx) \
171 return (type **)jmap_prevval((const struct jmap *)map, idx); \
175 * JMAP_DEFINE_PTRIDX_TYPE - create a map of jmap ops for ptr->ptr map
176 * @itype: a type whose pointers will idx into the map.
177 * @type: a type whose pointers will be values in the map.
178 * @name: a name for all the functions to define (of form jmap_<name>_*)
180 * This macro defines a map of inline functions for typesafe and
181 * convenient usage of a pointer-idxed Judy map of pointers. It is
182 * assumed that a NULL pointer is never an idx in the map, as
183 * various functions return NULL for "invalid idx". Similarly,
184 * jmap_@name_get will return NULL if an idx isn't valid, so NULL indices
185 * are not recommended (though you can tell using jmap_@name_test).
187 * Since the ordering is by idx pointer value, it's generally quite useless.
188 * Thus we don't define order-specific functions, except first/next for
191 * The following wrapper functions are defined; each one is the same as
192 * the jmap.h generic equivalent:
194 * struct jmap_@name *jmap_@name_new(void);
195 * void jmap_@name_free(const struct jmap_@name *map);
196 * const char *jmap_@name_error(struct jmap_@name *map);
198 * bool jmap_@name_add(const struct jmap_@name *map,
199 * const itype *idx, const type *value);
200 * bool jmap_@name_set(const struct jmap_@name *map,
201 * const itype *idx, const type *value);
202 * bool jmap_@name_del(struct jmap_@name *map, const itype *idx);
203 * bool jmap_@name_test(const struct jmap_@name *map, const itype *idx);
205 * type *jmap_@name_get(const struct jmap_@name *map, const itype *idx);
206 * itype *jmap_@name_count(const struct jmap_@name *map);
207 * itype *jmap_@name_first(const struct jmap_@name *map);
208 * itype *jmap_@name_next(const struct jmap_@name *map,
209 * const itype *prev);
211 * type **jmap_@name_getval(const struct jmap_@name *map,
213 * void jmap_@name_putval(struct jmap_@name *map, type ***p);
214 * type **jmap_@name_firstval(const struct jmap_@name *map,
215 * const itype **idx);
216 * type **jmap_@name_nextval(const struct jmap_@name *map,
217 * const itype **idx);
219 #define JMAP_DEFINE_PTRIDX_TYPE(itype, type, name) \
220 struct jmap_##name; \
221 static inline struct jmap_##name *jmap_##name##_new(void) \
223 return (struct jmap_##name *)jmap_new(); \
225 static inline void jmap_##name##_free(const struct jmap_##name *map) \
227 jmap_free((const struct jmap *)map); \
229 static inline const char *jmap_##name##_error(struct jmap_##name *map) \
231 return jmap_error((struct jmap *)map); \
233 static inline bool jmap_##name##_add(struct jmap_##name *map, \
234 const itype *idx, const type *value) \
236 return jmap_add((struct jmap *)map, (unsigned long)idx, \
237 (unsigned long)value); \
239 static inline bool jmap_##name##_set(const struct jmap_##name *map, \
240 const itype *idx, const type *value) \
242 return jmap_set((const struct jmap *)map, (unsigned long)idx, \
243 (unsigned long)value); \
245 static inline bool jmap_##name##_del(struct jmap_##name *map, \
248 return jmap_del((struct jmap *)map, (unsigned long)idx); \
250 static inline bool jmap_##name##_test(const struct jmap_##name *map, \
253 return jmap_test((const struct jmap *)map, (unsigned long)idx); \
255 static inline type *jmap_##name##_get(const struct jmap_##name *map, \
258 return (type *)jmap_get((const struct jmap *)map, \
259 (unsigned long)idx, 0); \
261 static inline unsigned long \
262 jmap_##name##_count(const struct jmap_##name *map) \
264 return jmap_popcount((const struct jmap *)map, 0, -1); \
266 static inline itype *jmap_##name##_first(const struct jmap_##name *map) \
268 return (itype *)jmap_first((const struct jmap *)map, 0); \
270 static inline itype *jmap_##name##_next(const struct jmap_##name *map, \
273 return (itype *)jmap_next((const struct jmap *)map, \
274 (unsigned long)prev, 0); \
276 static inline type **jmap_##name##_getval(const struct jmap_##name *map, \
279 return (type **)jmap_getval((struct jmap *)map, \
280 (unsigned long)idx); \
282 static inline void jmap_##name##_putval(struct jmap_##name *map, \
285 return jmap_putval((struct jmap *)map, (unsigned long **)p); \
287 static inline type **jmap_##name##_firstval(const struct jmap_##name *map, \
292 ret = (type **)jmap_firstval((const struct jmap *)map, &i); \
296 static inline type **jmap_##name##_nextval(const struct jmap_##name *map, \
299 return (type **)jmap_nextval((const struct jmap *)map, \
300 (unsigned long *)idx); \
302 #endif /* CCAN_JMAP_TYPE_H */