POOL(9)
πΈ NAME πΈ
okay so this is POOL(9) from the Kernel Developer's Manual, and it's literally the resource-pool manager β¨. The whole squad is pool_init, pool_destroy, pool_get, pool_put, pool_prime, pool_sethiwat, pool_setlowat, and pool_sethardlimit β resource-pool manager. π
β¨ SYNOPSIS β¨
#include <sys/pool.h>
void
pool_init(struct pool *pp, size_t size, u_int align, u_int align_offset,
int flags, const char *wchan, struct pool_allocator *palloc,
int ipl);
void
pool_destroy(struct pool *pp);
void *
pool_get(struct pool *pp, int flags);
void
pool_put(struct pool *pp, void *item);
void
pool_prime(struct pool *pp, int n);
void
pool_sethiwat(struct pool *pp, int n);
void
pool_setlowat(struct pool *pp, int n);
void
pool_sethardlimit(struct pool *pp, int n, const char *warnmess,
int ratecap);
π DESCRIPTION π
These utility routines provide management of pools of fixed-sized areas of memory. Resource pools set aside an amount of memory for exclusive use by the resource pool owner. This can be used by applications to guarantee the availability of a minimum amount of memory needed to continue operation independent of the memory resources currently available from the system-wide memory allocator (malloc(9)). periodt.
π§ INITIALIZING A POOL π§
The function pool_init() initializes a resource pool. The arguments are:
ppβ The handle identifying the pool resource instance.sizeβ Specifies the size of the memory items managed by the pool.alignβ Specifies the memory address alignment of the items returned bypool_get(). This argument must be a power of two. If zero, the alignment defaults to an architecture-specific natural alignment.align_offsetβ The offset within an item to which thealignparameter applies.flagsβ Should be set to zero,PR_NOTOUCH, orPR_PSERIALIZE. IfPR_NOTOUCHis given, free items are never used to keep internal state so that the pool can be used for non memory backed objects. IfPR_PSERIALIZEis given, then the allocator guarantees that a passive serialization barrier equivalent toxc_barrier(0)will be performed before the object's backing store is returned to the system.PR_PSERIALIZEimpliesPR_NOTOUCH. Because of the guarantees provided byPR_PSERIALIZE, objects must never be freed to a pool using this option from either hard or soft interrupt context, as doing so may block. do not play with that one, bestie πwchanβ The βwait channelβ passed on tocv_wait(9)ifpool_get()must wait for items to be returned to the pool.pallocβ Can be set toNULLorpool_allocator_kmem, in which case the default kernel memory allocator will be used. It can also be set topool_allocator_nointrwhen the pool will never be accessed from interrupt context.iplβ Specifies an interrupt priority level that will block all interrupt handlers that could potentially access the pool.
π DESTROYING A POOL π
The function pool_destroy() destroys a resource pool. It takes a single argument pp identifying the pool resource instance.
π ALLOCATING ITEMS FROM A POOL π
pool_get() allocates an item from the pool and returns a pointer to it. The arguments are:
ppβ The handle identifying the pool resource instance.flagsβ The flags can be used to define behaviour in case the pooled resources are depleted. If no resources are available andPR_NOWAITis given,pool_get()returnsNULL. IfPR_WAITOKis given and allocation is attempted with no resources available, the function will sleep until items are returned to the pool. If bothPR_LIMITFAILandPR_WAITOKare specified, and the pool has reached its hard limit,pool_get()will returnNULLwithout 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. If thePR_ZEROflag is specified, then the memory returned will be zeroed first usingmemset(3).
π¦ RETURNING ITEMS TO A POOL π¦
pool_put() returns the pool item pointed at by item to the resource pool identified by the pool handle pp. If the number of available items in the pool exceeds the maximum pool size set by pool_sethiwat() and there are no outstanding requests for pool items, the excess items will be returned to the system. The arguments to pool_put() are:
ppβ The handle identifying the pool resource instance.itemβ A pointer to a pool item previously obtained bypool_get().
π SETTING POOL RESOURCE WATERMARKS AND LIMITS π
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 targets for the minimum and maximum number of free items which a pool should try to keep available are known as the high and low watermarks. The functions pool_sethiwat() and pool_setlowat() set a pool's high and low watermarks, respectively.
The limits for the minimum and maximum number of total items (both free and allocated) that the pool can have at any time are specified by the functions pool_prime() and pool_sethardlimit(), respectively. The defaults for these limits are 0 and UINT_MAX, respectively. Changing these limits will cause memory to be immediately allocated to the pool or freed from the pool as needed.
pool_sethiwat()
ppβ The handle identifying the pool resource instance.nβ The maximum number of free items to keep in the pool. As items are returned and the total number of free items 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.
pool_setlowat()
ppβ The handle identifying the pool resource instance.nβ The minimum number of free items to keep in the pool. When the number of free items in the pool drops below this threshold, a non-blocking attempt is made to allocate memory for more items. The number of free items is not guaranteed to remain above this threshold.
pool_sethardlimit()
ppβ The handle identifying the pool resource instance.nβ The maximum number of total items in the pool (i.e. the hard limit).warnmessβ The warning message that will be logged when the hard limit is reached.ratecapβ The minimal interval (in seconds) after which another warning message is issued when the pool hits its hard limit again.
pool_prime()
ppβ The handle identifying the pool resource instance.nβ The minimum number of total items for the pool. If the current number of total items is less than the new minimum then new items are allocated with blocking allocations.
π POTENTIAL PITFALLS π
Note that undefined behaviour results when mixing the storage providing methods supported by the pool resource routines.
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. Additionally, the functions pool_init() and pool_destroy() should never be called in interrupt context. like, ever ever π
π©Ί DIAGNOSTICS π©Ί
Pool usage logs can be enabled by defining the compile-time option POOL_DIAGNOSTIC.
π CODE REFERENCES π
The pool manager is implemented in the file sys/kern/subr_pool.c.
π SEE ALSO π
β HISTORY β
The NetBSD pool manager appeared in NetBSD 1.4.
xoxo, your Debian bestie π β footer vibes: source/version is Debian, the date is April 12, 2020, and the page is POOL(9). slay β¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 1997, 1998, 2007 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms