1.\" $NetBSD: memoryallocators.9,v 1.3 2007/01/09 12:49:36 elad Exp $ 2.\" 3.\" Copyright (c) 2006 Elad Efrat <elad@NetBSD.org> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd January 7, 2007 29.Dt MEMORYALLOCATORS 9 30.Os 31.Sh NAME 32.Nm memoryallocators 33.Nd introduction to kernel memory allocators 34.Sh DESCRIPTION 35The 36.Nx 37kernel provides several memory allocators, each with different characteristics 38and purpose. 39This document summarizes the main differences between them. 40.Ss The Malloc Allocator 41The 42.Xr malloc 9 43allocator can be used for variable-sized allocations in the kernel address 44space. 45It is interrupt-safe, requires no setup (see below), and is considered to be 46stable (given the number of years it has been in the kernel). 47.Pp 48This interface also allows associating a 49.Dq type 50with an allocation to indicate what subsystem is using the memory allocated, 51thus providing statistics as to the memory usage of different kernel subsystems. 52To define a type, one should use the 53.Dv MALLOC_DEFINE 54macro, otherwise, one of the built-in types, like 55.Dv M_TEMP 56can be used. 57.Pp 58See 59.Xr malloc 9 60for more details. 61.Ss The Kmem Allocator 62The kmem allocator is modelled after an interface of similar name implemented in 63Solaris, and is under active development. 64.Pp 65It is implemented on-top of the 66.Xr vmem 9 67resource allocator (beyond the scope of this document), meaning it will be using 68.Xr pool_cache 9 69internally to speed-up common (small) sized allocations. 70.Pp 71Like 72.Xr malloc 9 , 73it requires no setup, but can't be used yet from interrupt context. 74.Pp 75See 76.Xr kmem_alloc 9 , 77.Xr kmem_zalloc 9 , 78and 79.Xr kmem_free 9 80for more details. 81.Ss The Pool Allocator 82The 83.Xr pool 9 84allocator is a fixed-size memory allocator. 85It requires setup (to initialize a memory pool) and is interrupt-safe. 86.Pp 87.\" On some architectures (foo, bar) the 88.\" .Xr pool 9 89.\" allocator will use direct-mapped segments rather than normal page 90.\" mappings, which can reduce TLB contentions. 91.\".Pp 92See 93.Xr pool 9 94for more details. 95.Ss The Pool Cache Allocator 96The pool cache allocator works on-top of the 97.Xr pool 9 98allocator, also allowing fixed-size allocation only, requires setup, 99and is interrupt-safe. 100.Pp 101The pool cache allocator is expected to be faster than other allocators, 102including the 103.Dq normal 104pool allocator. 105.Pp 106In the future this allocator is expected to have a per-CPU cache. 107.Pp 108See 109.Xr pool_cache 9 110for more details. 111.Ss The UVM Kernel Memory Allocator 112This is a low-level memory allocator interface. 113It allows variable-sized allocations in multiples of 114.Dv PAGE_SIZE , 115and can be used to allocate both wired and pageable kernel memory. 116.Pp 117See 118.Xr uvm 9 119for more details. 120.Sh SEE ALSO 121.Xr free 9 , 122.Xr intro 9 , 123.Xr kmem_alloc 9 , 124.Xr kmem_free 9 , 125.Xr kmem_zalloc 9 , 126.Xr malloc 9 , 127.Xr pool 9 , 128.Xr pool_cache 9 , 129.Xr uvm 9 , 130.Xr vmem 9 131.Sh AUTHORS 132.An Elad Efrat Aq elad@NetBSD.org 133.An YAMAMOTO Takashi Aq yamt@NetBSD.org 134