int fdset_iterate(FDSet *s, Iterator *i) {
void *p;
- p = set_iterate(MAKE_SET(s), i);
- if (!p)
+ if (!set_iterate(MAKE_SET(s), i, &p))
return -ENOENT;
return PTR_TO_FD(p);
: hashmap_iterate_in_internal_order(h, i);
}
-void *internal_hashmap_iterate(HashmapBase *h, Iterator *i, const void **key) {
+bool internal_hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key) {
struct hashmap_base_entry *e;
void *data;
unsigned idx;
idx = hashmap_iterate_entry(h, i);
if (idx == IDX_NIL) {
+ if (value)
+ *value = NULL;
if (key)
*key = NULL;
- return NULL;
+ return false;
}
e = bucket_at(h, idx);
data = entry_value(h, e);
+ if (value)
+ *value = data;
if (key)
*key = e->key;
- return data;
+ return true;
}
-void *set_iterate(Set *s, Iterator *i) {
- return internal_hashmap_iterate(HASHMAP_BASE(s), i, NULL);
+bool set_iterate(Set *s, Iterator *i, void **value) {
+ return internal_hashmap_iterate(HASHMAP_BASE(s), i, value, NULL);
}
#define HASHMAP_FOREACH_IDX(idx, h, i) \
} Iterator;
#define _IDX_ITERATOR_FIRST (UINT_MAX - 1)
+#define _IDX_ITERATOR_NIL (UINT_MAX)
#define ITERATOR_FIRST ((Iterator) { .idx = _IDX_ITERATOR_FIRST, .next_key = NULL })
typedef unsigned long (*hash_func_t)(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]);
return internal_hashmap_buckets(HASHMAP_BASE(h));
}
-void *internal_hashmap_iterate(HashmapBase *h, Iterator *i, const void **key);
-static inline void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) {
- return internal_hashmap_iterate(HASHMAP_BASE(h), i, key);
+bool internal_hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key);
+static inline bool hashmap_iterate(Hashmap *h, Iterator *i, void **value, const void **key) {
+ return internal_hashmap_iterate(HASHMAP_BASE(h), i, value, key);
}
-static inline void *ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, const void **key) {
- return internal_hashmap_iterate(HASHMAP_BASE(h), i, key);
+static inline bool ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, void **value, const void **key) {
+ return internal_hashmap_iterate(HASHMAP_BASE(h), i, value, key);
}
void internal_hashmap_clear(HashmapBase *h);
* It is safe to remove the current entry.
*/
#define HASHMAP_FOREACH(e, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); \
- (e); \
- (e) = hashmap_iterate((h), &(i), NULL))
+ for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), NULL); )
#define ORDERED_HASHMAP_FOREACH(e, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), NULL); \
- (e); \
- (e) = ordered_hashmap_iterate((h), &(i), NULL))
+ for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), NULL); )
#define HASHMAP_FOREACH_KEY(e, k, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); \
- (e); \
- (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
+ for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
#define ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
- for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)); \
- (e); \
- (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)))
+ for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
return ordered_hashmap_isempty((OrderedHashmap*) s);
}
-static inline void *ordered_set_iterate(OrderedSet *s, Iterator *i) {
- return ordered_hashmap_iterate((OrderedHashmap*) s, i, NULL);
+static inline bool ordered_set_iterate(OrderedSet *s, Iterator *i, void **value) {
+ return ordered_hashmap_iterate((OrderedHashmap*) s, i, value, NULL);
}
#define ORDERED_SET_FOREACH(e, s, i) \
- for ((i) = ITERATOR_FIRST, (e) = ordered_set_iterate((s), &(i)); (e); (e) = ordered_set_iterate((s), &(i)))
+ for ((i) = ITERATOR_FIRST; ordered_set_iterate((s), &(i), (void**)&(e)); )
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free);
return internal_hashmap_buckets(HASHMAP_BASE(s));
}
-void *set_iterate(Set *s, Iterator *i);
+bool set_iterate(Set *s, Iterator *i, void **value);
static inline void set_clear(Set *s) {
internal_hashmap_clear(HASHMAP_BASE(s));
int set_put_strdupv(Set *s, char **l);
#define SET_FOREACH(e, s, i) \
- for ((i) = ITERATOR_FIRST, (e) = set_iterate((s), &(i)); (e); (e) = set_iterate((s), &(i)))
+ for ((i) = ITERATOR_FIRST; set_iterate((s), &(i), (void**)&(e)); )
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
track->modified = false;
track->iterator = ITERATOR_FIRST;
- hashmap_iterate(track->names, &track->iterator, (const void**) &n);
+ hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
return n;
}
if (track->modified)
return NULL;
- hashmap_iterate(track->names, &track->iterator, (const void**) &n);
+ hashmap_iterate(track->names, &track->iterator, NULL, (const void**) &n);
return n;
}
}
_public_ const char *sd_device_get_tag_first(sd_device *device) {
+ void *v;
+
assert_return(device, NULL);
(void) device_read_db(device);
device->tags_iterator_generation = device->tags_generation;
device->tags_iterator = ITERATOR_FIRST;
- return set_iterate(device->tags, &device->tags_iterator);
+ set_iterate(device->tags, &device->tags_iterator, &v);
+ return v;
}
_public_ const char *sd_device_get_tag_next(sd_device *device) {
+ void *v;
+
assert_return(device, NULL);
(void) device_read_db(device);
if (device->tags_iterator_generation != device->tags_generation)
return NULL;
- return set_iterate(device->tags, &device->tags_iterator);
+ set_iterate(device->tags, &device->tags_iterator, &v);
+ return v;
}
_public_ const char *sd_device_get_devlink_first(sd_device *device) {
+ void *v;
+
assert_return(device, NULL);
(void) device_read_db(device);
device->devlinks_iterator_generation = device->devlinks_generation;
device->devlinks_iterator = ITERATOR_FIRST;
- return set_iterate(device->devlinks, &device->devlinks_iterator);
+ set_iterate(device->devlinks, &device->devlinks_iterator, &v);
+ return v;
}
_public_ const char *sd_device_get_devlink_next(sd_device *device) {
+ void *v;
+
assert_return(device, NULL);
(void) device_read_db(device);
if (device->devlinks_iterator_generation != device->devlinks_generation)
return NULL;
- return set_iterate(device->devlinks, &device->devlinks_iterator);
+ set_iterate(device->devlinks, &device->devlinks_iterator, &v);
+ return v;
}
static int device_properties_prepare(sd_device *device) {
device->properties_iterator_generation = device->properties_generation;
device->properties_iterator = ITERATOR_FIRST;
- value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key);
+ ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
if (_value)
*_value = value;
if (device->properties_iterator_generation != device->properties_generation)
return NULL;
- value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key);
+ ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
if (_value)
*_value = value;
}
_public_ const char *sd_device_get_sysattr_first(sd_device *device) {
+ void *v;
int r;
assert_return(device, NULL);
device->sysattrs_iterator = ITERATOR_FIRST;
- return set_iterate(device->sysattrs, &device->sysattrs_iterator);
+ set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
+ return v;
}
_public_ const char *sd_device_get_sysattr_next(sd_device *device) {
+ void *v;
+
assert_return(device, NULL);
if (!device->sysattrs_read)
return NULL;
- return set_iterate(device->sysattrs, &device->sysattrs_iterator);
+ set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
+ return v;
}
_public_ int sd_device_has_tag(sd_device *device, const char *tag) {
}
_public_ int sd_hwdb_enumerate(sd_hwdb *hwdb, const char **key, const char **value) {
- const void *k, *v;
+ const void *k;
+ void *v;
assert_return(hwdb, -EINVAL);
assert_return(key, -EINVAL);
if (hwdb->properties_modified)
return -EAGAIN;
- v = ordered_hashmap_iterate(hwdb->properties, &hwdb->properties_iterator, &k);
+ ordered_hashmap_iterate(hwdb->properties, &hwdb->properties_iterator, &v, &k);
if (!k)
return 0;
hashmap_put(m, key, (void*) (const char*) "my dummy val");
HASHMAP_FOREACH_KEY(s, key, m, i) {
+ assert(s);
if (!key_found[0] && streq(key, "key 1"))
key_found[0] = true;
else if (!key_found[1] && streq(key, "key 2"))