Re: [TECH TOPIC] Rust for Linux

Laurent Pinchart
2021-07-07T21:02:50Z

On Wed, Jul 07, 2021 at 01:32:36PM -0700, Bart Van Assche wrote:
> On 7/7/21 11:57 AM, Linus Torvalds wrote:
> > On Tue, Jul 6, 2021 at 7:41 PM Bart Van Assche <bvanassche@acm.org> wrote:
> >> As a sidenote, I'm surprised that C++ is not supported for Linux kernel
> >> code since C++ supports multiple mechanisms [..]
> >
> > You'd have to get rid of some of the complete garbage from C++ for it
> > to be usable.
> >
> > One of the trivial ones is "new" - not only is it a horribly stupid
> > namespace violation, but it depends on exception handling that isn't
> > viable for the kernel, so it's a namespace violation that has no
> > upsides, only downsides.
> >
> > Could we fix it with some kind of "-Dnew=New" trickery? Yes, but
> > considering all the other issues, it's just not worth the pain. C++ is
> > simply not a good language. It doesn't fix any of the fundamental
> > issues in C (ie no actual safety), and instead it introduces a lot of
> > new problems due to bad designs.
>
> Hi Linus,
>
> Thank you for having shared your opinion. You may want to know that
> every C++ project I have worked on so far enabled at least the following
> compiler flags: -fno-exceptions and -fno-rtti.
>
> What the C++ operator new does if not enough memory is available depends
> on the implementation of that operator. We could e.g. modify the
> behavior of operator new as follows:

Or we could forbid usage of new() and require different memory
allocation primitives. The kernel doesn't use malloc() either :-)

> - Add -fno-builtin-new to the compiler flags.
> - Define a custom version of operator new.
>
> An example (user space code):
>
> include <stdlib.h>
> #include <stdio.h>
>
> void *operator new(size_t size)
> {
> printf("%s\n", __func__);
> return malloc(size);
> }
>
> void operator delete(void *p)
> {
> printf("%s\n", __func__);
> free(p);
> }
>
> void operator delete(void *p, size_t size)
> {
> printf("%s\n", __func__);
> free(p);
> }
>
> void *operator new[](size_t size)
> {
> printf("%s\n", __func__);
> return malloc(size);
> }
>
> void operator delete[](void *p)
> {
> printf("%s\n", __func__);
> free(p);
> }
>
> void operator delete[](void *p, size_t size)
> {
> printf("%s\n", __func__);
> free(p);
> }
>
> int main(int, char **)
> {
> int *intp = new int;
> long *arrayp = new long[37];
> delete[] arrayp;
> delete intp;
> return 0;
> }
>
> The output of the above code:
>
> operator new
> operator new []
> operator delete []
> operator delete

--
Regards,

Laurent Pinchart