chiark / gitweb /
libudev: always return NULL in _unref() APIs
authorLennart Poettering <lennart@poettering.net>
Tue, 19 Nov 2013 00:15:31 +0000 (01:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 20 Nov 2013 18:36:14 +0000 (19:36 +0100)
Returning anything else but NULL would suggest the caller's reference
might still be valid, but it isn't, because the caller just invoked
_unref() after all.

This turns the return value into a typesafe shortcut that allows
unreffing and resetting a reference in one line. In contrast to
solutions for this which take a pointer to a pointer to accomplish the
same this solution is just syntactic sugar the developer can make use of
but doesn't have to, and this is particularly useful when immediately
unreffing objects returned by function calls.

src/libudev/libudev-device.c
src/libudev/libudev-enumerate.c
src/libudev/libudev-hwdb.c
src/libudev/libudev-monitor.c
src/libudev/libudev-queue.c

index b5b07fc5de0cd7c90e1356a35c11ccf022ee683a..059a590601d96d01f1119002baa626b15029be40 100644 (file)
@@ -1090,7 +1090,7 @@ _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
  * Drop a reference of a udev device. If the refcount reaches zero,
  * the resources of the device will be released.
  *
  * Drop a reference of a udev device. If the refcount reaches zero,
  * the resources of the device will be released.
  *
- * Returns: the passed udev device if it has still an active reference, or #NULL otherwise.
+ * Returns: #NULL
  **/
 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
 {
  **/
 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
 {
@@ -1098,7 +1098,7 @@ _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
                 return NULL;
         udev_device->refcount--;
         if (udev_device->refcount > 0)
                 return NULL;
         udev_device->refcount--;
         if (udev_device->refcount > 0)
-                return udev_device;
+                return NULL;
         if (udev_device->parent_device != NULL)
                 udev_device_unref(udev_device->parent_device);
         free(udev_device->syspath);
         if (udev_device->parent_device != NULL)
                 udev_device_unref(udev_device->parent_device);
         free(udev_device->syspath);
index e71d766c0283a0755031ecec1786c00983e42f97..48ffe83d364359204fdb575714a45dd077a3410f 100644 (file)
@@ -122,7 +122,7 @@ _public_ struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_e
  * Drop a reference of an enumeration context. If the refcount reaches zero,
  * all resources of the enumeration context will be released.
  *
  * Drop a reference of an enumeration context. If the refcount reaches zero,
  * all resources of the enumeration context will be released.
  *
- * Returns: the passed enumeration context if it has still an active reference, or #NULL otherwise.
+ * Returns: #NULL
  **/
 _public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
 {
  **/
 _public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
 {
@@ -132,7 +132,7 @@ _public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev
                 return NULL;
         udev_enumerate->refcount--;
         if (udev_enumerate->refcount > 0)
                 return NULL;
         udev_enumerate->refcount--;
         if (udev_enumerate->refcount > 0)
-                return udev_enumerate;
+                return NULL;
         udev_list_cleanup(&udev_enumerate->sysattr_match_list);
         udev_list_cleanup(&udev_enumerate->sysattr_nomatch_list);
         udev_list_cleanup(&udev_enumerate->subsystem_match_list);
         udev_list_cleanup(&udev_enumerate->sysattr_match_list);
         udev_list_cleanup(&udev_enumerate->sysattr_nomatch_list);
         udev_list_cleanup(&udev_enumerate->subsystem_match_list);
index de1cb831885016b3236d74be66fe4b289afc39b4..b53b35ce4a754c80e30cf3f01e934632775ddb0f 100644 (file)
@@ -334,14 +334,14 @@ _public_ struct udev_hwdb *udev_hwdb_ref(struct udev_hwdb *hwdb) {
  * Drop a reference of a hwdb context. If the refcount reaches zero,
  * all resources of the hwdb context will be released.
  *
  * Drop a reference of a hwdb context. If the refcount reaches zero,
  * all resources of the hwdb context will be released.
  *
- * Returns: the passed hwdb context if it has still an active reference, or #NULL otherwise.
+ * Returns: #NULL
  **/
 _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
         if (!hwdb)
                 return NULL;
         hwdb->refcount--;
         if (hwdb->refcount > 0)
  **/
 _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) {
         if (!hwdb)
                 return NULL;
         hwdb->refcount--;
         if (hwdb->refcount > 0)
-                return hwdb;
+                return NULL;
         if (hwdb->map)
                 munmap((void *)hwdb->map, hwdb->st.st_size);
         if (hwdb->f)
         if (hwdb->map)
                 munmap((void *)hwdb->map, hwdb->st.st_size);
         if (hwdb->f)
index 24efdc65e2908c6cb83f476e4be7ba7a914a70cf..56a6c5e39a6c1cdfed19a37c7e33fadec3859de9 100644 (file)
@@ -408,7 +408,7 @@ _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor
  * the bound socket will be closed, and the resources of the monitor
  * will be released.
  *
  * the bound socket will be closed, and the resources of the monitor
  * will be released.
  *
- * Returns: the passed udev monitor if it has still an active reference, or #NULL otherwise.
+ * Returns: #NULL
  **/
 _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
 {
  **/
 _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
 {
@@ -416,7 +416,7 @@ _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monit
                 return NULL;
         udev_monitor->refcount--;
         if (udev_monitor->refcount > 0)
                 return NULL;
         udev_monitor->refcount--;
         if (udev_monitor->refcount > 0)
-                return udev_monitor;
+                return NULL;
         if (udev_monitor->sock >= 0)
                 close(udev_monitor->sock);
         udev_list_cleanup(&udev_monitor->filter_subsystem_list);
         if (udev_monitor->sock >= 0)
                 close(udev_monitor->sock);
         udev_list_cleanup(&udev_monitor->filter_subsystem_list);
index 0dd20313d9fe892dad73a1256b83041bd55bc002..f67dba99584ab2dad1f74a7629924fcf14c144e5 100644 (file)
@@ -101,7 +101,7 @@ _public_ struct udev_queue *udev_queue_ref(struct udev_queue *udev_queue)
  * Drop a reference of a udev queue context. If the refcount reaches zero,
  * the resources of the queue context will be released.
  *
  * Drop a reference of a udev queue context. If the refcount reaches zero,
  * the resources of the queue context will be released.
  *
- * Returns: the passed queue context if it has still an active reference, or #NULL otherwise.
+ * Returns: #NULL
  **/
 _public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
 {
  **/
 _public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
 {
@@ -109,7 +109,7 @@ _public_ struct udev_queue *udev_queue_unref(struct udev_queue *udev_queue)
                 return NULL;
         udev_queue->refcount--;
         if (udev_queue->refcount > 0)
                 return NULL;
         udev_queue->refcount--;
         if (udev_queue->refcount > 0)
-                return udev_queue;
+                return NULL;
         udev_list_cleanup(&udev_queue->queue_list);
         free(udev_queue);
         return NULL;
         udev_list_cleanup(&udev_queue->queue_list);
         free(udev_queue);
         return NULL;