q Xr malloc 9 . .Ss INITIALIZING A POOL The function .Fn pool_init initializes a resource pool. The arguments are:
p l -tag -offset indent -width "align_offset" t Fa pp The handle identifying the pool resource instance. t Fa size Specifies the size of the memory items managed by the pool. t Fa align Specifies the memory address alignment of the items returned by .Fn pool_get . This argument must be a power of two. If zero, the alignment defaults to an architecture-specific natural alignment. t Fa align_offset The offset within an item to which the .Fa align parameter applies. t Fa nitems Specifies the number of memory items that are allocated to the pool at creation time. This number may be zero, in which case .Fn pool_prime can be used at a later time to add permanent items to the pool. t Fa wchan The .Sq wait channel passed on to .Xr tsleep 9 if .Fn pool_get must wait for items to be returned to the pool. t Fa palloc can be set to .Dv NULL or .Dv pool_allocator_kmem , in which case the default kernel memory allocator will be used. It can also be set to .Dv pool_allocator_nointr when the pool will never be accessed from interrupt context. .El .Ss DESTROYING A POOL The function .Fn pool_destroy destroys a resource pool. It takes a single argument .Fa pp identifying the pool resource instance. .Ss ALLOCATING ITEMS FROM A POOL .Fn pool_get allocates an item from the pool and returns a pointer to it. The arguments are: l -tag -offset indent -width "flags" t Fa pp The handle identifying the pool resource instance. t Fa flags One or more of .Dv PR_URGENT , .Dv PR_WAITOK or .Dv PR_LIMITFAIL , that define behaviour in case the pooled resources are depleted. If no resources are available and .Dv PR_WAITOK is given, this function will wait until items are returned to the pool. Otherwise .Fn pool_get returns .Dv NULL . If .Dv PR_URGENT is specified and no items are available and .Fn palloc cannot allocate a new page, the system will panic
q XXX .
Undefined behaviour results if
.Dv PR_MALLOCOK
is specified on a pool handle that was created using client-provided
storage.
a bunch of other flags aren't documented.
If both
.Dv PR_LIMITFAIL
and
.Dv PR_WAITOK
is specified, and the pool has reached its hard limit,
.Fn pool_get
will return
.Dv NULL
without waiting, allowing the caller to do its own garbage collection;
however, it will still wait if the pool is not yet at its hard limit.
.El
.Ss RETURNING ITEMS TO A POOL
.Fn pool_put
returns the pool item pointed at by
.Fa item
to the resource pool identified by the pool handle
.Fa pp .
If the number of available items in the pool exceeds the maximum pool
size set by
.Fn pool_sethiwat
and there are no outstanding requests for pool items,
the excess items will be returned to the system.
The arguments to
.Fn pool_put
are:
l -tag -offset indent -width "item" t Fa pp The handle identifying the pool resource instance.
t Fa item A pointer to a pool item previously obtained by
.Fn pool_get .
.El
.Ss PRIMING A POOL
.Fn pool_prime
adds items to the pool.
Storage space for the items is either allocated by using the page allocation
routine specified to
.Fn pool_create ,
or provided to
.Fn pool_prime
by the caller through the
.Fa storage
parameter.
p The arguments to .Fn pool_prime are: l -tag -offset indent -width "storage" t Fa pp The handle identifying the pool resource instance. t Fa nitems The number of items to add to the pool. t Fa storage Optional pre-allocated storage. .El
p This function may return .Dv ENOMEM in case the requested number of items could not be allocated. Otherwise, the return value is 0. .Ss SETTING POOL RESOURCE WATERMARKS A pool will attempt to increase its resource usage to keep up with the demand for its items. Conversely, it will return unused memory to the system should the number of accumulated unused items in the pool exceed a programmable limit. The limits for the minimum and maximum number of items which a pool should keep at hand are known as the high and low .Sy watermarks . The functions .Fn pool_sethiwat and .Fn pool_setlowat set a pool's high and low watermarks, respectively.
p .Fn pool_sethiwat l -tag -offset indent -width "flags" t Fa pp The handle identifying the pool resource instance. t Fa n The maximum number of items to keep in the pool. As items are returned and the total number of pages in the pool is larger than the maximum set by this function, any completely unused pages are released immediately. If this function is not used to specify a maximum number of items, the pages will remain associated with the pool until the system runs low on memory, at which point the VM system will try to reclaim unused pages. .El
p .Fn pool_setlowat l -tag -offset indent -width "flags" t Fa pp The handle identifying the pool resource instance. t Fa n The minimum number of items to keep in the pool. The number pages in the pool will not decrease below the required value to accommodate the minimum number of items specified by this function. Unlike .Fn pool_prime , this function does not allocate the necessary memory up-front. .El .Ss POTENTIAL PITFALLS Note that undefined behaviour results when mixing the storage providing methods supported by the pool resource routines.
p
The pool resource code uses a per-pool lock to protect its internal state.
If any pool functions are called in an interrupt context,
the caller must block all interrupts that might cause the
code to be reentered.
.Ss DIAGNOSTICS
Pool usage logs can be enabled by defining the compile-time option
.Dv POOL_DIAGNOSTIC .
.Sh RETURN VALUES
.Sh EXAMPLES
.Sh CODE REFERENCES
The pool manager is implemented in the file
a sys/kern/subr_pool.c .
.Sh AUTHOR
.Sh SEE ALSO
.Xr free 9 ,
.Xr malloc 9 ,
.Xr uvm 9
.Sh HISTORY
The
.Nx
pool manager appeared in
.Nx 1.4 .