1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

libsystemd: use DEFINE_ATOMIC_REF_UNREF_FUNC or frineds where applicable

This commit is contained in:
Yu Watanabe 2018-08-27 14:00:01 +09:00
parent 1d3044e275
commit 1c71f7f329
4 changed files with 39 additions and 89 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);