Memory allocation in kernel -- what to use in which situation? What is the best for page-sized allocations?

Lev Serebryakov lev at FreeBSD.org
Sun Oct 2 12:21:25 UTC 2011


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?

-- 
// Black Lion AKA Lev Serebryakov <lev at FreeBSD.org>



More information about the freebsd-hackers mailing list