diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-08-27 07:01:46 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-08-27 07:01:46 +0200 |
commit | 8301aa0bf145099e55443b8c10d00a62d20e0d29 (patch) | |
tree | dee970b9c80b599a83143e255d634e1293c89254 | |
parent | macro: introduce DEFINE_TRIVIAL_REF_UNREF_FUNC() macro and friends (diff) | |
download | systemd-8301aa0bf145099e55443b8c10d00a62d20e0d29.tar.xz systemd-8301aa0bf145099e55443b8c10d00a62d20e0d29.zip |
tree-wide: use DEFINE_TRIVIAL_REF_UNREF_FUNC() macro or friends where applicable
34 files changed, 176 insertions, 763 deletions
diff --git a/src/basic/bpf-program.c b/src/basic/bpf-program.c index ed57f9ffdc..2c61e04132 100644 --- a/src/basic/bpf-program.c +++ b/src/basic/bpf-program.c @@ -29,25 +29,8 @@ int bpf_program_new(uint32_t prog_type, BPFProgram **ret) { return 0; } -BPFProgram *bpf_program_ref(BPFProgram *p) { - if (!p) - return NULL; - - assert(p->n_ref > 0); - p->n_ref++; - - return p; -} - -BPFProgram *bpf_program_unref(BPFProgram *p) { - if (!p) - return NULL; - - assert(p->n_ref > 0); - p->n_ref--; - - if (p->n_ref > 0) - return NULL; +static BPFProgram *bpf_program_free(BPFProgram *p) { + assert(p); /* Unfortunately, the kernel currently doesn't implicitly detach BPF programs from their cgroups when the last * fd to the BPF program is closed. This has nasty side-effects since this means that abnormally terminated @@ -66,6 +49,8 @@ BPFProgram *bpf_program_unref(BPFProgram *p) { return mfree(p); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(BPFProgram, bpf_program, bpf_program_free); + int bpf_program_add_instructions(BPFProgram *p, const struct bpf_insn *instructions, size_t count) { assert(p); diff --git a/src/basic/format-table.c b/src/basic/format-table.c index 89cdd06b2a..8ef278248d 100644 --- a/src/basic/format-table.c +++ b/src/basic/format-table.c @@ -171,32 +171,16 @@ Table *table_new_internal(const char *first_header, ...) { return TAKE_PTR(t); } -static TableData *table_data_unref(TableData *d) { - if (!d) - return NULL; - - assert(d->n_ref > 0); - d->n_ref--; - - if (d->n_ref > 0) - return NULL; +static TableData *table_data_free(TableData *d) { + assert(d); free(d->formatted); return mfree(d); } +DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(TableData, table_data, table_data_free); DEFINE_TRIVIAL_CLEANUP_FUNC(TableData*, table_data_unref); -static TableData *table_data_ref(TableData *d) { - if (!d) - return NULL; - - assert(d->n_ref > 0); - d->n_ref++; - - return d; -} - Table *table_unref(Table *t) { size_t i; diff --git a/src/core/dynamic-user.c b/src/core/dynamic-user.c index 5e20783102..b3b197b494 100644 --- a/src/core/dynamic-user.c +++ b/src/core/dynamic-user.c @@ -20,6 +20,8 @@ /* Takes a value generated randomly or by hashing and turns it into a UID in the right range */ #define UID_CLAMP_INTO_RANGE(rnd) (((uid_t) (rnd) % (DYNAMIC_UID_MAX - DYNAMIC_UID_MIN + 1)) + DYNAMIC_UID_MIN) +DEFINE_PRIVATE_TRIVIAL_REF_FUNC(DynamicUser, dynamic_user); + static DynamicUser* dynamic_user_free(DynamicUser *d) { if (!d) return NULL; @@ -531,16 +533,6 @@ int dynamic_user_current(DynamicUser *d, uid_t *ret) { return 0; } -static DynamicUser* dynamic_user_ref(DynamicUser *d) { - if (!d) - return NULL; - - assert(d->n_ref > 0); - d->n_ref++; - - return d; -} - static DynamicUser* dynamic_user_unref(DynamicUser *d) { if (!d) return NULL; diff --git a/src/core/socket.c b/src/core/socket.c index 1dcd8162f4..92cf181541 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -547,26 +547,8 @@ static SocketPeer *socket_peer_new(void) { return p; } -SocketPeer *socket_peer_ref(SocketPeer *p) { - if (!p) - return NULL; - - assert(p->n_ref > 0); - p->n_ref++; - - return p; -} - -SocketPeer *socket_peer_unref(SocketPeer *p) { - if (!p) - return NULL; - - assert(p->n_ref > 0); - - p->n_ref--; - - if (p->n_ref > 0) - return NULL; +static SocketPeer *socket_peer_free(SocketPeer *p) { + assert(p); if (p->socket) set_remove(p->socket->peers_by_address, p); @@ -574,6 +556,8 @@ SocketPeer *socket_peer_unref(SocketPeer *p) { return mfree(p); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer, socket_peer, socket_peer_free); + int socket_acquire_peer(Socket *s, int fd, SocketPeer **p) { _cleanup_(socket_peer_unrefp) SocketPeer *remote = NULL; SocketPeer sa = {}, *i; diff --git a/src/journal-remote/journal-remote-write.c b/src/journal-remote/journal-remote-write.c index 9211e7df82..188ff3582d 100644 --- a/src/journal-remote/journal-remote-write.c +++ b/src/journal-remote/journal-remote-write.c @@ -34,7 +34,7 @@ Writer* writer_new(RemoteServer *server) { return w; } -Writer* writer_free(Writer *w) { +static Writer* writer_free(Writer *w) { if (!w) return NULL; @@ -54,19 +54,7 @@ Writer* writer_free(Writer *w) { return mfree(w); } -Writer* writer_unref(Writer *w) { - if (w && (-- w->n_ref <= 0)) - writer_free(w); - - return NULL; -} - -Writer* writer_ref(Writer *w) { - if (w) - assert_se(++ w->n_ref >= 2); - - return w; -} +DEFINE_TRIVIAL_REF_UNREF_FUNC(Writer, writer, writer_free); int writer_write(Writer *w, struct iovec_wrapper *iovw, diff --git a/src/journal-remote/journal-remote-write.h b/src/journal-remote/journal-remote-write.h index 9f3df423d8..e445859ecf 100644 --- a/src/journal-remote/journal-remote-write.h +++ b/src/journal-remote/journal-remote-write.h @@ -20,8 +20,6 @@ typedef struct Writer { } Writer; Writer* writer_new(RemoteServer* server); -Writer* writer_free(Writer *w); - Writer* writer_ref(Writer *w); Writer* writer_unref(Writer *w); diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c index 442c994b9f..90549f1c9f 100644 --- a/src/journal/mmap-cache.c +++ b/src/journal/mmap-cache.c @@ -85,14 +85,6 @@ MMapCache* mmap_cache_new(void) { return m; } -MMapCache* mmap_cache_ref(MMapCache *m) { - assert(m); - assert(m->n_ref > 0); - - m->n_ref++; - return m; -} - static void window_unlink(Window *w) { Context *c; @@ -278,7 +270,7 @@ static void context_free(Context *c) { free(c); } -static void mmap_cache_free(MMapCache *m) { +static MMapCache *mmap_cache_free(MMapCache *m) { int i; assert(m); @@ -292,22 +284,10 @@ static void mmap_cache_free(MMapCache *m) { while (m->unused) window_free(m->unused); - free(m); + return mfree(m); } -MMapCache* mmap_cache_unref(MMapCache *m) { - - if (!m) - return NULL; - - assert(m->n_ref > 0); - - m->n_ref--; - if (m->n_ref == 0) - mmap_cache_free(m); - - return NULL; -} +DEFINE_TRIVIAL_REF_UNREF_FUNC(MMapCache, mmap_cache, mmap_cache_free); static int make_room(MMapCache *m) { assert(m); diff --git a/src/libsystemd-network/ndisc-router.c b/src/libsystemd-network/ndisc-router.c index 25b693a458..c6e75ab312 100644 --- a/src/libsystemd-network/ndisc-router.c +++ b/src/libsystemd-network/ndisc-router.c @@ -15,28 +15,7 @@ #include "ndisc-router.h" #include "strv.h" -_public_ sd_ndisc_router* sd_ndisc_router_ref(sd_ndisc_router *rt) { - if (!rt) - return NULL; - - assert(rt->n_ref > 0); - rt->n_ref++; - - return rt; -} - -_public_ sd_ndisc_router* sd_ndisc_router_unref(sd_ndisc_router *rt) { - if (!rt) - return NULL; - - assert(rt->n_ref > 0); - rt->n_ref--; - - if (rt->n_ref > 0) - return NULL; - - return mfree(rt); -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc_router, sd_ndisc_router, mfree); sd_ndisc_router *ndisc_router_new(size_t raw_size) { sd_ndisc_router *rt; diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index de86028a5d..9777040fbd 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -1951,27 +1951,8 @@ sd_event *sd_dhcp_client_get_event(sd_dhcp_client *client) { return client->event; } -sd_dhcp_client *sd_dhcp_client_ref(sd_dhcp_client *client) { - - if (!client) - return NULL; - - assert(client->n_ref >= 1); - client->n_ref++; - - return client; -} - -sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) { - - if (!client) - return NULL; - - assert(client->n_ref >= 1); - client->n_ref--; - - if (client->n_ref > 0) - return NULL; +static sd_dhcp_client *dhcp_client_free(sd_dhcp_client *client) { + assert(client); log_dhcp_client(client, "FREE"); @@ -1990,6 +1971,8 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) { return mfree(client); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_client, sd_dhcp_client, dhcp_client_free); + int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) { _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 8b6f5ad34f..a65e6166fb 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -246,27 +246,8 @@ int sd_dhcp_lease_get_vendor_specific(sd_dhcp_lease *lease, const void **data, s return 0; } -sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) { - - if (!lease) - return NULL; - - assert(lease->n_ref >= 1); - lease->n_ref++; - - return lease; -} - -sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) { - - if (!lease) - return NULL; - - assert(lease->n_ref >= 1); - lease->n_ref--; - - if (lease->n_ref > 0) - return NULL; +static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) { + assert(lease); while (lease->private_options) { struct sd_dhcp_raw_option *option = lease->private_options; @@ -288,6 +269,8 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) { return mfree(lease); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_lease, sd_dhcp_lease, dhcp_lease_free); + static int lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) { assert(option); assert(ret); diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index d91849f379..1a1aba90c1 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -101,17 +101,6 @@ int sd_dhcp_server_is_running(sd_dhcp_server *server) { return !!server->receive_message; } -sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server) { - - if (!server) - return NULL; - - assert(server->n_ref >= 1); - server->n_ref++; - - return server; -} - void client_id_hash_func(const void *p, struct siphash *state) { const DHCPClientId *id = p; @@ -145,17 +134,10 @@ static const struct hash_ops client_id_hash_ops = { .compare = client_id_compare_func }; -sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server) { +static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) { DHCPLease *lease; - if (!server) - return NULL; - - assert(server->n_ref >= 1); - server->n_ref--; - - if (server->n_ref > 0) - return NULL; + assert(server); log_dhcp_server(server, "UNREF"); @@ -175,6 +157,8 @@ sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server) { return mfree(server); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_server, sd_dhcp_server, dhcp_server_free); + int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) { _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL; diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 84c58af346..4f3993134f 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -1431,27 +1431,8 @@ sd_event *sd_dhcp6_client_get_event(sd_dhcp6_client *client) { return client->event; } -sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client) { - - if (!client) - return NULL; - - assert(client->n_ref >= 1); - client->n_ref++; - - return client; -} - -sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) { - - if (!client) - return NULL; - - assert(client->n_ref >= 1); - client->n_ref--; - - if (client->n_ref > 0) - return NULL; +static sd_dhcp6_client *dhcp6_client_free(sd_dhcp6_client *client) { + assert(client); client_reset(client); @@ -1464,6 +1445,8 @@ sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) { return mfree(client); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp6_client, sd_dhcp6_client, dhcp6_client_free); + int sd_dhcp6_client_new(sd_dhcp6_client **ret) { _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL; size_t t; diff --git a/src/libsystemd-network/sd-dhcp6-lease.c b/src/libsystemd-network/sd-dhcp6-lease.c index 779ad54b4a..54283133cb 100644 --- a/src/libsystemd-network/sd-dhcp6-lease.c +++ b/src/libsystemd-network/sd-dhcp6-lease.c @@ -374,27 +374,8 @@ int sd_dhcp6_lease_get_ntp_fqdn(sd_dhcp6_lease *lease, char ***ntp_fqdn) { return -ENOENT; } -sd_dhcp6_lease *sd_dhcp6_lease_ref(sd_dhcp6_lease *lease) { - - if (!lease) - return NULL; - - assert(lease->n_ref >= 1); - lease->n_ref++; - - return lease; -} - -sd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease) { - - if (!lease) - return NULL; - - assert(lease->n_ref >= 1); - lease->n_ref--; - - if (lease->n_ref > 0) - return NULL; +static sd_dhcp6_lease *dhcp6_lease_free(sd_dhcp6_lease *lease) { + assert(lease); free(lease->serverid); dhcp6_lease_free_ia(&lease->ia); @@ -410,6 +391,8 @@ sd_dhcp6_lease *sd_dhcp6_lease_unref(sd_dhcp6_lease *lease) { return mfree(lease); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp6_lease, sd_dhcp6_lease, dhcp6_lease_free); + int dhcp6_lease_new(sd_dhcp6_lease **ret) { sd_dhcp6_lease *lease; diff --git a/src/libsystemd-network/sd-ipv4acd.c b/src/libsystemd-network/sd-ipv4acd.c index a40d40db90..6c12b5144e 100644 --- a/src/libsystemd-network/sd-ipv4acd.c +++ b/src/libsystemd-network/sd-ipv4acd.c @@ -97,25 +97,8 @@ static void ipv4acd_reset(sd_ipv4acd *acd) { ipv4acd_set_state(acd, IPV4ACD_STATE_INIT, true); } -sd_ipv4acd *sd_ipv4acd_ref(sd_ipv4acd *acd) { - if (!acd) - return NULL; - - assert_se(acd->n_ref >= 1); - acd->n_ref++; - - return acd; -} - -sd_ipv4acd *sd_ipv4acd_unref(sd_ipv4acd *acd) { - if (!acd) - return NULL; - - assert_se(acd->n_ref >= 1); - acd->n_ref--; - - if (acd->n_ref > 0) - return NULL; +static sd_ipv4acd *ipv4acd_free(sd_ipv4acd *acd) { + assert(acd); ipv4acd_reset(acd); sd_ipv4acd_detach_event(acd); @@ -123,6 +106,8 @@ sd_ipv4acd *sd_ipv4acd_unref(sd_ipv4acd *acd) { return mfree(acd); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4acd, sd_ipv4acd, ipv4acd_free); + int sd_ipv4acd_new(sd_ipv4acd **ret) { _cleanup_(sd_ipv4acd_unrefp) sd_ipv4acd *acd = NULL; diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c index 7307e1668f..e451dff744 100644 --- a/src/libsystemd-network/sd-ipv4ll.c +++ b/src/libsystemd-network/sd-ipv4ll.c @@ -55,30 +55,15 @@ struct sd_ipv4ll { static void ipv4ll_on_acd(sd_ipv4acd *ll, int event, void *userdata); -sd_ipv4ll *sd_ipv4ll_ref(sd_ipv4ll *ll) { - if (!ll) - return NULL; - - assert(ll->n_ref >= 1); - ll->n_ref++; - - return ll; -} - -sd_ipv4ll *sd_ipv4ll_unref(sd_ipv4ll *ll) { - if (!ll) - return NULL; - - assert(ll->n_ref >= 1); - ll->n_ref--; - - if (ll->n_ref > 0) - return NULL; +static sd_ipv4ll *ipv4ll_free(sd_ipv4ll *ll) { + assert(ll); sd_ipv4acd_unref(ll->acd); return mfree(ll); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_ipv4ll, sd_ipv4ll, ipv4ll_free); + int sd_ipv4ll_new(sd_ipv4ll **ret) { _cleanup_(sd_ipv4ll_unrefp) sd_ipv4ll *ll = NULL; int r; diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c index c75d6079e1..e3e6ac34d8 100644 --- a/src/libsystemd-network/sd-lldp.c +++ b/src/libsystemd-network/sd-lldp.c @@ -329,27 +329,8 @@ _public_ int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex) { return 0; } -_public_ sd_lldp* sd_lldp_ref(sd_lldp *lldp) { - - if (!lldp) - return NULL; - - assert(lldp->n_ref > 0); - lldp->n_ref++; - - return lldp; -} - -_public_ sd_lldp* sd_lldp_unref(sd_lldp *lldp) { - - if (!lldp) - return NULL; - - assert(lldp->n_ref > 0); - lldp->n_ref --; - - if (lldp->n_ref > 0) - return NULL; +static sd_lldp* lldp_free(sd_lldp *lldp) { + assert(lldp); lldp_reset(lldp); sd_lldp_detach_event(lldp); @@ -360,6 +341,8 @@ _public_ sd_lldp* sd_lldp_unref(sd_lldp *lldp) { return mfree(lldp); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_lldp, sd_lldp, lldp_free); + _public_ int sd_lldp_new(sd_lldp **ret) { _cleanup_(sd_lldp_unrefp) sd_lldp *lldp = NULL; int r; diff --git a/src/libsystemd-network/sd-ndisc.c b/src/libsystemd-network/sd-ndisc.c index 2d81160b02..acab75ee18 100644 --- a/src/libsystemd-network/sd-ndisc.c +++ b/src/libsystemd-network/sd-ndisc.c @@ -100,17 +100,6 @@ _public_ sd_event *sd_ndisc_get_event(sd_ndisc *nd) { return nd->event; } -_public_ sd_ndisc *sd_ndisc_ref(sd_ndisc *nd) { - - if (!nd) - return NULL; - - assert(nd->n_ref > 0); - nd->n_ref++; - - return nd; -} - static int ndisc_reset(sd_ndisc *nd) { assert(nd); @@ -123,22 +112,16 @@ static int ndisc_reset(sd_ndisc *nd) { return 0; } -_public_ sd_ndisc *sd_ndisc_unref(sd_ndisc *nd) { - - if (!nd) - return NULL; - - assert(nd->n_ref > 0); - nd->n_ref--; - - if (nd->n_ref > 0) - return NULL; +static sd_ndisc *ndisc_free(sd_ndisc *nd) { + assert(nd); ndisc_reset(nd); sd_ndisc_detach_event(nd); return mfree(nd); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_ndisc, sd_ndisc, ndisc_free); + _public_ int sd_ndisc_new(sd_ndisc **ret) { _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL; diff --git a/src/libsystemd-network/sd-radv.c b/src/libsystemd-network/sd-radv.c index 86750b876c..b4de6bda90 100644 --- a/src/libsystemd-network/sd-radv.c +++ b/src/libsystemd-network/sd-radv.c @@ -86,25 +86,8 @@ static void radv_reset(sd_radv *ra) { ra->ra_sent = 0; } -_public_ sd_radv *sd_radv_ref(sd_radv *ra) { - if (!ra) - return NULL; - - assert(ra->n_ref > 0); - ra->n_ref++; - - return ra; -} - -_public_ sd_radv *sd_radv_unref(sd_radv *ra) { - if (!ra) - return NULL; - - assert(ra->n_ref > 0); - ra->n_ref--; - - if (ra->n_ref > 0) - return NULL; +static sd_radv *radv_free(sd_radv *ra) { + assert(ra); while (ra->prefixes) { sd_radv_prefix *p = ra->prefixes; @@ -125,6 +108,8 @@ _public_ sd_radv *sd_radv_unref(sd_radv *ra) { return mfree(ra); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free); + static int radv_send(sd_radv *ra, const struct in6_addr *dst, const uint32_t router_lifetime) { static const struct ether_addr mac_zero = {}; @@ -746,28 +731,7 @@ _public_ int sd_radv_prefix_new(sd_radv_prefix **ret) { return 0; } -_public_ sd_radv_prefix *sd_radv_prefix_ref(sd_radv_prefix *p) { - if (!p) - return NULL; - - assert(p->n_ref > 0); - p->n_ref++; - - return p; -} - -_public_ sd_radv_prefix *sd_radv_prefix_unref(sd_radv_prefix *p) { - if (!p) - return NULL; - - assert(p->n_ref > 0); - p->n_ref--; - - if (p->n_ref > 0) - return NULL; - - return mfree(p); -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_prefix, sd_radv_prefix, mfree); _public_ int sd_radv_prefix_set_prefix(sd_radv_prefix *p, const struct in6_addr *in6_addr, unsigned char prefixlen) { diff --git a/src/libsystemd/sd-bus/bus-message.c b/src/libsystemd/sd-bus/bus-message.c index f3c9e5691d..6e404367db 100644 --- a/src/libsystemd/sd-bus/bus-message.c +++ b/src/libsystemd/sd-bus/bus-message.c @@ -874,30 +874,7 @@ int bus_message_new_synthetic_error( return 0; } -_public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) { - - if (!m) - return NULL; - - assert(m->n_ref > 0); - m->n_ref++; - - return m; -} - -_public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) { - - if (!m) - return NULL; - - assert(m->n_ref > 0); - m->n_ref--; - - if (m->n_ref > 0) - return NULL; - - return message_free(m); -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_message, sd_bus_message, message_free); _public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) { assert_return(m, -EINVAL); diff --git a/src/libsystemd/sd-bus/bus-slot.c b/src/libsystemd/sd-bus/bus-slot.c index 19ef692e7f..c9aca07f90 100644 --- a/src/libsystemd/sd-bus/bus-slot.c +++ b/src/libsystemd/sd-bus/bus-slot.c @@ -37,17 +37,6 @@ sd_bus_slot *bus_slot_allocate( return slot; } -_public_ sd_bus_slot* sd_bus_slot_ref(sd_bus_slot *slot) { - - if (!slot) - return NULL; - - assert(slot->n_ref > 0); - - slot->n_ref++; - return slot; -} - void bus_slot_disconnect(sd_bus_slot *slot, bool unref) { sd_bus *bus; @@ -187,17 +176,8 @@ void bus_slot_disconnect(sd_bus_slot *slot, bool unref) { sd_bus_slot_unref(slot); } -_public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) { - - if (!slot) - return NULL; - - assert(slot->n_ref > 0); - - if (slot->n_ref > 1) { - slot->n_ref--; - return NULL; - } +static sd_bus_slot* bus_slot_free(sd_bus_slot *slot) { + assert(slot); bus_slot_disconnect(slot, false); @@ -208,6 +188,8 @@ _public_ sd_bus_slot* sd_bus_slot_unref(sd_bus_slot *slot) { return mfree(slot); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_slot, sd_bus_slot, bus_slot_free); + _public_ sd_bus* sd_bus_slot_get_bus(sd_bus_slot *slot) { assert_return(slot, NULL); diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c index d183eb57dc..4c959cc29c 100644 --- a/src/libsystemd/sd-bus/bus-track.c +++ b/src/libsystemd/sd-bus/bus-track.c @@ -143,27 +143,8 @@ _public_ int sd_bus_track_new( return 0; } -_public_ sd_bus_track* sd_bus_track_ref(sd_bus_track *track) { - - if (!track) - return NULL; - - assert(track->n_ref > 0); - - track->n_ref++; - - return track; -} - -_public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { - if (!track) - return NULL; - - assert(track->n_ref > 0); - track->n_ref--; - - if (track->n_ref > 0) - return NULL; +static sd_bus_track *track_free(sd_bus_track *track) { + assert(track); if (track->in_list) LIST_REMOVE(tracks, track->bus->tracks, track); @@ -178,6 +159,8 @@ _public_ sd_bus_track* sd_bus_track_unref(sd_bus_track *track) { return mfree(track); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus_track, sd_bus_track, track_free); + static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { sd_bus_track *track = userdata; const char *name, *old, *new; diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index ba336364d5..7e0d313a08 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -57,38 +57,30 @@ _public_ int sd_device_enumerator_new(sd_device_enumerator **ret) { return 0; } -_public_ sd_device_enumerator *sd_device_enumerator_ref(sd_device_enumerator *enumerator) { - assert_return(enumerator, NULL); - - assert_se((++ enumerator->n_ref) >= 2); - - return enumerator; -} - -_public_ sd_device_enumerator *sd_device_enumerator_unref(sd_device_enumerator *enumerator) { - if (enumerator && (-- enumerator->n_ref) == 0) { - sd_device *device; +static sd_device_enumerator *device_enumerator_free(sd_device_enumerator *enumerator) { + sd_device *device; - while ((device = prioq_pop(enumerator->devices))) - sd_device_unref(device); + assert(enumerator); - prioq_free(enumerator->devices); + while ((device = prioq_pop(enumerator->devices))) + sd_device_unref(device); - set_free_free(enumerator->match_subsystem); - set_free_free(enumerator->nomatch_subsystem); - hashmap_free_free_free(enumerator->match_sysattr); - hashmap_free_free_free(enumerator->nomatch_sysattr); - hashmap_free_free_free(enumerator->match_property); - set_free_free(enumerator->match_sysname); - set_free_free(enumerator->match_tag); - sd_device_unref(enumerator->match_parent); + prioq_free(enumerator->devices); - free(enumerator); - } + set_free_free(enumerator->match_subsystem); + set_free_free(enumerator->nomatch_subsystem); + hashmap_free_free_free(enumerator->match_sysattr); + hashmap_free_free_free(enumerator->nomatch_sysattr); + hashmap_free_free_free(enumerator->match_property); + set_free_free(enumerator->match_sysname); + set_free_free(enumerator->match_tag); + sd_device_unref(enumerator->match_parent); - return NULL; + return mfree(enumerator); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_enumerator, sd_device_enumerator, device_enumerator_free); + _public_ int sd_device_enumerator_add_match_subsystem(sd_device_enumerator *enumerator, const char *subsystem, int match) { Set **set; int r; diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 0b810037a6..1d3999acbd 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -43,39 +43,32 @@ int device_new_aux(sd_device **ret) { return 0; } -_public_ sd_device *sd_device_ref(sd_device *device) { - if (device) - assert_se(++ device->n_ref >= 2); +static sd_device *device_free(sd_device *device) { + assert(device); - return device; + sd_device_unref(device->parent); + free(device->syspath); + free(device->sysname); + free(device->devtype); + free(device->devname); + free(device->subsystem); + free(device->driver_subsystem); + free(device->driver); + free(device->id_filename); + free(device->properties_strv); + free(device->properties_nulstr); + + ordered_hashmap_free_free_free(device->properties); + ordered_hashmap_free_free_free(device->properties_db); + hashmap_free_free_free(device->sysattr_values); + set_free_free(device->sysattrs); + set_free_free(device->tags); + set_free_free(device->devlinks); + + return mfree(device); } -_public_ sd_device *sd_device_unref(sd_device *device) { - if (device && -- device->n_ref == 0) { - sd_device_unref(device->parent); - free(device->syspath); - free(device->sysname); - free(device->devtype); - free(device->devname); - free(device->subsystem); - free(device->driver_subsystem); - free(device->driver); - free(device->id_filename); - free(device->properties_strv); - free(device->properties_nulstr); - - ordered_hashmap_free_free_free(device->properties); - ordered_hashmap_free_free_free(device->properties_db); - hashmap_free_free_free(device->sysattr_values); - set_free_free(device->sysattrs); - set_free_free(device->tags); - set_free_free(device->devlinks); - - free(device); - } - - return NULL; -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device, sd_device, device_free); int device_add_property_aux(sd_device *device, const char *_key, const char *_value, bool db) { OrderedHashmap **properties; diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 6e50fcc4c6..6d55a98ed7 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -456,7 +456,7 @@ static void free_clock_data(struct clock_data *d) { prioq_free(d->latest); } -static void event_free(sd_event *e) { +static sd_event *event_free(sd_event *e) { sd_event_source *s; assert(e); @@ -492,7 +492,8 @@ static void event_free(sd_event *e) { hashmap_free(e->child_sources); set_free(e->post_sources); - free(e); + + return mfree(e); } _public_ int sd_event_new(sd_event** ret) { @@ -553,30 +554,7 @@ fail: return r; } -_public_ sd_event* sd_event_ref(sd_event *e) { - - if (!e) - return NULL; - - assert(e->n_ref >= 1); - e->n_ref++; - - return e; -} - -_public_ sd_event* sd_event_unref(sd_event *e) { - - if (!e) - return NULL; - - assert(e->n_ref >= 1); - e->n_ref--; - - if (e->n_ref <= 0) - event_free(e); - - return NULL; -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event, sd_event, event_free); static bool event_pid_changed(sd_event *e) { assert(e); @@ -1926,46 +1904,31 @@ fail: return r; } -_public_ sd_event_source* sd_event_source_ref(sd_event_source *s) { - +static sd_event_source* event_source_free(sd_event_source *s) { if (!s) return NULL; - assert(s->n_ref >= 1); - s->n_ref++; + /* Here's a special hack: when we are called from a + * dispatch handler we won't free the event source + * immediately, but we will detach the fd from the + * epoll. This way it is safe for the caller to unref + * the event source and immediately close the fd, but + * we still retain a valid event source object after + * the callback. */ - return s; -} - -_public_ sd_event_source* sd_event_source_unref(sd_event_source *s) { - - if (!s) - return NULL; - - assert(s->n_ref >= 1); - s->n_ref--; - - if (s->n_ref <= 0) { - /* Here's a special hack: when we are called from a - * dispatch handler we won't free the event source - * immediately, but we will detach the fd from the - * epoll. This way it is safe for the caller to unref - * the event source and immediately close the fd, but - * we still retain a valid event source object after - * the callback. */ - - if (s->dispatching) { - if (s->type == SOURCE_IO) - source_io_unregister(s); + if (s->dispatching) { + if (s->type == SOURCE_IO) + source_io_unregister(s); - source_disconnect(s); - } else - source_free(s); - } + source_disconnect(s); + } else + source_free(s); return NULL; } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event_source, sd_event_source, event_source_free); + _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) { assert_return(s, -EINVAL); assert_return(!event_pid_changed(s->event), -ECHILD); diff --git a/src/libsystemd/sd-resolve/sd-resolve.c b/src/libsystemd/sd-resolve/sd-resolve.c index 3eb42c59eb..61876781c0 100644 --- a/src/libsystemd/sd-resolve/sd-resolve.c +++ b/src/libsystemd/sd-resolve/sd-resolve.c @@ -543,7 +543,7 @@ _public_ int sd_resolve_get_tid(sd_resolve *resolve, pid_t *tid) { return -ENXIO; } -static void resolve_free(sd_resolve *resolve) { +static sd_resolve *resolve_free(sd_resolve *resolve) { PROTECT_ERRNO; sd_resolve_query *q; unsigned i; @@ -582,30 +582,11 @@ static void resolve_free(sd_resolve *resolve) { /* Close all communication channels */ close_many(resolve->fds, _FD_MAX); - free(resolve); -} - -_public_ sd_resolve* sd_resolve_ref(sd_resolve *resolve) { - assert_return(resolve, NULL); - assert(resolve->n_ref >= 1); - resolve->n_ref++; - - return resolve; + return mfree(resolve); } -_public_ sd_resolve* sd_resolve_unref(sd_resolve *resolve) { - if (!resolve) - return NULL; - - assert(resolve->n_ref >= 1); - resolve->n_ref--; - - if (resolve->n_ref <= 0) - resolve_free(resolve); - - return NULL; -} +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_resolve, sd_resolve, resolve_free); _public_ int sd_resolve_get_fd(sd_resolve *resolve) { assert_return(resolve, -EINVAL); @@ -1070,15 +1051,6 @@ static int getnameinfo_done(sd_resolve_query *q) { return q->getnameinfo_handler(q, q->ret, q->host, q->serv, q->userdata); } -_public_ sd_resolve_query* sd_resolve_query_ref(sd_resolve_query *q) { - assert_return(q, NULL); - - assert(q->n_ref >= 1); - q->n_ref++; - - return q; -} - static void resolve_freeaddrinfo(struct addrinfo *ai) { while (ai) { struct addrinfo *next = ai->ai_next; @@ -1118,7 +1090,7 @@ static void resolve_query_disconnect(sd_resolve_query *q) { sd_resolve_unref(resolve); } -static void resolve_query_free(sd_resolve_query *q) { +static sd_resolve_query *resolve_query_free(sd_resolve_query *q) { assert(q); resolve_query_disconnect(q); @@ -1126,22 +1098,12 @@ static void resolve_query_free(sd_resolve_query *q) { resolve_freeaddrinfo(q->addrinfo); free(q->host); free(q->serv); - free(q); -} - -_public_ sd_resolve_query* sd_resolve_query_unref(sd_resolve_query* q) { - if (!q) - return NULL; - - assert(q->n_ref >= 1); - q->n_ref--; - if (q->n_ref <= 0) - resolve_query_free(q); - - return NULL; + return mfree(q); } +DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_resolve_query, sd_resolve_query, resolve_query_free); + _public_ int sd_resolve_query_is_done(sd_resolve_query *q) { assert_return(q, -EINVAL); assert_return(!resolve_pid_changed(q->resolve), -ECHILD); diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c index 93df63b5c1..72c9db7d07 100644 --- a/src/network/netdev/netdev.c +++ b/src/network/netdev/netdev.c @@ -124,9 +124,8 @@ static void netdev_cancel_callbacks(NetDev *netdev) { } } -static void netdev_free(NetDev *netdev) { - if (!netdev) - return; +static NetDev *netdev_free(NetDev *netdev) { + assert(netdev); netdev_cancel_callbacks(netdev); @@ -157,22 +156,10 @@ static void netdev_free(NetDev *netdev) { NETDEV_VTABLE(netdev)->done) NETDEV_VTABLE(netdev)->done(netdev); - free(netdev); -} - -NetDev *netdev_unref(NetDev *netdev) { - if (netdev && (-- netdev->n_ref <= 0)) - netdev_free(netdev); - - return NULL; + return mfree(netdev); } -NetDev *netdev_ref(NetDev *netdev) { - if (netdev) - assert_se(++ netdev->n_ref >= 2); - - return netdev; -} +DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free); void netdev_drop(NetDev *netdev) { if (!netdev || netdev->state == NETDEV_STATE_LINGER) diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 8152ebef9b..c169ffa160 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -497,14 +497,13 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { return 0; } -static void link_free(Link *link) { +static Link *link_free(Link *link) { Address *address; Link *carrier; Route *route; Iterator i; - if (!link) - return; + assert(link); while ((route = set_first(link->routes))) route_free(route); @@ -569,35 +568,10 @@ static void link_free(Link *link) { hashmap_remove(link->bound_by_links, INT_TO_PTR(carrier->ifindex)); hashmap_free(link->bound_by_links); - free(link); + return mfree(link); } -Link *link_unref(Link *link) { - if (!link) - return NULL; - - assert(link->n_ref > 0); - - link->n_ref--; - - if (link->n_ref > 0) - return NULL; - - link_free(link); - - return NULL; -} - -Link *link_ref(Link *link) { - if (!link) - return NULL; - - assert(link->n_ref > 0); - - link->n_ref++; - - return link; -} +DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free); int link_get(Manager *m, int ifindex, Link **ret) { Link *link; diff --git a/src/resolve/resolved-dns-answer.c b/src/resolve/resolved-dns-answer.c index 66dc5d0299..6767dca302 100644 --- a/src/resolve/resolved-dns-answer.c +++ b/src/resolve/resolved-dns-answer.c @@ -19,15 +19,6 @@ DnsAnswer *dns_answer_new(size_t n) { return a; } -DnsAnswer *dns_answer_ref(DnsAnswer *a) { - if (!a) - return NULL; - - assert(a->n_ref > 0); - a->n_ref++; - return a; -} - static void dns_answer_flush(DnsAnswer *a) { DnsResourceRecord *rr; @@ -40,21 +31,15 @@ static void dns_answer_flush(DnsAnswer *a) { a->n_rrs = 0; } -DnsAnswer *dns_answer_unref(DnsAnswer *a) { - if (!a) - return NULL; - - assert(a->n_ref > 0); - - if (a->n_ref == 1) { - dns_answer_flush(a); - free(a); - } else - a->n_ref--; +static DnsAnswer *dns_answer_free(DnsAnswer *a) { + assert(a); - return NULL; + dns_answer_flush(a); + return mfree(a); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsAnswer, dns_answer, dns_answer_free); + static int dns_answer_add_raw(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags) { assert(rr); diff --git a/src/resolve/resolved-dns-question.c b/src/resolve/resolved-dns-question.c index 68fb3b9eac..1ed9171564 100644 --- a/src/resolve/resolved-dns-question.c +++ b/src/resolve/resolved-dns-question.c @@ -20,33 +20,18 @@ DnsQuestion *dns_question_new(size_t n) { return q; } -DnsQuestion *dns_question_ref(DnsQuestion *q) { - if (!q) - return NULL; - - assert(q->n_ref > 0); - q->n_ref++; - return q; -} - -DnsQuestion *dns_question_unref(DnsQuestion *q) { - if (!q) - return NULL; - - assert(q->n_ref > 0); - - if (q->n_ref == 1) { - size_t i; +static DnsQuestion *dns_question_free(DnsQuestion *q) { + size_t i; - for (i = 0; i < q->n_keys; i++) - dns_resource_key_unref(q->keys[i]); - free(q); - } else - q->n_ref--; + assert(q); - return NULL; + for (i = 0; i < q->n_keys; i++) + dns_resource_key_unref(q->keys[i]); + return mfree(q); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsQuestion, dns_question, dns_question_free); + int dns_question_add(DnsQuestion *q, DnsResourceKey *key) { size_t i; int r; diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index e6d997ed97..67565d267e 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -399,26 +399,8 @@ DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, c return dns_resource_record_new(key); } -DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) { - if (!rr) - return NULL; - - assert(rr->n_ref > 0); - rr->n_ref++; - - return rr; -} - -DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) { - if (!rr) - return NULL; - - assert(rr->n_ref > 0); - - if (rr->n_ref > 1) { - rr->n_ref--; - return NULL; - } +static DnsResourceRecord* dns_resource_record_free(DnsResourceRecord *rr) { + assert(rr); if (rr->key) { switch(rr->key->type) { @@ -512,6 +494,8 @@ DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) { return mfree(rr); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsResourceRecord, dns_resource_record, dns_resource_record_free); + int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) { _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; diff --git a/src/resolve/resolved-dns-search-domain.c b/src/resolve/resolved-dns-search-domain.c index c3817acf11..368ec4da19 100644 --- a/src/resolve/resolved-dns-search-domain.c +++ b/src/resolve/resolved-dns-search-domain.c @@ -65,30 +65,15 @@ int dns_search_domain_new( return 0; } -DnsSearchDomain* dns_search_domain_ref(DnsSearchDomain *d) { - if (!d) - return NULL; - - assert(d->n_ref > 0); - d->n_ref++; - - return d; -} - -DnsSearchDomain* dns_search_domain_unref(DnsSearchDomain *d) { - if (!d) - return NULL; - - assert(d->n_ref > 0); - d->n_ref--; - - if (d->n_ref > 0) - return NULL; +static DnsSearchDomain* dns_search_domain_free(DnsSearchDomain *d) { + assert(d); free(d->name); return mfree(d); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsSearchDomain, dns_search_domain, dns_search_domain_free); + void dns_search_domain_unlink(DnsSearchDomain *d) { assert(d); assert(d->manager); diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index 24164362c4..be0ae7a731 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -98,25 +98,8 @@ int dns_server_new( return 0; } -DnsServer* dns_server_ref(DnsServer *s) { - if (!s) - return NULL; - - assert(s->n_ref > 0); - s->n_ref++; - - return s; -} - -DnsServer* dns_server_unref(DnsServer *s) { - if (!s) - return NULL; - - assert(s->n_ref > 0); - s->n_ref--; - - if (s->n_ref > 0) - return NULL; +static DnsServer* dns_server_free(DnsServer *s) { + assert(s); dns_stream_unref(s->stream); @@ -128,6 +111,8 @@ DnsServer* dns_server_unref(DnsServer *s) { return mfree(s); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsServer, dns_server, dns_server_free); + void dns_server_unlink(DnsServer *s) { assert(s); assert(s->manager); diff --git a/src/resolve/resolved-dns-stream.c b/src/resolve/resolved-dns-stream.c index c3ffc75930..9a5c7c34f3 100644 --- a/src/resolve/resolved-dns-stream.c +++ b/src/resolve/resolved-dns-stream.c @@ -424,18 +424,11 @@ static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *use return 0; } -DnsStream *dns_stream_unref(DnsStream *s) { +static DnsStream *dns_stream_free(DnsStream *s) { DnsPacket *p; Iterator i; - if (!s) - return NULL; - - assert(s->n_ref > 0); - s->n_ref--; - - if (s->n_ref > 0) - return NULL; + assert(s); dns_stream_stop(s); @@ -464,15 +457,7 @@ DnsStream *dns_stream_unref(DnsStream *s) { return mfree(s); } -DnsStream *dns_stream_ref(DnsStream *s) { - if (!s) - return NULL; - - assert(s->n_ref > 0); - s->n_ref++; - - return s; -} +DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsStream, dns_stream, dns_stream_free); int dns_stream_new(Manager *m, DnsStream **ret, DnsProtocol protocol, int fd, const union sockaddr_union *tfo_address) { _cleanup_(dns_stream_unrefp) DnsStream *s = NULL; diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index 00a5eff670..52cf461109 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -56,15 +56,8 @@ static const char* const image_search_path[_IMAGE_CLASS_MAX] = { "/usr/lib/portables\0", }; -Image *image_unref(Image *i) { - if (!i) - return NULL; - - assert(i->n_ref > 0); - i->n_ref--; - - if (i->n_ref > 0) - return NULL; +static Image *image_free(Image *i) { + assert(i); free(i->name); free(i->path); @@ -76,15 +69,7 @@ Image *image_unref(Image *i) { return mfree(i); } -Image *image_ref(Image *i) { - if (!i) - return NULL; - - assert(i->n_ref > 0); - i->n_ref++; - - return i; -} +DEFINE_TRIVIAL_REF_UNREF_FUNC(Image, image, image_free); static char **image_settings_path(Image *image) { _cleanup_strv_free_ char **l = NULL; |