Re: [TECH TOPIC] Rust for Linux

Bart Van Assche
2021-07-07T02:41:16Z

On 7/6/21 4:08 PM, Stephen Hemminger wrote:
> Why not use the GCC cleanup function attribute, it is a lot cleaner than
> having ugly error prone macros.
>
> #define _cleanup_free __atttribute__((__cleanup__(free)))
>
> int main(int argc, char **argv)
> {
> _cleanup_free_ void *p;
> _cleanup_free_ void *q;

Huh? My code uses the gcc cleanup attribute.

The above code will trigger a crash because gcc passes a pointer to
local variable to the cleanup function (free) instead of the value of
the local variable.

As a sidenote, I'm surprised that C++ is not supported for Linux kernel
code since C++ supports multiple mechanisms that are useful in kernel
code, e.g. RAII, lambda functions, range-based for loops, std::span<>
and std::string_view<>. Lambda functions combined with std::function<>
allow to implement type-safe callbacks. Implementing type-safe callbacks
in C without macro trickery is not possible.

Bart.