DEFINE_FREE/CLASS && code readability (Was: [PATCH v2 2/6] Provide __free(argv) for argv_split() users)
From: Oleg Nesterov
Date: Sun Jan 05 2025 - 09:15:09 EST
Masami,
Sorry for abusing this thread. Your patches look fine to me, it is not
that I suggest to change them. I will use your patch as an example for
off-topic discussion.
On 01/05, Masami Hiramatsu (Google) wrote:
>
> +DEFINE_FREE(argv, char **, if (!IS_ERR_OR_NULL(_T)) argv_free(_T))
(IS_ERR looks unneeded but this is cosmetic).
OK, so it can be used as
void func(void)
{
char **argv __free(argv) = argv_split(...);
do_something(argv);
return;
}
And I cry every time when I read the code like this ;)
Because, to understand this code, I need to do the "nontrivial" grep to find
"DEFINE_FREE(argv,".
Perhaps we can establish a simple rule that every DEFINE_FREE() or DEFINE_CLASS()
should add another #define? I mean something like
DEFINE_FREE(argv, char **, if (!IS_ERR_OR_NULL(_T)) argv_free(_T))
#define __FREE_ARGV __free(argv)
void func(void)
{
char **argv __FREE_ARGV = argv_split(...);
do_something(argv);
return;
}
This way I can press Ctrl-] and see what the cleanup code actually does.
Can save a second or two. Important when you try to read the code you are
not familiar with.
Same for DEFINE_CLASS. For example,
int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
{
CLASS(fd, f)(fd);
if (fd_empty(f))
return -EBADF;
return vfs_fchown(fd_file(f), user, group);
}
If you are not familiar with this code, it looks mysterious until you find
DEFINE_CLASS(fd, ...) in include/linux/file.h.
Oleg.