1 .\" $NetBSD: pool.9,v 1.42 2007/07/25 09:14:18 pooka Exp $
3 .\" Copyright (c) 1997, 1998, 2007 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Paul Kranenburg.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
41 .Nd resource-pool manager
49 .Fa "u_int align_offset"
51 .Fa "const char *wchan"
52 .Fa "struct pool_allocator *palloc"
56 .Fn pool_destroy "struct pool *pp"
58 .Fn pool_get "struct pool *pp" "int flags"
60 .Fn pool_put "struct pool *pp" "void *item"
62 .Fn pool_prime "struct pool *pp" "int nitems"
64 .Fn pool_sethiwat "struct pool *pp" "int n"
66 .Fn pool_setlowat "struct pool *pp" "int n"
68 These utility routines provide management of pools of fixed-sized
70 Resource pools set aside an amount of memory for exclusive use by the resource
72 This can be used by applications to guarantee the availability of a minimum
73 amount of memory needed to continue operation independent of the memory
74 resources currently available from the system-wide memory allocator
76 .Ss INITIALIZING A POOL
79 initializes a resource pool.
82 .Bl -tag -offset indent -width "align_offset"
84 The handle identifying the pool resource instance.
86 Specifies the size of the memory items managed by the pool.
88 Specifies the memory address alignment of the items returned by
90 This argument must be a power of two.
92 the alignment defaults to an architecture-specific natural alignment.
94 The offset within an item to which the
98 Should be set to zero or
102 is given, free items are never used to keep internal state so that
103 the pool can be used for non memory backed objects.
111 must wait for items to be returned to the pool.
116 .Dv pool_allocator_kmem ,
117 in which case the default kernel memory allocator will be used.
118 It can also be set to
119 .Dv pool_allocator_nointr
120 when the pool will never be accessed from interrupt context.
122 Specifies an interrupt priority level that will block all interrupt
123 handlers that could potentially access the pool.
128 macro can be used to both declare and initialize a resource pool.
131 macro has the same arguments as the
133 function and the resource pool will be initialized automatically
134 during system startup.
135 .Ss DESTROYING A POOL
138 destroys a resource pool.
139 It takes a single argument
141 identifying the pool resource instance.
142 .Ss ALLOCATING ITEMS FROM A POOL
144 allocates an item from the pool and returns a pointer to it.
146 .Bl -tag -offset indent -width "flags"
148 The handle identifying the pool resource instance.
150 The flags can be used to define behaviour in case the pooled resources
152 If no resources are available and
160 is given and allocation is attempted with no resources available,
161 the function will sleep until items are returned to the pool.
162 .\"Undefined behaviour results if
164 .\"is specified on a pool handle that was created using client-provided
166 .\" a bunch of other flags aren't documented.
171 are specified, and the pool has reached its hard limit,
175 without waiting, allowing the caller to do its own garbage collection;
176 however, it will still wait if the pool is not yet at its hard limit.
178 .Ss RETURNING ITEMS TO A POOL
180 returns the pool item pointed at by
182 to the resource pool identified by the pool handle
184 If the number of available items in the pool exceeds the maximum pool
187 and there are no outstanding requests for pool items,
188 the excess items will be returned to the system.
192 .Bl -tag -offset indent -width "item"
194 The handle identifying the pool resource instance.
196 A pointer to a pool item previously obtained by
201 adds items to the pool.
202 Storage space for the items is allocated by using the page allocation
209 .Bl -tag -offset indent -width "storage"
211 The handle identifying the pool resource instance.
213 The number of items to add to the pool.
216 This function may return
218 in case the requested number of items could not be allocated.
220 the return value is 0.
221 .Ss SETTING POOL RESOURCE WATERMARKS
222 A pool will attempt to increase its resource usage to keep up with the demand
225 it will return unused memory to the system should the number of accumulated
226 unused items in the pool exceed a programmable limit.
227 The limits for the minimum and maximum number of items which a pool should keep
228 at hand are known as the high and low
234 set a pool's high and low watermarks, respectively.
237 .Bl -tag -offset indent -width "flags"
239 The handle identifying the pool resource instance.
241 The maximum number of items to keep in the pool.
242 As items are returned and the total number of pages in the pool is larger
243 than the maximum set by this function,
244 any completely unused pages are released immediately.
245 If this function is not used to specify a maximum number of items,
246 the pages will remain associated with the pool until the system runs low
248 at which point the VM system will try to reclaim unused pages.
252 .Bl -tag -offset indent -width "flags"
254 The handle identifying the pool resource instance.
256 The minimum number of items to keep in the pool.
257 The number pages in the pool will not decrease below the required value to
258 accommodate the minimum number of items specified by this function.
261 this function does not allocate the necessary memory up-front.
263 .Ss POTENTIAL PITFALLS
264 Note that undefined behaviour results when mixing the storage providing
265 methods supported by the pool resource routines.
267 The pool resource code uses a per-pool lock to protect its internal state.
268 If any pool functions are called in an interrupt context,
269 the caller must block all interrupts that might cause the
270 code to be reentered.
271 Additionally, the functions
275 should never be called in interrupt context.
277 Pool usage logs can be enabled by defining the compile-time option
278 .Dv POOL_DIAGNOSTIC .
279 .\" .Sh RETURN VALUES
282 The pool manager is implemented in the file
283 .Pa sys/kern/subr_pool.c .
288 .Xr memoryallocators 9 ,
294 pool manager appeared in