> as i know,kmalloc is based on kmem_cache_alloc ,so what's the different?

kmalloc uses the generic slab caches available to any kernel code. so your
module will share slab cache with other components in kernel, but if you
specifically want a better slab cache management dedicated to your module
only, that too for a specific type of objects, use the lower function i.e.
kmem_cache_alloc, it will allocate objects from a dedicated slab cache for
your module objects only, you must create the slab cache by calling
kmem_cache_create before allocating any object. kmem_cache_create takes
sizeof your object you want to create slab of, a name which appears in
/proc/slabinfo and flags to govern behavior of your slab cache.

Rajat

On Fri, Nov 5, 2010 at 1:22 PM, Venkatram Tummala <venkatram...@gmail.com>wrote:

> On Thu, Nov 4, 2010 at 8:03 PM, kernel.niko <kernel.n...@gmail.com> wrote:
> > hi,everyone,
> > I'm wandering how to use kmalloc and kmem_cache_alloc.
> > as i know,kmalloc is based on kmem_cache_alloc ,so what's the different?
>
> kmalloc should be used when you want to allocate byte sized memory as
> opposed to allocating memory in units of pages, in which case you
> should use alloc_pages(..).
>
> kmem_cache_alloc is a part of the slab cache interface. This is used
> for reusing objects in the kernel. Lets take mm_struct objects as an
> example. On a live system, processes come & go. Every process needs a
> mm_struct object. And when a process dies, you can throw away the
> object. Instead of creating & throwing away objects, slab cache
> maintains a cache of objects. When you free an object, instead of
> deallocating it, you give it back to the cache. Next time, if  you
> want to create a new object, slab cache gives you one object from the
> slab cache. kmem_cache_alloc is one of the interface functions to use
> the slab cache in the kernel. You might want to read about slab caches
> in detail if you need more information.
>
> Venkatram Tummala
> > when to use kmalloc? and
> > in what occasion,kmem_cache_alloc should be used?
> > thanks.
> >
> > 2010-11-05
> > ________________________________
> > kernel.niko
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecar...@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>

Reply via email to