From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] CodingStyle: delete "kmalloc(sizeof(*var))" as preferred allocation form
Date: Tue, 23 May 2017 00:38:37 +0300 [thread overview]
Message-ID: <20170522213837.GA13861@avx2> (raw)
There are valid reasons for
malloc(sizeof(struct S))
form:
* struct S acts as an anchor for ctags quickly reminding which type is
in focus
* argument re changing name prevents bugs is semi bogus:
such changes are rare,
"void *" cast gives both forms equal opportunity to be screwed up
* proper way to fix those rare misallocation bugs (which indeed happened)
is type safe allocation macros (see tmalloc from Samba).
However amount of disruption will be so high so it may never be done.
* ratio of allocation styles is ~6400:12000 which is about 1:2
so the amount of churn to maintain this rule is pretty high in theory.
The winning move is to not play and not encourage people send trivial stuff.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
Documentation/process/coding-style.rst | 10 ----------
1 file changed, 10 deletions(-)
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -808,16 +808,6 @@ kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and
vzalloc(). Please refer to the API documentation for further information
about them.
-The preferred form for passing a size of a struct is the following:
-
-.. code-block:: c
-
- p = kmalloc(sizeof(*p), ...);
-
-The alternative form where struct name is spelled out hurts readability and
-introduces an opportunity for a bug when the pointer variable type is changed
-but the corresponding sizeof that is passed to a memory allocator is not.
-
Casting the return value which is a void pointer is redundant. The conversion
from void pointer to any other pointer type is guaranteed by the C programming
language.
next reply other threads:[~2017-05-22 21:38 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-22 21:38 Alexey Dobriyan [this message]
2017-05-22 21:43 ` [PATCH] CodingStyle: delete "kmalloc(sizeof(*var))" as preferred allocation form Joe Perches
2017-05-22 21:49 ` Randy Dunlap
2017-05-22 22:22 ` Andrew Morton
2017-05-22 22:41 ` Joe Perches
2017-05-24 10:18 ` Alexey Dobriyan
2017-05-25 10:35 ` Joe Perches
2017-05-25 10:46 ` Bernd Petrovitsch
2017-05-28 19:16 ` Pavel Machek
2017-05-22 21:49 ` [PATCH] checkpatch: Remove preference for alloc(sizeof(*p), ...) Joe Perches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170522213837.GA13861@avx2 \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).