basic/set: let set_put_strdup() create the set with string hash ops

If we're using a set with _put_strdup(), most of the time we want to use
string hash ops on the set, and free the strings when done. This defines
the appropriate a new string_hash_ops_free structure to automatically free
the keys when removing the set, and makes set_put_strdup() and set_put_strdupv()
instantiate the set with those hash ops.

hashmap_put_strdup() was already doing something similar.

(It is OK to instantiate the set earlier, possibly with a different hash ops
structure. set_put_strdup() will then use the existing set. It is also OK
to call set_free_free() instead of set_free() on a set with
string_hash_ops_free, the effect is the same, we're just overriding the
override of the cleanup function.)

No functional change intended.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-05-06 16:54:06 +02:00
parent 0894f08bf1
commit be32732168
25 changed files with 71 additions and 108 deletions
+4 -4
View File
@@ -101,7 +101,7 @@ _public_ int sd_device_enumerator_add_match_subsystem(sd_device_enumerator *enum
if (r < 0)
return r;
r = set_put_strdup(*set, subsystem);
r = set_put_strdup(set, subsystem);
if (r < 0)
return r;
@@ -192,7 +192,7 @@ _public_ int sd_device_enumerator_add_match_sysname(sd_device_enumerator *enumer
if (r < 0)
return r;
r = set_put_strdup(enumerator->match_sysname, sysname);
r = set_put_strdup(&enumerator->match_sysname, sysname);
if (r < 0)
return r;
@@ -211,7 +211,7 @@ _public_ int sd_device_enumerator_add_match_tag(sd_device_enumerator *enumerator
if (r < 0)
return r;
r = set_put_strdup(enumerator->match_tag, tag);
r = set_put_strdup(&enumerator->match_tag, tag);
if (r < 0)
return r;
@@ -242,7 +242,7 @@ int device_enumerator_add_match_parent_incremental(sd_device_enumerator *enumera
if (r < 0)
return r;
r = set_put_strdup(enumerator->match_parent, path);
r = set_put_strdup(&enumerator->match_parent, path);
if (r < 0)
return r;
+6 -18
View File
@@ -68,9 +68,9 @@ static sd_device *device_free(sd_device *device) {
ordered_hashmap_free_free_free(device->properties);
ordered_hashmap_free_free_free(device->properties_db);
hashmap_free_free_free(device->sysattr_values);
set_free_free(device->sysattrs);
set_free_free(device->tags);
set_free_free(device->devlinks);
set_free(device->sysattrs);
set_free(device->tags);
set_free(device->devlinks);
return mfree(device);
}
@@ -1078,11 +1078,7 @@ int device_add_tag(sd_device *device, const char *tag) {
if (!is_valid_tag(tag))
return -EINVAL;
r = set_ensure_allocated(&device->tags, &string_hash_ops);
if (r < 0)
return r;
r = set_put_strdup(device->tags, tag);
r = set_put_strdup(&device->tags, tag);
if (r < 0)
return r;
@@ -1098,11 +1094,7 @@ int device_add_devlink(sd_device *device, const char *devlink) {
assert(device);
assert(devlink);
r = set_ensure_allocated(&device->devlinks, &string_hash_ops);
if (r < 0)
return r;
r = set_put_strdup(device->devlinks, devlink);
r = set_put_strdup(&device->devlinks, devlink);
if (r < 0)
return r;
@@ -1591,10 +1583,6 @@ static int device_sysattrs_read_all(sd_device *device) {
if (!dir)
return -errno;
r = set_ensure_allocated(&device->sysattrs, &string_hash_ops);
if (r < 0)
return r;
FOREACH_DIRENT_ALL(dent, dir, return -errno) {
_cleanup_free_ char *path = NULL;
struct stat statbuf;
@@ -1613,7 +1601,7 @@ static int device_sysattrs_read_all(sd_device *device) {
if (!(statbuf.st_mode & S_IRUSR))
continue;
r = set_put_strdup(device->sysattrs, dent->d_name);
r = set_put_strdup(&device->sysattrs, dent->d_name);
if (r < 0)
return r;
}