udev-ctrl: use DEFINE_TRIVIAL_REF_FUNC() macro or friends
This commit is contained in:
parent
2024ed616e
commit
8f71a0d163
|
@ -49,13 +49,13 @@ struct udev_ctrl_msg_wire {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct udev_ctrl_msg {
|
struct udev_ctrl_msg {
|
||||||
int refcount;
|
unsigned n_ref;
|
||||||
struct udev_ctrl_connection *conn;
|
struct udev_ctrl_connection *conn;
|
||||||
struct udev_ctrl_msg_wire ctrl_msg_wire;
|
struct udev_ctrl_msg_wire ctrl_msg_wire;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct udev_ctrl {
|
struct udev_ctrl {
|
||||||
int refcount;
|
unsigned n_ref;
|
||||||
int sock;
|
int sock;
|
||||||
union sockaddr_union saddr;
|
union sockaddr_union saddr;
|
||||||
socklen_t addrlen;
|
socklen_t addrlen;
|
||||||
|
@ -65,7 +65,7 @@ struct udev_ctrl {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct udev_ctrl_connection {
|
struct udev_ctrl_connection {
|
||||||
int refcount;
|
unsigned n_ref;
|
||||||
struct udev_ctrl *uctrl;
|
struct udev_ctrl *uctrl;
|
||||||
int sock;
|
int sock;
|
||||||
};
|
};
|
||||||
|
@ -78,7 +78,7 @@ struct udev_ctrl *udev_ctrl_new_from_fd(int fd) {
|
||||||
uctrl = new0(struct udev_ctrl, 1);
|
uctrl = new0(struct udev_ctrl, 1);
|
||||||
if (uctrl == NULL)
|
if (uctrl == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
uctrl->refcount = 1;
|
uctrl->n_ref = 1;
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) {
|
static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
|
||||||
if (uctrl)
|
assert(uctrl);
|
||||||
uctrl->refcount++;
|
|
||||||
|
|
||||||
return uctrl;
|
safe_close(uctrl->sock);
|
||||||
|
return mfree(uctrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) {
|
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl);
|
||||||
if (uctrl && -- uctrl->refcount == 0) {
|
DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
|
||||||
if (uctrl->sock >= 0)
|
|
||||||
close(uctrl->sock);
|
|
||||||
free(uctrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
|
int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
|
||||||
if (uctrl == NULL)
|
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);
|
conn = new(struct udev_ctrl_connection, 1);
|
||||||
if (conn == NULL)
|
if (conn == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
conn->refcount = 1;
|
conn->n_ref = 1;
|
||||||
conn->uctrl = uctrl;
|
conn->uctrl = uctrl;
|
||||||
|
|
||||||
conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
|
conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
|
||||||
|
@ -207,25 +200,15 @@ err:
|
||||||
return mfree(conn);
|
return mfree(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) {
|
static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) {
|
||||||
if (conn == NULL)
|
assert(conn);
|
||||||
return NULL;
|
|
||||||
conn->refcount++;
|
safe_close(conn->sock);
|
||||||
return conn;
|
udev_ctrl_unref(conn->uctrl);
|
||||||
|
return mfree(conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) {
|
DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
|
||||||
if (conn && -- conn->refcount == 0) {
|
|
||||||
if (conn->sock >= 0)
|
|
||||||
close(conn->sock);
|
|
||||||
|
|
||||||
udev_ctrl_unref(conn->uctrl);
|
|
||||||
|
|
||||||
free(conn);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
|
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;
|
struct udev_ctrl_msg_wire ctrl_msg_wire;
|
||||||
|
@ -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);
|
uctrl_msg = new0(struct udev_ctrl_msg, 1);
|
||||||
if (uctrl_msg == NULL)
|
if (uctrl_msg == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
uctrl_msg->refcount = 1;
|
uctrl_msg->n_ref = 1;
|
||||||
uctrl_msg->conn = conn;
|
uctrl_msg->conn = conn;
|
||||||
udev_ctrl_connection_ref(conn);
|
udev_ctrl_connection_ref(conn);
|
||||||
|
|
||||||
|
@ -396,15 +379,15 @@ err:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) {
|
static struct udev_ctrl_msg *udev_ctrl_msg_free(struct udev_ctrl_msg *ctrl_msg) {
|
||||||
if (ctrl_msg && -- ctrl_msg->refcount == 0) {
|
assert(ctrl_msg);
|
||||||
udev_ctrl_connection_unref(ctrl_msg->conn);
|
|
||||||
free(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) {
|
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)
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
|
||||||
return ctrl_msg->ctrl_msg_wire.intval;
|
return ctrl_msg->ctrl_msg_wire.intval;
|
||||||
|
|
Loading…
Reference in a new issue