On 8/29/05, Erik Mouw <J.A.K.Mouw@xxxxxxxxxxxxxx> wrote: > 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. What is this, can you point me to some articles on slab allocator ..... I am not aware of it. > > > 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. Well I was not aware of thee functions, yes these can be used .... thanks for this. > > > Erik > > -- > Erik Mouw > J.A.K.Mouw@xxxxxxxxxxxxxx mouw@xxxxxxxxxxxx > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.1 (GNU/Linux) > > iD8DBQFDEr32/PlVHJtIto0RAuaPAJ4oJfSQAuodghwcJSLyOTj/uncEuACdGeXx > YfHqH65v00ofxmW/sbedUCs= > =DxJq > -----END PGP SIGNATURE----- > > > -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/