> Hi, > > 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. > > 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. > The kmalloc memory allocator is built on top of the slab allocator. So to allocate a structure of 10 bytes, it will allocate from a slab whose object size is 32 bytes. A very good description of how kmalloc works is found at http://infamous.hackaholic.org/Kmalloc_Internals.html. On 8/29/05, Gaurav Dhiman <gaurav4lkg@xxxxxxxxx> wrote: > Hi, > > 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. > > 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. > > 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. > > regards, > -Gaurav > > -- > Kernelnewbies: Help each other learn about the Linux kernel. > Archive: http://mail.nl.linux.org/kernelnewbies/ > FAQ: http://kernelnewbies.org/faq/ > > -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/