lkml.org 
[lkml]   [2017]   [Jan]   [26]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH 2/5] gtp: Improve another size determination in ipv4_pdp_add()
On Thu, Jan 26, 2017 at 1:18 PM, SF Markus Elfring
<elfring@users.sourceforge.net> wrote:

> - pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
> + pctx = kmalloc(sizeof(*pctx), GFP_KERNEL);

The rule about "sizeof(*p)" style of allocation is bogus and should be
abolished.

Rationale says that

> The alternative form where struct name is spelled out hurts readability

In terms of line length, yes, "sizeof(*p)" wins most of the time.
However, the former variant clearly show the type of data, so you don't need
to look it up and makes it readily available to follow with tags or equivalent.

> 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.

The correct way to prevent this kind of mistake is to not return "void
*" pointer
(at least most of the uses use typed allocation)

#define lmalloc(T, gfp) (T*)_kmalloc(sizeof(T), (gfp))

Bacause of a cast changing type of pointer will be noticed,

\
 
 \ /
  Last update: 2017-01-26 14:14    [W:0.074 / U:0.440 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site