Re: query related to kmalloc()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Aug 29, 2005 at 12:03:51PM +0530, Gaurav Dhiman wrote:
> Just wanted to know, is it a good idea to kmalloc for a small
> structure. For example if I define a structure in kernel of 10 bytes,
> and I want to get the memory allocated dynamically for it N number of
> times, is it good to kmalloc for it every time.

Depends on how many times you need to allocate such a structure.

> As kmalloc() allocates the memory in terms of page size (4k), atleast
> 4K will be allocated for each kmalloc() call in above case. On other
> side, wont it be good idea to get one page allocated with kmalloc()
> and then use it to store an array of our 10 byte structure and if we
> need more we can get one more page allocated with kmalloc(). In this
> way we can have list of kmalloc'ed pages and each page represents the
> array of out 10 byte strucutre.

kmalloc() will use the slab allocator, 10 byte structures will go into
the "size-32" slab, so only 22 bytes are wasted.

> I am asking this as I am adopting the second approch (list of
> kmalloc'ed pages) in one of my kernel modules, just wanted to know is
> it worth doing so much of list maintaince of kmalloc'ed pages or we
> can simply use the kmalloc for allocating memory to each structure.

Don't try to reinvent the wheel, there already is such a way to
allocate memory. Use the slab allocator, see kmem_cache_create() and
kmem_cache_alloc() in mm/slab.c.


Erik

-- 
Erik Mouw
J.A.K.Mouw@xxxxxxxxxxxxxx  mouw@xxxxxxxxxxxx

Attachment: signature.asc
Description: Digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux