Skip to content

Commit 2c08060

Browse files
committed
Copy TAKE_PTR from systemd
Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent 095cb73 commit 2c08060

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,15 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char*, unlink_and_free);
765765

766766
int free_and_strdup(char **p, const char *s);
767767

768+
/* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
769+
* NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
770+
#define TAKE_PTR(ptr) \
771+
({ \
772+
typeof(ptr) _ptr_ = (ptr); \
773+
(ptr) = NULL; \
774+
_ptr_; \
775+
})
776+
768777
/* A check against a list of errors commonly used to indicate that a syscall/ioctl/other kernel operation we request is
769778
* not supported locally. We maintain a generic list for this here, instead of adjusting the possible error codes to
770779
* exactly what the calls might return for the simple reasons that due to FUSE and many differing in-kernel

0 commit comments

Comments
 (0)