1 #ifndef CCAN_HTABLE_TYPE_H
2 #define CCAN_HTABLE_TYPE_H
3 #include <ccan/htable/htable.h>
7 * HTABLE_DEFINE_TYPE - create a set of htable ops for a type
8 * @type: a type whose pointers will be values in the hash.
9 * @keyof: a function/macro to extract a key from a @type element.
10 * @hashfn: a hash function for a @key
11 * @cmpfn: a comparison function for two keyof()s.
12 * @name: a name for all the functions to define (of form htable_<name>_*)
14 * NULL values may not be placed into the hash table.
16 * The following wrapper functions are defined; each one is a
17 * simplified version of the htable.h equivalent:
19 * // Creating and freeing.
20 * struct htable_@name *htable_@name_new(void);
21 * void htable_@name_free(const struct htable_@name *ht);
23 * // Add, delete and find.
24 * bool htable_@name_add(struct htable_@name *ht, const type *e);
25 * bool htable_@name_del(struct htable_@name *ht, const type *e);
26 * bool htable_@name_delkey(struct htable_@name *ht, const ktype *k);
27 * type *htable_@name_get(const struct htable_@name *ht, const ktype *k);
30 * struct htable_@name_iter;
31 * type *htable_@name_first(const struct htable_@name *ht,
32 * struct htable_@name_iter *i);
33 * type *htable_@name_next(const struct htable_@name *ht,
34 * struct htable_@name_iter *i);
36 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, cmpfn, name) \
37 struct htable_##name; \
38 struct htable_##name##_iter { struct htable_iter i; }; \
39 static inline size_t htable_##name##_hash(const void *elem, void *priv) \
41 return hashfn(keyof((const type *)elem)); \
43 static inline struct htable_##name *htable_##name##_new(void) \
45 return (struct htable_##name *)htable_new(htable_##name##_hash, \
48 static inline void htable_##name##_free(const struct htable_##name *ht) \
50 htable_free((const struct htable *)ht); \
52 static inline bool htable_##name##_add(struct htable_##name *ht, \
55 return htable_add((struct htable *)ht, hashfn(keyof(elem)), elem); \
57 static inline bool htable_##name##_del(const struct htable_##name *ht, \
60 return htable_del((struct htable *)ht, hashfn(keyof(elem)), elem); \
62 static inline type *htable_##name##_get(const struct htable_##name *ht, \
63 const HTABLE_KTYPE(keyof) k) \
65 /* Typecheck for cmpfn */ \
66 (void)sizeof(cmpfn((const type *)NULL, \
67 keyof((const type *)NULL))); \
68 return (type *)htable_get((const struct htable *)ht, \
70 (bool (*)(const void *, void *))(cmpfn), \
73 static inline bool htable_##name##_delkey(struct htable_##name *ht, \
74 const HTABLE_KTYPE(keyof) k) \
76 type *elem = htable_##name##_get(ht, k); \
78 return htable_##name##_del(ht, elem); \
81 static inline type *htable_##name##_first(const struct htable_##name *ht, \
82 struct htable_##name##_iter *iter) \
84 return htable_first((const struct htable *)ht, &iter->i); \
86 static inline type *htable_##name##_next(const struct htable_##name *ht, \
87 struct htable_##name##_iter *iter) \
89 return htable_next((const struct htable *)ht, &iter->i); \
93 #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
95 #define HTABLE_KTYPE(keyof) void *
97 #endif /* CCAN_HTABLE_TYPE_H */