diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-04-17 00:20:16 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-04-19 11:01:46 +0200 |
commit | c2d7dd35d2a8cda439384a385b0c1bec804b9b79 (patch) | |
tree | 34aa1cb827054afbce26f8b5f98ddfa7f330d473 /src | |
parent | sd-bus: fix reference counter to be incremented (diff) | |
download | systemd-c2d7dd35d2a8cda439384a385b0c1bec804b9b79.tar.xz systemd-c2d7dd35d2a8cda439384a385b0c1bec804b9b79.zip |
sd-bus: introduce ref/unref function for track_item
Diffstat (limited to 'src')
-rw-r--r-- | src/libsystemd/sd-bus/bus-track.c | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c index 891fd0c899..135dfddc5f 100644 --- a/src/libsystemd/sd-bus/bus-track.c +++ b/src/libsystemd/sd-bus/bus-track.c @@ -40,7 +40,6 @@ struct sd_bus_track { "arg0='", name, "'") static struct track_item* track_item_free(struct track_item *i) { - if (!i) return NULL; @@ -49,7 +48,8 @@ static struct track_item* track_item_free(struct track_item *i) { return mfree(i); } -DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free); +DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(struct track_item, track_item, track_item_free); +DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_unref); DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(track_item_hash_ops, char, string_hash_func, string_compare_func, struct track_item, track_item_free); @@ -180,7 +180,7 @@ static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus } _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) { - _cleanup_(track_item_freep) struct track_item *n = NULL; + _cleanup_(track_item_unrefp) struct track_item *n = NULL; struct track_item *i; const char *match; int r; @@ -190,14 +190,8 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) { i = hashmap_get(track->names, name); if (i) { - if (track->recursive) { - unsigned k = i->n_ref + 1; - - if (k < i->n_ref) /* Check for overflow */ - return -EOVERFLOW; - - i->n_ref = k; - } + if (track->recursive) + track_item_ref(i); bus_track_remove_from_queue(track); return 0; @@ -207,9 +201,14 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) { if (r < 0) return r; - n = new0(struct track_item, 1); + n = new(struct track_item, 1); if (!n) return -ENOMEM; + + *n = (struct track_item) { + .n_ref = 1, + }; + n->name = strdup(name); if (!n->name) return -ENOMEM; @@ -241,8 +240,7 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) { return r; } - n->n_ref = 1; - n = NULL; + TAKE_PTR(n); bus_track_remove_from_queue(track); track->modified = true; @@ -264,14 +262,13 @@ _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) { i = hashmap_get(track->names, name); if (!i) return -EUNATCH; - if (i->n_ref <= 0) - return -EUNATCH; - i->n_ref--; - - if (i->n_ref <= 0) + assert(i->n_ref >= 1); + if (i->n_ref <= 1) return bus_track_remove_name_fully(track, name); + track_item_unref(i); + return 1; } |