]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: introduce device_unref_and_replace()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Jul 2022 23:46:51 +0000 (08:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 22 Jul 2022 23:16:38 +0000 (08:16 +0900)
src/basic/alloc-util.h
src/libsystemd/sd-device/device-util.h

index cf4b8b166a9dabe57b5d13c8c89830a7dd221b7b..b38db7d4737d1fb7aa93bbcd3747501218bfa884 100644 (file)
@@ -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 */
 
index e2adfe8132f2aeaf9caf7b85bb046b45a11fb73c..4eda2abaf366c7b67144393b16680188db22a0e1 100644 (file)
@@ -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;                                                  \