]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Consolidate various TAKE_* into TAKE_GENERIC(), add TAKE_STRUCT()
authorDan Streetman <ddstreet@ieee.org>
Tue, 6 Dec 2022 18:07:34 +0000 (13:07 -0500)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 24 Jan 2023 18:33:43 +0000 (18:33 +0000)
src/basic/fd-util.h
src/basic/process-util.h
src/fundamental/macro-fundamental.h
src/shared/keyring-util.h

index 97339254baaa7c1a7c011757ee0016a842e0307f..952afdd64f0054755790dd2861f93254e0c21239 100644 (file)
@@ -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)                 \
index 6ceb8ebf7d70f4331a72c0f3608fb442c7ec6b11..96da0bb2921062f9291c1f27b0eb39f4610968a9 100644 (file)
@@ -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);
index b2bc435bd35072798aa960edbd32e919a3a5f32f..b939ee1a7e97d61c814c287bcec42945c67f3c22 100644 (file)
 
 /* 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.
index 838e990b809010ede8efe3d4fd309319896ee99f..c8c53f1be14d61d60c0e281b5503ad83327631eb 100644 (file)
@@ -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);