]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: add simple DEFER_VOID_CALL() helper
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Aug 2025 21:19:35 +0000 (23:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 24 Sep 2025 13:46:30 +0000 (15:46 +0200)
src/basic/macro.h

index 3ddc5272b856c9956c9de70ca20dee33c3bf0b5b..b53805d5b81d695cf9389b1e15b11f9642bce79d 100644 (file)
@@ -206,3 +206,16 @@ static inline size_t size_add(size_t x, size_t y) {
         for (typeof(entry) _va_sentinel_[1] = {}, _entries_[] = { __VA_ARGS__ __VA_OPT__(,) _va_sentinel_[0] }, *_current_ = _entries_; \
              ((long)(_current_ - _entries_) < (long)(ELEMENTSOF(_entries_) - 1)) && ({ entry = *_current_; true; }); \
              _current_++)
+
+typedef void (*void_func_t)(void);
+
+static inline void dispatch_void_func(void_func_t *f) {
+        assert(f);
+        assert(*f);
+        (*f)();
+}
+
+/* Inspired by Go's "defer" construct, but much more basic. This basically just calls a void function when
+ * the current scope is left. Doesn't do function parameters (i.e. no closures). */
+#define DEFER_VOID_CALL(x) _DEFER_VOID_CALL(UNIQ, x)
+#define _DEFER_VOID_CALL(uniq, x) _unused_ _cleanup_(dispatch_void_func) void_func_t UNIQ_T(defer, uniq) = (x)