summaryrefslogtreecommitdiff
path: root/include/linux/cleanup.h
diff options
context:
space:
mode:
authorChristian Brauner <brauner@kernel.org>2024-06-27 16:11:39 +0200
committerChristian Brauner <brauner@kernel.org>2024-06-28 10:36:45 +0200
commitc6269149cbf7053272d918101981869438ff7c1e (patch)
tree56268222b9012639dff71b6a54f5e756a82ed3cc /include/linux/cleanup.h
parentff2c570ef7eaa9ded58e7a02dd7a68874a897508 (diff)
downloadlinux-next-c6269149cbf7053272d918101981869438ff7c1e.tar.gz
linux-next-c6269149cbf7053272d918101981869438ff7c1e.zip
file: add take_fd() cleanup helper
Add a helper that returns the file descriptor and ensures that the old variable contains a negative value. This makes it easy to rely on CLASS(get_unused_fd). Link: https://lore.kernel.org/r/20240627-work-pidfs-v1-1-7e9ab6cc3bb1@kernel.org Reviewed-by: Jeff Layton <jlayton@kernel.org> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
Diffstat (limited to 'include/linux/cleanup.h')
-rw-r--r--include/linux/cleanup.h13
1 files changed, 8 insertions, 5 deletions
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index c2d09bc4f976..80c4181e194a 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -63,17 +63,20 @@
#define __free(_name) __cleanup(__free_##_name)
-#define __get_and_null_ptr(p) \
- ({ __auto_type __ptr = &(p); \
- __auto_type __val = *__ptr; \
- *__ptr = NULL; __val; })
+#define __get_and_null(p, nullvalue) \
+ ({ \
+ __auto_type __ptr = &(p); \
+ __auto_type __val = *__ptr; \
+ *__ptr = nullvalue; \
+ __val; \
+ })
static inline __must_check
const volatile void * __must_check_fn(const volatile void *val)
{ return val; }
#define no_free_ptr(p) \
- ((typeof(p)) __must_check_fn(__get_and_null_ptr(p)))
+ ((typeof(p)) __must_check_fn(__get_and_null(p, NULL)))
#define return_ptr(p) return no_free_ptr(p)