Re: [PATCH 2/5] gtp: Improve another size determination in ipv4_pdp_add()

From: Alexey Dobriyan
Date: Thu Jan 26 2017 - 08:11:17 EST


On Thu, Jan 26, 2017 at 1:18 PM, SF Markus Elfring
<elfring@xxxxxxxxxxxxxxxxxxxxx> 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,