core/device: rewrite how device unit is removed from Manager.devices_by_sysfs

If the device unit is not the head of the list saved in
Manager.devices_by_sysfs, then it is not necessary to replace the
existing hashmap entry. This should not change any behavior, just
refactoring.
This commit is contained in:
Yu Watanabe 2023-04-20 03:20:34 +09:00 committed by Daan De Meyer
parent 24a5370bbc
commit 114e85d28e

View file

@ -55,24 +55,31 @@ static int device_by_path(Manager *m, const char *path, Unit **ret) {
static void device_unset_sysfs(Device *d) {
Hashmap *devices;
Device *first;
assert(d);
if (!d->sysfs)
return;
/* Remove this unit from the chain of devices which share the
* same sysfs path. */
devices = UNIT(d)->manager->devices_by_sysfs;
first = hashmap_get(devices, d->sysfs);
LIST_REMOVE(same_sysfs, first, d);
/* Remove this unit from the chain of devices which share the same sysfs path. */
if (first)
hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
devices = UNIT(d)->manager->devices_by_sysfs;
if (d->same_sysfs_prev)
/* If this is not the first unit, then simply remove this unit. */
d->same_sysfs_prev->same_sysfs_next = d->same_sysfs_next;
else if (d->same_sysfs_next)
/* If this is the first unit, replace with the next unit. */
assert_se(hashmap_replace(devices, d->same_sysfs_next->sysfs, d->same_sysfs_next) >= 0);
else
/* Otherwise, remove the entry. */
hashmap_remove(devices, d->sysfs);
if (d->same_sysfs_next)
d->same_sysfs_next->same_sysfs_prev = d->same_sysfs_prev;
d->same_sysfs_prev = d->same_sysfs_next = NULL;
d->sysfs = mfree(d->sysfs);
}