From: Yu Watanabe Date: Thu, 21 Jul 2022 23:46:51 +0000 (+0900) Subject: sd-device: introduce device_unref_and_replace() X-Git-Tag: v252-rc1~593^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d6985e76f2c3c40552e310e3e93fca86da5a3e3a;p=thirdparty%2Fsystemd.git sd-device: introduce device_unref_and_replace() --- diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index cf4b8b166a9..b38db7d4737 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -63,6 +63,17 @@ typedef void* (*mfree_func_t)(void *p); #define free_and_replace(a, b) \ free_and_replace_full(a, b, free) +/* This is similar to free_and_replace_full(), but NULL is not assigned to 'b', and its reference counter is + * increased. */ +#define unref_and_replace_full(a, b, ref_func, unref_func) \ + ({ \ + typeof(a)* _a = &(a); \ + typeof(b) _b = ref_func(b); \ + unref_func(*_a); \ + *_a = _b; \ + 0; \ + }) + void* memdup(const void *p, size_t l) _alloc_(2); void* memdup_suffix0(const void *p, size_t l); /* We can't use _alloc_() here, since we return a buffer one byte larger than the specified size */ diff --git a/src/libsystemd/sd-device/device-util.h b/src/libsystemd/sd-device/device-util.h index e2adfe8132f..4eda2abaf36 100644 --- a/src/libsystemd/sd-device/device-util.h +++ b/src/libsystemd/sd-device/device-util.h @@ -10,6 +10,9 @@ #include "macro.h" #include "set.h" +#define device_unref_and_replace(a, b) \ + unref_and_replace_full(a, b, sd_device_ref, sd_device_unref) + #define FOREACH_DEVICE_PROPERTY(device, key, value) \ for (key = sd_device_get_property_first(device, &(value)); \ key; \