summaryrefslogtreecommitdiff
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
parent98a13530145fa4b663c4402689717deccd2080ea (diff)
downloadsystemd-40c5cc2b214fd47ebfe85786a2a220bd3e9f275a.tar.gz
Consolidate various TAKE_* into TAKE_GENERIC(), add TAKE_STRUCT()
-rw-r--r--src/basic/fd-util.h10
-rw-r--r--src/basic/process-util.h10
-rw-r--r--src/fundamental/macro-fundamental.h17
-rw-r--r--src/shared/keyring-util.h10
4 files changed, 17 insertions, 30 deletions
diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h
index 97339254ba..952afdd64f 100644
--- a/src/basic/fd-util.h
+++ b/src/basic/fd-util.h
@@ -90,14 +90,8 @@ static inline int make_null_stdio(void) {
return rearrange_stdio(-EBADF, -EBADF, -EBADF);
}
-/* Like TAKE_PTR() but for file descriptors, resetting them to -1 */
-#define TAKE_FD(fd) \
- ({ \
- int *_fd_ = &(fd); \
- int _ret_ = *_fd_; \
- *_fd_ = -EBADF; \
- _ret_; \
- })
+/* Like TAKE_PTR() but for file descriptors, resetting them to -EBADF */
+#define TAKE_FD(fd) TAKE_GENERIC(fd, int, -EBADF)
/* Like free_and_replace(), but for file descriptors */
#define close_and_replace(a, b) \
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index 6ceb8ebf7d..96da0bb292 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -180,14 +180,8 @@ int get_oom_score_adjust(int *ret);
assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX);
-/* Like TAKE_PTR() but for child PIDs, resetting them to 0 */
-#define TAKE_PID(pid) \
- ({ \
- pid_t *_ppid_ = &(pid); \
- pid_t _pid_ = *_ppid_; \
- *_ppid_ = 0; \
- _pid_; \
- })
+/* Like TAKE_PTR() but for pid_t, resetting them to 0 */
+#define TAKE_PID(pid) TAKE_GENERIC(pid, pid_t, 0)
int pidfd_get_pid(int fd, pid_t *ret);
int pidfd_verify_pid(int pidfd, pid_t pid);
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.
diff --git a/src/shared/keyring-util.h b/src/shared/keyring-util.h
index 838e990b80..c8c53f1be1 100644
--- a/src/shared/keyring-util.h
+++ b/src/shared/keyring-util.h
@@ -5,13 +5,7 @@
#include "missing_keyctl.h"
-/* TAKE_FD but for key_serial_t instead of fds */
-#define TAKE_KEY_SERIAL(key_serial) \
- ({ \
- key_serial_t *_key_serialp_ = &(key_serial); \
- key_serial_t _key_serial_ = *_key_serialp_; \
- *_key_serialp_ = -1; \
- _key_serial_; \
- })
+/* Like TAKE_PTR() but for key_serial_t, resetting them to -1 */
+#define TAKE_KEY_SERIAL(key_serial) TAKE_GENERIC(key_serial, key_serial_t, -1)
int keyring_read(key_serial_t serial, void **ret, size_t *ret_size);