From 1c71f7f32940a5f3ec093b0e5a249a04f8c2f163 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 27 Aug 2018 14:00:01 +0900 Subject: [PATCH] libsystemd: use DEFINE_ATOMIC_REF_UNREF_FUNC or frineds where applicable --- src/libsystemd/sd-bus/sd-bus.c | 22 +----- src/libsystemd/sd-hwdb/sd-hwdb.c | 24 +++---- src/libsystemd/sd-netlink/netlink-message.c | 8 +-- src/libsystemd/sd-netlink/sd-netlink.c | 74 ++++++++------------- 4 files changed, 39 insertions(+), 89 deletions(-) diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 0da69a17d6f..3ea71ea13d2 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -1567,27 +1567,7 @@ void bus_enter_closing(sd_bus *bus) { bus_set_state(bus, BUS_CLOSING); } -_public_ sd_bus *sd_bus_ref(sd_bus *bus) { - if (!bus) - return NULL; - - assert_se(REFCNT_INC(bus->n_ref) >= 2); - - return bus; -} - -_public_ sd_bus *sd_bus_unref(sd_bus *bus) { - unsigned i; - - if (!bus) - return NULL; - - i = REFCNT_DEC(bus->n_ref); - if (i > 0) - return NULL; - - return bus_free(bus); -} +DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(sd_bus, sd_bus, bus_free); _public_ int sd_bus_is_open(sd_bus *bus) { assert_return(bus, -EINVAL); diff --git a/src/libsystemd/sd-hwdb/sd-hwdb.c b/src/libsystemd/sd-hwdb/sd-hwdb.c index 5de716effb0..371dac53088 100644 --- a/src/libsystemd/sd-hwdb/sd-hwdb.c +++ b/src/libsystemd/sd-hwdb/sd-hwdb.c @@ -359,25 +359,17 @@ _public_ int sd_hwdb_new(sd_hwdb **ret) { return 0; } -_public_ sd_hwdb *sd_hwdb_ref(sd_hwdb *hwdb) { - assert_return(hwdb, NULL); +static sd_hwdb *hwdb_free(sd_hwdb *hwdb) { + assert(hwdb); - assert_se(REFCNT_INC(hwdb->n_ref) >= 2); - - return hwdb; + if (hwdb->map) + munmap((void *)hwdb->map, hwdb->st.st_size); + safe_fclose(hwdb->f); + ordered_hashmap_free(hwdb->properties); + return mfree(hwdb); } -_public_ sd_hwdb *sd_hwdb_unref(sd_hwdb *hwdb) { - if (hwdb && REFCNT_DEC(hwdb->n_ref) == 0) { - if (hwdb->map) - munmap((void *)hwdb->map, hwdb->st.st_size); - safe_fclose(hwdb->f); - ordered_hashmap_free(hwdb->properties); - free(hwdb); - } - - return NULL; -} +DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(sd_hwdb, sd_hwdb, hwdb_free) bool hwdb_validate(sd_hwdb *hwdb) { bool found = false; diff --git a/src/libsystemd/sd-netlink/netlink-message.c b/src/libsystemd/sd-netlink/netlink-message.c index 23907c8224f..0447a8ca050 100644 --- a/src/libsystemd/sd-netlink/netlink-message.c +++ b/src/libsystemd/sd-netlink/netlink-message.c @@ -96,13 +96,7 @@ int sd_netlink_message_request_dump(sd_netlink_message *m, int dump) { return 0; } -sd_netlink_message *sd_netlink_message_ref(sd_netlink_message *m) { - if (!m) - return NULL; - - assert_se(REFCNT_INC(m->n_ref) >= 2); - return m; -} +DEFINE_ATOMIC_REF_FUNC(sd_netlink_message, sd_netlink_message); sd_netlink_message *sd_netlink_message_unref(sd_netlink_message *m) { sd_netlink_message *t; diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c index c01ee73d516..15830d30c3d 100644 --- a/src/libsystemd/sd-netlink/sd-netlink.c +++ b/src/libsystemd/sd-netlink/sd-netlink.c @@ -146,55 +146,39 @@ int sd_netlink_inc_rcvbuf(sd_netlink *rtnl, size_t size) { return fd_inc_rcvbuf(rtnl->fd, size); } -sd_netlink *sd_netlink_ref(sd_netlink *rtnl) { - assert_return(rtnl, NULL); - assert_return(!rtnl_pid_changed(rtnl), NULL); +static sd_netlink *netlink_free(sd_netlink *rtnl) { + struct match_callback *f; + unsigned i; - if (rtnl) - assert_se(REFCNT_INC(rtnl->n_ref) >= 2); + assert(rtnl); - return rtnl; + for (i = 0; i < rtnl->rqueue_size; i++) + sd_netlink_message_unref(rtnl->rqueue[i]); + free(rtnl->rqueue); + + for (i = 0; i < rtnl->rqueue_partial_size; i++) + sd_netlink_message_unref(rtnl->rqueue_partial[i]); + free(rtnl->rqueue_partial); + + free(rtnl->rbuffer); + + hashmap_free_free(rtnl->reply_callbacks); + prioq_free(rtnl->reply_callbacks_prioq); + + sd_event_source_unref(rtnl->io_event_source); + sd_event_source_unref(rtnl->time_event_source); + sd_event_unref(rtnl->event); + + while ((f = rtnl->match_callbacks)) + sd_netlink_remove_match(rtnl, f->type, f->callback, f->userdata); + + hashmap_free(rtnl->broadcast_group_refs); + + safe_close(rtnl->fd); + return mfree(rtnl); } -sd_netlink *sd_netlink_unref(sd_netlink *rtnl) { - if (!rtnl) - return NULL; - - assert_return(!rtnl_pid_changed(rtnl), NULL); - - if (REFCNT_DEC(rtnl->n_ref) == 0) { - struct match_callback *f; - unsigned i; - - for (i = 0; i < rtnl->rqueue_size; i++) - sd_netlink_message_unref(rtnl->rqueue[i]); - free(rtnl->rqueue); - - for (i = 0; i < rtnl->rqueue_partial_size; i++) - sd_netlink_message_unref(rtnl->rqueue_partial[i]); - free(rtnl->rqueue_partial); - - free(rtnl->rbuffer); - - hashmap_free_free(rtnl->reply_callbacks); - prioq_free(rtnl->reply_callbacks_prioq); - - sd_event_source_unref(rtnl->io_event_source); - sd_event_source_unref(rtnl->time_event_source); - sd_event_unref(rtnl->event); - - while ((f = rtnl->match_callbacks)) { - sd_netlink_remove_match(rtnl, f->type, f->callback, f->userdata); - } - - hashmap_free(rtnl->broadcast_group_refs); - - safe_close(rtnl->fd); - free(rtnl); - } - - return NULL; -} +DEFINE_ATOMIC_REF_UNREF_FUNC(sd_netlink, sd_netlink, netlink_free); static void rtnl_seal_message(sd_netlink *rtnl, sd_netlink_message *m) { assert(rtnl);