summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-04-20 03:20:34 +0900
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-04-20 09:22:25 +0200
commit114e85d28e9543e39d25414475c3f7e70a6fcbbb (patch)
tree11f730eed1daf05fe40561ac64ed97d80feed4ba /src/core
parent24a5370bbc1b52fee52d8891f66af13e9d77d799 (diff)
downloadsystemd-114e85d28e9543e39d25414475c3f7e70a6fcbbb.tar.gz
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.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/device.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/core/device.c b/src/core/device.c
index 4f6ecf4d7f..1449867e35 100644
--- a/src/core/device.c
+++ b/src/core/device.c
@@ -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. */
+ /* 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);
- if (first)
- hashmap_remove_and_replace(devices, d->sysfs, first->sysfs, first);
+ 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);
}