Re: p = kmalloc(sizeof(*p), )

From: Alan Cox (alan_at_lxorguk.ukuu.org.uk)
Date: 09/18/05

  • Next message: Denis Vlasenko: "Re: Eradic disk access during reads"
    To: Russell King <rmk+lkml@arm.linux.org.uk>
    Date:	Sun, 18 Sep 2005 12:04:34 +0100
    
    

    On Sul, 2005-09-18 at 11:06 +0100, Russell King wrote:
    > I completely disagree with the above assertion for the following
    > reasons:

    Ditto, but you forgot #4. People are always getting sizeof(*p) wrong, in
    particular forgetting that p happens to be a void *, or a struct that is
    some generic type not the full space for the more complex object
    allocated, so using (*p) everywhere just causes more memory scribbles.

    If it bugs people add a kmalloc_object macro that is

    #define new_object(foo, gfp) (foo *)kmalloc(sizeof(foo), (gfp))

    then you can

            x = new_object(struct frob, GFP_KERNEL)

    Other good practice in many cases is a single routine which allocates
    and initialises the structure and is used by all allocators of that
    object. That removes duplicate initialisers, stops people forgetting to
    update all cases, allows better debug and far more.
     
    Alan

    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/


  • Next message: Denis Vlasenko: "Re: Eradic disk access during reads"

    Relevant Pages

    • Re: [PATCH] ohci1394: dma_pool_destroy while in_atomic() && irqs_disabled()
      ... > I disagree because the impact of this bug is small. ... I have to agree that the impact is small even for the people using ISO recv - ... OTOH, if it allocates so much of memory while irqs disabled and holding locks, ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Sock leak in net/ipv4/af_inet.c - 2.4.26
      ... It allocates from the sock slab using sk_alloc, ... sk_freeis never called on these sock structs. ... around the sk_alloc/sk_free calls to track allocations. ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Re: [patch] IBM HDAPS accelerometer driver.
      ... >> You also must not use any syscall that allocates even temporary memory in ... >> shutdown or blocked at suspend. ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Re: can device drivers return non-ram via vm_ops->nopage?
      ... >> Remember that we're fond of telling driver writers to use scatter gather ... > anything else that doesn't use any IOTLB and just allocates a chunk of ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)
    • Re: [PATCH 0/6] freepgt: free_pgtables shakeup
      ... arch_get_unmapped_area allocates address space dynamically ... The protection against mapping things below 32K comes from the syscall ... send the line "unsubscribe linux-kernel" in ...
      (Linux-Kernel)