From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936089AbdEVVtU (ORCPT ); Mon, 22 May 2017 17:49:20 -0400 Received: from bombadil.infradead.org ([65.50.211.133]:48392 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934097AbdEVVtS (ORCPT ); Mon, 22 May 2017 17:49:18 -0400 Subject: Re: [PATCH] CodingStyle: delete "kmalloc(sizeof(*var))" as preferred allocation form To: Joe Perches , Alexey Dobriyan , akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org References: <20170522213837.GA13861@avx2> <1495489398.2093.48.camel@perches.com> From: Randy Dunlap Message-ID: <1ca80eb8-c095-ad74-de29-5924a0624e1f@infradead.org> Date: Mon, 22 May 2017 14:49:16 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: <1495489398.2093.48.camel@perches.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/22/17 14:43, Joe Perches wrote: > On Tue, 2017-05-23 at 00:38 +0300, Alexey Dobriyan wrote: >> 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 >> --- >> >> 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. > > Thanks. I agree with this deletion. > Acked-by: Randy Dunlap Thanks. -- ~Randy