mirror of
https://github.com/systemd/systemd.git
synced 2025-03-08 08:58:27 +03:00
libudev: use DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC() macro where applicable
This commit is contained in:
parent
50d2158901
commit
3c6ac21929
@ -18,7 +18,7 @@ struct udev_device {
|
||||
sd_device *device;
|
||||
|
||||
/* legacy */
|
||||
int refcount;
|
||||
unsigned n_ref;
|
||||
|
||||
struct udev_device *parent;
|
||||
bool parent_set;
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user