[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAHk-=whFKYMrF6euVvziW+drw7-yi1pYdf=uccnzJ8k09DoTXA@mail.gmail.com>
Date: Thu, 24 Feb 2022 09:45:05 -0800
From: Linus Torvalds <torvalds@...uxfoundation.org>
To: Kalle Valo <kvalo@...nel.org>
Cc: Dan Carpenter <dan.carpenter@...cle.com>,
Francesco Magliocca <franciman12@...il.com>,
Jeff Johnson <quic_jjohnson@...cinc.com>,
ath10k@...ts.infradead.org, rmanohar@....qualcomm.com,
linux-wireless <linux-wireless@...r.kernel.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>
Subject: Re: Use of void pointer arithmetic?
On Thu, Feb 24, 2022 at 1:59 AM Kalle Valo <kvalo@...nel.org> wrote:
>
> > What does -Wpointer-arith buy us?
>
> A good question. I have always just thought we should avoid void pointer
> arithmetic due to the C standard, but now that you mention it void
> pointers can indeed simplify the code. So I'm not so sure anymore.
>
> Any opinions? Is there a kernel wide recommendation for this?
We consciously use arithmetic on 'void *' in some places, although
it's not exactly _hugely_ common.
It's much more common to turn a pointer into an 'unsigned long' and
then doing basic operations on that.
The advantage of 'void *' is that it does avoid the need to cast the
pointer back.
But at the same time it will never replace the 'cast to an actual
integer type', because the 'void *' arithmetic only does simple
addition and subtraction, and many pointer operations need more (ie
alignment needs to do the bitops).
So I think it's mostly a personal preference. I *do* think that doing
arithmetic on 'void *' is generally superior to doing it the
old-fashioned C way on 'char *' - unless, of course, 'char *' is
simply the native type in question.
So if 'char *' casts (and casting back) is the alternative, then by
all means use 'void *' as a kind of generic and type-independent "byte
pointer". That is how it is meant to be used in the gcc extension.
And no, nobody should ever use -Wpointer-arith on the kernel in
general. Our use of it is not _hugely_ common, but it's does exist,
and it's not really frowned upon.
Linus
Powered by blists - more mailing lists