Skip to content

Commit 11bd8fd

Browse files
committed
Merge branch 'ak/oidmap-free-update' into pu
* ak/oidmap-free-update: oidmap: rework iterators to return typed pointer oidmap: make oidmap_free independent of struct layout
2 parents ade586d + 56349b4 commit 11bd8fd

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

hashmap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void hashmap_free_(struct hashmap *map, ssize_t offset);
245245
* where @member is the hashmap_entry struct used to associate with @map
246246
*/
247247
#define hashmap_free_entries(map, type, member) \
248-
hashmap_free_(map, offsetof(type, member));
248+
hashmap_free_(map, offsetof(type, member))
249249

250250
/* hashmap_entry functions */
251251

oidmap.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ void oidmap_free(struct oidmap *map, int free_entries)
2626
if (!map)
2727
return;
2828

29-
/* TODO: make oidmap itself not depend on struct layouts */
30-
hashmap_free_(&map->map, free_entries ? 0 : -1);
29+
if (free_entries)
30+
hashmap_free_entries(&map->map, struct oidmap_entry, internal_entry);
31+
else
32+
hashmap_free(&map->map);
3133
}
3234

3335
void *oidmap_get(const struct oidmap *map, const struct object_id *key)

oidmap.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,21 @@ static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter
7676
hashmap_iter_init(&map->map, &iter->h_iter);
7777
}
7878

79-
static inline void *oidmap_iter_next(struct oidmap_iter *iter)
80-
{
81-
/* TODO: this API could be reworked to do compile-time type checks */
82-
return (void *)hashmap_iter_next(&iter->h_iter);
83-
}
79+
/*
80+
* Returns the next entry, or NULL if there are no more entries.
81+
*
82+
* The entry is of @type (e.g. "struct foo") and has a member of type struct
83+
* oidmap_entry.
84+
*/
85+
#define oidmap_iter_next(iter, type) \
86+
(type *) hashmap_iter_next(&(iter)->h_iter)
8487

85-
static inline void *oidmap_iter_first(struct oidmap *map,
86-
struct oidmap_iter *iter)
87-
{
88-
oidmap_iter_init(map, iter);
89-
/* TODO: this API could be reworked to do compile-time type checks */
90-
return (void *)oidmap_iter_next(iter);
91-
}
88+
/*
89+
* Returns the first entry in @map using @iter, where the entry is of @type
90+
* (e.g. "struct foo") and has a member of type struct oidmap_entry.
91+
*/
92+
#define oidmap_iter_first(map, iter, type) \
93+
({oidmap_iter_init(map, iter); \
94+
oidmap_iter_next(iter, type); })
9295

9396
#endif

t/helper/test-oidmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int cmd__oidmap(int argc, const char **argv)
9696

9797
struct oidmap_iter iter;
9898
oidmap_iter_init(&map, &iter);
99-
while ((entry = oidmap_iter_next(&iter)))
99+
while ((entry = oidmap_iter_next(&iter, struct test_entry)))
100100
printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
101101

102102
} else {

0 commit comments

Comments
 (0)