Memory allocation in kernel -- what to use in which situation?
What is the best for page-sized allocations?
kostikbel at gmail.com
Sun Oct 2 13:06:53 UTC 2011
On Sun, Oct 02, 2011 at 04:21:09PM +0400, Lev Serebryakov wrote:
> Hello, Freebsd-hackers.
> Here are several memory-allocation mechanisms in the kernel. The two
> I'm aware of is MALLOC_DEFINE()/malloc()/free() and uma_* (zone(9)).
> As far as I understand, malloc() is general-purpose, but it has
> fixed "transaction cost" (in term of memory consumption) for each
> block allocated, and is not very suitable for allocation of many small
> blocks, as lots of memory will be wasted for bookkeeping.
> zone(9) allocator, on other hand, have very low cost of each
> allocated block, but could allocate only pre-configured fixed-size
> blocks, and ideal for allocation tons of small objects (and provide
> API for reusing them, too!).
> Am I right?
> But what if I need to allocate a lot (say, 16K-32K) of page-sized
> blocks? Not in one chunk, for sure, but in lifetime of my kernel
> module. Which allocator should I use? It seems, the best one will be
> very low-level only-page-sized allocator. Is here any in kernel?
Very short answer is that you could use kmem_alloc() or kmem_malloc().
kmem_alloc() sleeps, while kmem_malloc() may be directed to not sleep.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 196 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20111002/64730c9f/attachment.pgp
More information about the freebsd-hackers