1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-07 17:17:44 +03:00

Consolidate various TAKE_* into TAKE_GENERIC(), add TAKE_STRUCT()

This commit is contained in:
Dan Streetman 2022-12-06 13:07:34 -05:00 committed by Luca Boccassi
parent 98a1353014
commit 40c5cc2b21
4 changed files with 17 additions and 30 deletions

View 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) \

View 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);

View File

@ -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.

View 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);