From 8f71a0d1634b6878249072563eb595e92ae312bf Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 28 Aug 2018 14:01:35 +0900 Subject: [PATCH] udev-ctrl: use DEFINE_TRIVIAL_REF_FUNC() macro or friends --- src/udev/udev-ctrl.c | 67 +++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/src/udev/udev-ctrl.c b/src/udev/udev-ctrl.c index 0f2aa33512..a9b19051bf 100644 --- a/src/udev/udev-ctrl.c +++ b/src/udev/udev-ctrl.c @@ -49,13 +49,13 @@ struct udev_ctrl_msg_wire { }; struct udev_ctrl_msg { - int refcount; + unsigned n_ref; struct udev_ctrl_connection *conn; struct udev_ctrl_msg_wire ctrl_msg_wire; }; struct udev_ctrl { - int refcount; + unsigned n_ref; int sock; union sockaddr_union saddr; socklen_t addrlen; @@ -65,7 +65,7 @@ struct udev_ctrl { }; struct udev_ctrl_connection { - int refcount; + unsigned n_ref; struct udev_ctrl *uctrl; int sock; }; @@ -78,7 +78,7 @@ struct udev_ctrl *udev_ctrl_new_from_fd(int fd) { uctrl = new0(struct udev_ctrl, 1); if (uctrl == NULL) return NULL; - uctrl->refcount = 1; + uctrl->n_ref = 1; if (fd < 0) { uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); @@ -133,22 +133,15 @@ int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) { return 0; } -static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) { - if (uctrl) - uctrl->refcount++; +static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) { + assert(uctrl); - return uctrl; + safe_close(uctrl->sock); + return mfree(uctrl); } -struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) { - if (uctrl && -- uctrl->refcount == 0) { - if (uctrl->sock >= 0) - close(uctrl->sock); - free(uctrl); - } - - return NULL; -} +DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl); +DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free); int udev_ctrl_cleanup(struct udev_ctrl *uctrl) { if (uctrl == NULL) @@ -173,7 +166,7 @@ struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) { conn = new(struct udev_ctrl_connection, 1); if (conn == NULL) return NULL; - conn->refcount = 1; + conn->n_ref = 1; conn->uctrl = uctrl; conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK); @@ -207,26 +200,16 @@ err: return mfree(conn); } -struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) { - if (conn == NULL) - return NULL; - conn->refcount++; - return conn; -} - -struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) { - if (conn && -- conn->refcount == 0) { - if (conn->sock >= 0) - close(conn->sock); +static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) { + assert(conn); - udev_ctrl_unref(conn->uctrl); - - free(conn); - } - - return NULL; + safe_close(conn->sock); + udev_ctrl_unref(conn->uctrl); + return mfree(conn); } +DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free); + static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) { struct udev_ctrl_msg_wire ctrl_msg_wire; int err = 0; @@ -330,7 +313,7 @@ struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) { uctrl_msg = new0(struct udev_ctrl_msg, 1); if (uctrl_msg == NULL) return NULL; - uctrl_msg->refcount = 1; + uctrl_msg->n_ref = 1; uctrl_msg->conn = conn; udev_ctrl_connection_ref(conn); @@ -396,15 +379,15 @@ err: return NULL; } -struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) { - if (ctrl_msg && -- ctrl_msg->refcount == 0) { - udev_ctrl_connection_unref(ctrl_msg->conn); - free(ctrl_msg); - } +static struct udev_ctrl_msg *udev_ctrl_msg_free(struct udev_ctrl_msg *ctrl_msg) { + assert(ctrl_msg); - return NULL; + udev_ctrl_connection_unref(ctrl_msg->conn); + return mfree(ctrl_msg); } +DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg, udev_ctrl_msg, udev_ctrl_msg_free); + int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) { if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL) return ctrl_msg->ctrl_msg_wire.intval; -- 2.25.1