summaryrefslogtreecommitdiff
path: root/src/fundamental
diff options
context:
space:
mode:
authorDan Streetman <ddstreet@ieee.org>2022-12-06 13:07:34 -0500
committerLuca Boccassi <luca.boccassi@gmail.com>2023-01-24 18:33:43 +0000
commit40c5cc2b214fd47ebfe85786a2a220bd3e9f275a (patch)
tree07764acbdd87dde4091a4e1abc820c030c159a5b /src/fundamental
parent98a13530145fa4b663c4402689717deccd2080ea (diff)
downloadsystemd-40c5cc2b214fd47ebfe85786a2a220bd3e9f275a.tar.gz
Consolidate various TAKE_* into TAKE_GENERIC(), add TAKE_STRUCT()
Diffstat (limited to 'src/fundamental')
-rw-r--r--src/fundamental/macro-fundamental.h17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
index b2bc435bd3..b939ee1a7e 100644
--- a/src/fundamental/macro-fundamental.h
+++ b/src/fundamental/macro-fundamental.h
@@ -298,13 +298,18 @@
/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time
* resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
-#define TAKE_PTR(ptr) \
- ({ \
- typeof(ptr) *_pptr_ = &(ptr); \
- typeof(ptr) _ptr_ = *_pptr_; \
- *_pptr_ = NULL; \
- _ptr_; \
+#define TAKE_GENERIC(var, type, nullvalue) \
+ ({ \
+ type *_pvar_ = &(var); \
+ type _var_ = *_pvar_; \
+ type _nullvalue_ = nullvalue; \
+ *_pvar_ = _nullvalue_; \
+ _var_; \
})
+#define TAKE_PTR_TYPE(ptr, type) TAKE_GENERIC(ptr, type, NULL)
+#define TAKE_PTR(ptr) TAKE_PTR_TYPE(ptr, typeof(ptr))
+#define TAKE_STRUCT_TYPE(s, type) TAKE_GENERIC(s, type, {})
+#define TAKE_STRUCT(s) TAKE_STRUCT_TYPE(s, typeof(s))
/*
* STRLEN - return the length of a string literal, minus the trailing NUL byte.