diff options
Diffstat (limited to 'src/libudev/libudev-hwdb.c')
-rw-r--r-- | src/libudev/libudev-hwdb.c | 79 |
1 files changed, 31 insertions, 48 deletions
diff --git a/src/libudev/libudev-hwdb.c b/src/libudev/libudev-hwdb.c index 45322677e9..ed755e5d3c 100644 --- a/src/libudev/libudev-hwdb.c +++ b/src/libudev/libudev-hwdb.c @@ -1,10 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include <errno.h> + #include "sd-hwdb.h" #include "alloc-util.h" #include "hwdb-util.h" -#include "libudev-private.h" +#include "libudev-list-internal.h" /** * SECTION:libudev-hwdb @@ -19,17 +21,14 @@ * Opaque object representing the hardware database. */ struct udev_hwdb { - struct udev *udev; - int refcount; - + unsigned n_ref; sd_hwdb *hwdb; - struct udev_list properties_list; }; /** * udev_hwdb_new: - * @udev: udev library context + * @udev: udev library context (unused) * * Create a hardware database context to query properties for devices. * @@ -40,28 +39,32 @@ _public_ struct udev_hwdb *udev_hwdb_new(struct udev *udev) { struct udev_hwdb *hwdb; int r; - assert_return_errno(udev, NULL, EINVAL); - r = sd_hwdb_new(&hwdb_internal); - if (r < 0) { - errno = -r; - return NULL; - } + if (r < 0) + return_with_errno(NULL, r); - hwdb = new0(struct udev_hwdb, 1); - if (!hwdb) { - errno = ENOMEM; - return NULL; - } + hwdb = new(struct udev_hwdb, 1); + if (!hwdb) + return_with_errno(NULL, ENOMEM); - hwdb->refcount = 1; - hwdb->hwdb = TAKE_PTR(hwdb_internal); + *hwdb = (struct udev_hwdb) { + .n_ref = 1, + .hwdb = TAKE_PTR(hwdb_internal), + }; - udev_list_init(udev, &hwdb->properties_list, true); + udev_list_init(&hwdb->properties_list, true); 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 @@ -70,12 +73,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: @@ -86,16 +83,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: @@ -110,27 +98,22 @@ _public_ struct udev_hwdb *udev_hwdb_unref(struct udev_hwdb *hwdb) { * * Returns: a udev_list_entry. */ -_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned int flags) { +_public_ struct udev_list_entry *udev_hwdb_get_properties_list_entry(struct udev_hwdb *hwdb, const char *modalias, unsigned flags) { const char *key, *value; struct udev_list_entry *e; - if (!hwdb || !modalias) { - errno = EINVAL; - return NULL; - } + assert_return_errno(hwdb, NULL, EINVAL); + assert_return_errno(modalias, NULL, EINVAL); udev_list_cleanup(&hwdb->properties_list); - SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) { - if (udev_list_entry_add(&hwdb->properties_list, key, value) == NULL) { - errno = ENOMEM; - return NULL; - } - } + SD_HWDB_FOREACH_PROPERTY(hwdb->hwdb, modalias, key, value) + if (!udev_list_entry_add(&hwdb->properties_list, key, value)) + return_with_errno(NULL, ENOMEM); e = udev_list_get_entry(&hwdb->properties_list); if (!e) - errno = ENODATA; + return_with_errno(NULL, ENODATA); return e; } |