@@ -66,6 +66,8 @@ void *oidmap_put(struct oidmap *map, void *entry);
*/
void *oidmap_remove(struct oidmap *map, const struct object_id *key);
+#define oidmap_entry_from_hashmap_entry(entry) \
+ container_of_or_null(entry, struct oidmap_entry, internal_entry)
struct oidmap_iter {
struct hashmap_iter h_iter;
@@ -76,18 +78,45 @@ static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter
hashmap_iter_init(&map->map, &iter->h_iter);
}
-static inline void *oidmap_iter_next(struct oidmap_iter *iter)
+/* Returns the next oidmap_entry, or NULL if there are no more entries. */
+static inline struct oidmap_entry *oidmap_iter_next(struct oidmap_iter *iter)
{
- /* TODO: this API could be reworked to do compile-time type checks */
- return (void *)hashmap_iter_next(&iter->h_iter);
+ return oidmap_entry_from_hashmap_entry(
+ hashmap_iter_next(&iter->h_iter));
}
-static inline void *oidmap_iter_first(struct oidmap *map,
+/* Initializes the iterator and returns the first entry, if any. */
+static inline struct oidmap_entry *oidmap_iter_first(struct oidmap *map,
struct oidmap_iter *iter)
{
oidmap_iter_init(map, iter);
- /* TODO: this API could be reworked to do compile-time type checks */
- return (void *)oidmap_iter_next(iter);
+ return oidmap_iter_next(iter);
}
+/*
+ * Returns the first entry in @map using @iter, where the entry if of
+ * @type (e.g. "struct foo") and @member is the name of the
+ * "struct oidmap_entry" in @type
+ */
+#define oidmap_iter_first_entry(map, iter, type, member) \
+ container_of_or_null(oidmap_iter_first(map, iter), type, member)
+
+/* Internal macro for oidmap_for_each_entry */
+#define oidmap_iter_next_entry_offset(iter, offset) \
+ container_of_or_null_offset(oidmap_iter_next(iter), offset)
+
+/* Internal macro for oidmap_for_each_entry */
+#define oidmap_iter_first_entry_offset(map, iter, offset) \
+ container_of_or_null_offset(oidmap_iter_first(map, iter), offset)
+
+/*
+ * Iterate through @map using @iter, @var is a pointer to a type
+ * containing a @member which is a "struct oidmap_entry"
+ */
+#define oidmap_for_each_entry(map, iter, var, member) \
+ for (var = oidmap_iter_first_entry_offset(map, iter, \
+ OFFSETOF_VAR(var, member)); \
+ var; \
+ var = oidmap_iter_next_entry_offset(iter, \
+ OFFSETOF_VAR(var, member)))
#endif
@@ -95,9 +95,10 @@ int cmd__oidmap(int argc, const char **argv)
} else if (!strcmp("iterate", cmd)) {
struct oidmap_iter iter;
- oidmap_iter_init(&map, &iter);
- while ((entry = oidmap_iter_next(&iter)))
- printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
+ struct test_entry *ent;
+
+ oidmap_for_each_entry(&map, &iter, ent, entry)
+ printf("%s %s\n", oid_to_hex(&ent->entry.oid), ent->name);
} else {
87571c3f (hashmap: use *_entry APIs for iteration, 2019-10-06) modified hashmap_iter_next() to return a hashmap_entry pointer instead of void pointer. However, oidmap_iter_next() is unaware of the struct type containing oidmap_entry and explicitly returns a void pointer. Rewrite oidmap_iter_next() to return oidmap_entry pointer and add helper macros to return pointers to container struct. Signed-off-by: Abhishek Kumar <abhishekkumar8222@gmail.com> --- oidmap.h | 41 +++++++++++++++++++++++++++++++++++------ t/helper/test-oidmap.c | 7 ++++--- 2 files changed, 39 insertions(+), 9 deletions(-)