Re: [PATCH 0/3] using guard/__free in networking

From: Johannes Berg
Date: Wed Mar 27 2024 - 17:43:46 EST


On Wed, 2024-03-27 at 22:28 +0100, Johannes Berg wrote:
>
> +typedef struct class_##_name##_drop##_t { \
> + class_##_name##_t obj; \
> + void (*destructor)(struct class_##_name##_drop##_t *); \
> +} class_##_name##_drop##_t; \

No, I misread the compiler output, it does output a real destructor
function to push it into a stack variable with this...

So I guess it'd have to be

void my_something(my_t *my)
{
..
named_guard(lock, mutex)(&my->mutex);
..
if (foo)
return -EINVAL; // automatically unlocks
..
// no need for lock any more
drop_guard(lock, mutex);
..
// do other things now unlocked
}


instead, syntax-wise.


Which obviously simplifies the changes:


diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index c2d09bc4f976..cf39a4a3f56f 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -163,6 +163,12 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
#define guard(_name) \
CLASS(_name, __UNIQUE_ID(guard))

+#define named_guard(_name, _class) \
+ CLASS(_class, _name)
+
+#define drop_guard(_name, _class) \
+ do { class_##_class##_destructor(&_name); _name = NULL; } while (0)
+
#define __guard_ptr(_name) class_##_name##_lock_ptr

#define scoped_guard(_name, args...) \