Use _cleanup_ functions for udev/udev_device

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2015-10-13 08:33:55 +10:00
parent 2d7f874a57
commit 31dc1f4772
3 changed files with 19 additions and 11 deletions

View File

@@ -203,7 +203,6 @@ __ratbag_find_hidraw(struct ratbag_device *device,
struct ratbag *ratbag = device->ratbag;
struct udev_enumerate *e;
struct udev_list_entry *entry;
struct udev_device *udev_device;
const char *path;
struct udev_device *hid_udev;
struct udev_device *parent_udev;
@@ -234,6 +233,8 @@ __ratbag_find_hidraw(struct ratbag_device *device,
udev_enumerate_add_match_parent(e, parent_udev);
udev_enumerate_scan_devices(e);
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
_cleanup_udev_device_unref_ struct udev_device *udev_device;
path = udev_list_entry_get_name(entry);
udev_device = udev_device_new_from_syspath(udev, path);
if (!udev_device)
@@ -246,13 +247,11 @@ __ratbag_find_hidraw(struct ratbag_device *device,
matched = match(device);
if (matched == 1) {
rc = 0;
udev_device_unref(udev_device);
goto out;
}
skip:
ratbag_close_hidraw(device);
udev_device_unref(udev_device);
rc = -ENODEV;
}

View File

@@ -85,8 +85,22 @@ cleanup_free(void *p) {
free(*(void**)p);
}
static inline void
cleanup_udev_unref(struct udev **udev) {
if (*udev)
udev_unref(*udev);
}
static inline void
cleanup_udev_device_unref(struct udev_device **udev_device) {
if (*udev_device)
udev_device_unref(*udev_device);
}
#define _cleanup_(x) __attribute__((cleanup(x)))
#define _cleanup_free_ _cleanup_(cleanup_free)
#define _cleanup_udev_unref_ _cleanup_(cleanup_udev_unref)
#define _cleanup_udev_device_unref_ _cleanup_(cleanup_udev_device_unref)
static inline char*
strncpy_safe(char *dest, const char *src, size_t n)

View File

@@ -220,21 +220,16 @@ struct ratbag_device *
ratbag_cmd_open_device(struct ratbag *ratbag, const char *path)
{
struct ratbag_device *device;
struct udev *udev;
struct udev_device *udev_device;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
udev = udev_new();
udev_device = udev_device_from_path(udev, path);
if (!udev_device) {
udev_unref(udev);
if (!udev_device)
return NULL;
}
device = ratbag_device_new_from_udev_device(ratbag, udev_device);
udev_device_unref(udev_device);
udev_unref(udev);
return device;
}