summaryrefslogtreecommitdiffstats
path: root/src/libudev
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2018-08-28 06:07:48 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-09-11 05:45:21 +0200
commit3c6ac219296aaba8ef99644b1185326ed114392d (patch)
treeea1936f89e4bc940588e9b1fa1631605e7cbcceb /src/libudev
parentlibudev-monitor: use safe_close() in udev_monitor_disconnect() (diff)
downloadsystemd-3c6ac219296aaba8ef99644b1185326ed114392d.tar.xz
systemd-3c6ac219296aaba8ef99644b1185326ed114392d.zip
libudev: use DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC() macro where applicable
Diffstat (limited to 'src/libudev')
-rw-r--r--src/libudev/libudev-device-internal.h2
-rw-r--r--src/libudev/libudev-device.c40
-rw-r--r--src/libudev/libudev-enumerate.c28
-rw-r--r--src/libudev/libudev-hwdb.c29
-rw-r--r--src/libudev/libudev-monitor.c34
-rw-r--r--src/libudev/libudev-queue.c33
-rw-r--r--src/libudev/libudev.c23
7 files changed, 73 insertions, 116 deletions
diff --git a/src/libudev/libudev-device-internal.h b/src/libudev/libudev-device-internal.h
index 45763f2c99..974601f79f 100644
--- a/src/libudev/libudev-device-internal.h
+++ b/src/libudev/libudev-device-internal.h
@@ -18,7 +18,7 @@ struct udev_device {
sd_device *device;
/* legacy */
- int refcount;
+ unsigned n_ref;
struct udev_device *parent;
bool parent_set;
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c
index e6d6f70510..ce8acd59b2 100644
--- a/src/libudev/libudev-device.c
+++ b/src/libudev/libudev-device.c
@@ -201,7 +201,7 @@ struct udev_device *udev_device_new(struct udev *udev) {
errno = ENOMEM;
return NULL;
}
- udev_device->refcount = 1;
+ udev_device->n_ref = 1;
udev_device->udev = udev;
udev_list_init(udev, &udev_device->properties, true);
udev_list_init(udev, &udev_device->tags, true);
@@ -501,6 +501,20 @@ _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
return udev_device->udev;
}
+static struct udev_device *udev_device_free(struct udev_device *udev_device) {
+ assert(udev_device);
+
+ sd_device_unref(udev_device->device);
+ udev_device_unref(udev_device->parent);
+
+ udev_list_cleanup(&udev_device->properties);
+ udev_list_cleanup(&udev_device->sysattrs);
+ udev_list_cleanup(&udev_device->tags);
+ udev_list_cleanup(&udev_device->devlinks);
+
+ return mfree(udev_device);
+}
+
/**
* udev_device_ref:
* @udev_device: udev device
@@ -509,13 +523,6 @@ _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
*
* Returns: the passed udev device
**/
-_public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
-{
- if (udev_device)
- udev_device->refcount++;
-
- return udev_device;
-}
/**
* udev_device_unref:
@@ -526,22 +533,7 @@ _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
*
* Returns: #NULL
**/
-_public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
-{
- if (udev_device && (-- udev_device->refcount) == 0) {
- sd_device_unref(udev_device->device);
- udev_device_unref(udev_device->parent);
-
- udev_list_cleanup(&udev_device->properties);
- udev_list_cleanup(&udev_device->sysattrs);
- udev_list_cleanup(&udev_device->tags);
- udev_list_cleanup(&udev_device->devlinks);
-
- free(udev_device);
- }
-
- return NULL;
-}
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_device, udev_device, udev_device_free);
/**
* udev_device_get_devpath:
diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c
index e9f0f2e47e..6b0e5850bd 100644
--- a/src/libudev/libudev-enumerate.c
+++ b/src/libudev/libudev-enumerate.c
@@ -33,7 +33,7 @@
*/
struct udev_enumerate {
struct udev *udev;
- int refcount;
+ unsigned n_ref;
struct udev_list devices_list;
bool devices_uptodate:1;
@@ -70,7 +70,7 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
return NULL;
}
- udev_enumerate->refcount = 1;
+ udev_enumerate->n_ref = 1;
udev_enumerate->udev = udev;
udev_list_init(udev, &udev_enumerate->devices_list, false);
@@ -78,6 +78,14 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
return TAKE_PTR(udev_enumerate);
}
+static struct udev_enumerate *udev_enumerate_free(struct udev_enumerate *udev_enumerate) {
+ assert(udev_enumerate);
+
+ udev_list_cleanup(&udev_enumerate->devices_list);
+ sd_device_enumerator_unref(udev_enumerate->enumerator);
+ return mfree(udev_enumerate);
+}
+
/**
* udev_enumerate_ref:
* @udev_enumerate: context
@@ -86,12 +94,6 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev) {
*
* Returns: the passed enumeration context
**/
-_public_ struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate) {
- if (udev_enumerate)
- udev_enumerate->refcount++;
-
- return udev_enumerate;
-}
/**
* udev_enumerate_unref:
@@ -102,15 +104,7 @@ _public_ struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_e
*
* Returns: #NULL
**/
-_public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate) {
- if (udev_enumerate && (-- udev_enumerate->refcount) == 0) {
- udev_list_cleanup(&udev_enumerate->devices_list);
- sd_device_enumerator_unref(udev_enumerate->enumerator);
- free(udev_enumerate);
- }
-
- return NULL;
-}
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_enumerate, udev_enumerate, udev_enumerate_free);
/**
* udev_enumerate_get_udev:
diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c
index d13cc4dfb5..a6e7ce4b85 100644
--- a/src/libudev/libudev-hwdb.c
+++ b/src/libudev/libudev-hwdb.c
@@ -20,7 +20,7 @@
*/
struct udev_hwdb {
struct udev *udev;
- int refcount;
+ unsigned n_ref;
sd_hwdb *hwdb;
@@ -52,7 +52,7 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
return NULL;
}
- hwdb->refcount = 1;
+ hwdb->n_ref = 1;
hwdb->hwdb = TAKE_PTR(hwdb_internal);
udev_list_init(udev, &hwdb->properties_list, true);
@@ -60,6 +60,14 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
return hwdb;
}
+static struct udev_hwdb *udev_hwdb_free(struct udev_hwdb *hwdb) {
+ assert(hwdb);
+
+ sd_hwdb_unref(hwdb->hwdb);
+ udev_list_cleanup(&hwdb->properties_list);
+ return mfree(hwdb);
+}
+
/**
* udev_hwdb_ref:
* @hwdb: context
@@ -68,12 +76,6 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) {
*
* Returns: the passed enumeration context
**/
-_public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
- if (!hwdb)
- return NULL;
- hwdb->refcount++;
- return hwdb;
-}
/**
* udev_hwdb_unref:
@@ -84,16 +86,7 @@ _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
*
* Returns: #NULL
**/
-_public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
- if (!hwdb)
- return NULL;
- hwdb->refcount--;
- if (hwdb->refcount > 0)
- return NULL;
- sd_hwdb_unref(hwdb->hwdb);
- udev_list_cleanup(&hwdb->properties_list);
- return mfree(hwdb);
-}
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_hwdb, udev_hwdb, udev_hwdb_free);
/**
* udev_hwdb_get_properties_list_entry:
diff --git a/src/libudev/libudev-monitor.c b/src/libudev/libudev-monitor.c
index fe4595930f..f93854dc45 100644
--- a/src/libudev/libudev-monitor.c
+++ b/src/libudev/libudev-monitor.c
@@ -38,7 +38,7 @@
*/
struct udev_monitor {
struct udev *udev;
- int refcount;
+ unsigned n_ref;
int sock;
union sockaddr_union snl;
union sockaddr_union snl_trusted_sender;
@@ -87,7 +87,7 @@ static struct udev_monitor *udev_monitor_new(struct udev *udev) {
errno = ENOMEM;
return NULL;
}
- udev_monitor->refcount = 1;
+ udev_monitor->n_ref = 1;
udev_monitor->udev = udev;
udev_list_init(udev, &udev_monitor->filter_subsystem_list, false);
udev_list_init(udev, &udev_monitor->filter_tag_list, true);
@@ -432,6 +432,15 @@ int udev_monitor_disconnect(struct udev_monitor *udev_monitor) {
return 0;
}
+static struct udev_monitor *udev_monitor_free(struct udev_monitor *udev_monitor) {
+ assert(udev_monitor);
+
+ udev_monitor_disconnect(udev_monitor);
+ udev_list_cleanup(&udev_monitor->filter_subsystem_list);
+ udev_list_cleanup(&udev_monitor->filter_tag_list);
+ return mfree(udev_monitor);
+}
+
/**
* udev_monitor_ref:
* @udev_monitor: udev monitor
@@ -440,13 +449,6 @@ int udev_monitor_disconnect(struct udev_monitor *udev_monitor) {
*
* Returns: the passed udev monitor
**/
-_public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
-{
- if (udev_monitor == NULL)
- return NULL;
- udev_monitor->refcount++;
- return udev_monitor;
-}
/**
* udev_monitor_unref:
@@ -458,19 +460,7 @@ _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor
*
* Returns: #NULL
**/
-_public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
-{
- if (udev_monitor == NULL)
- return NULL;
- udev_monitor->refcount--;
- if (udev_monitor->refcount > 0)
- return NULL;
- if (udev_monitor->sock >= 0)
- close(udev_monitor->sock);
- udev_list_cleanup(&udev_monitor->filter_subsystem_list);
- udev_list_cleanup(&udev_monitor->filter_tag_list);
- return mfree(udev_monitor);
-}
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_monitor, udev_monitor, udev_monitor_free);
/**
* udev_monitor_get_udev:
diff --git a/src/libudev/libudev-queue.c b/src/libudev/libudev-queue.c
index c45abfd520..4c60c886e7 100644
--- a/src/libudev/libudev-queue.c
+++ b/src/libudev/libudev-queue.c
@@ -28,7 +28,7 @@
*/
struct udev_queue {
struct udev *udev;
- int refcount;
+ unsigned n_ref;
int fd;
};
@@ -51,12 +51,19 @@ _public_ struct udev_queue *udev_queue_new(struct udev *udev)
return NULL;
}
- udev_queue->refcount = 1;
+ udev_queue->n_ref = 1;
udev_queue->udev = udev;
udev_queue->fd = -1;
return udev_queue;
}
+static struct udev_queue *udev_queue_free(struct udev_queue *udev_queue) {
+ assert(udev_queue);
+
+ safe_close(udev_queue->fd);
+ return mfree(udev_queue);
+}
+
/**
* udev_queue_ref:
* @udev_queue: udev queue context
@@ -65,14 +72,6 @@ _public_ struct udev_queue *udev_queue_new(struct udev *udev)
*
* Returns: the same udev queue context.
**/
-_public_ struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue)
-{
- if (udev_queue == NULL)
- return NULL;
-
- udev_queue->refcount++;
- return udev_queue;
-}
/**
* udev_queue_unref:
@@ -83,19 +82,7 @@ _public_ struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue)
*
* Returns: #NULL
**/
-_public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
-{
- if (udev_queue == NULL)
- return NULL;
-
- udev_queue->refcount--;
- if (udev_queue->refcount > 0)
- return NULL;
-
- safe_close(udev_queue->fd);
-
- return mfree(udev_queue);
-}
+DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_queue, udev_queue, udev_queue_free);
/**
* udev_queue_get_udev:
diff --git a/src/libudev/libudev.c b/src/libudev/libudev.c
index 8af97fe16d..1df0968780 100644
--- a/src/libudev/libudev.c
+++ b/src/libudev/libudev.c
@@ -29,7 +29,7 @@
* Opaque object representing the library context.
*/
struct udev {
- int refcount;
+ unsigned n_ref;
void (*log_fn)(struct udev *udev,
int priority, const char *file, int line, const char *fn,
const char *format, va_list args);
@@ -82,7 +82,7 @@ _public_ struct udev *udev_new(void) {
errno = ENOMEM;
return NULL;
}
- udev->refcount = 1;
+ udev->n_ref = 1;
return udev;
}
@@ -95,12 +95,7 @@ _public_ struct udev *udev_new(void) {
*
* Returns: the passed udev library context
**/
-_public_ struct udev *udev_ref(struct udev *udev) {
- if (udev == NULL)
- return NULL;
- udev->refcount++;
- return udev;
-}
+DEFINE_PUBLIC_TRIVIAL_REF_FUNC(struct udev, udev);
/**
* udev_unref:
@@ -112,11 +107,17 @@ _public_ struct udev *udev_ref(struct udev *udev) {
* Returns: the passed udev library context if it has still an active reference, or #NULL otherwise.
**/
_public_ struct udev *udev_unref(struct udev *udev) {
- if (udev == NULL)
+ if (!udev)
return NULL;
- udev->refcount--;
- if (udev->refcount > 0)
+
+ assert(udev->n_ref > 0);
+ udev->n_ref--;
+ if (udev->n_ref > 0)
+ /* This is different from our convetion, but let's keep backward
+ * compatibility. So, do not use DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC()
+ * macro to define this function. */
return udev;
+
return mfree(udev);
}