RE: [PATCH] cleanup: Add usage and style documentation

From: Markus Elfring
Date: Sat Mar 23 2024 - 14:02:09 EST


> DEFINE_FREE(remove_free, struct object *, if (_T) remove_free(_T))
> static int init(void)
> {
> struct object *obj __free(remove_free) = NULL;
> int err;
>
> guard(mutex)(lock);
> obj = alloc_add();
>
> if (!obj)
> return -ENOMEM;
>
> err = other_init(obj);
> if (err)
> return err; // remove_free() called without the lock!!
>
> no_free_ptr(obj);
> return 0;
> }

You demonstrated an improvable lock granularity and a questionable combination
of variable scopes.


> The fix for this bug is to replace the "__free(...) = NULL" pattern and
> move the assignment to the declaration.
>
> guard(mutex)(lock);
> struct object *obj __free(remove_free) = alloc_add();

How do you think about to describe such a source code transformation
as a conversion of a variable assignment to a variable definition
at the place of a resource allocation?

Would you like to increase the collaboration with the macros “DEFINE_CLASS” and “CLASS”?
https://elixir.bootlin.com/linux/v6.8.1/source/include/linux/cleanup.h#L82

Regards,
Markus