PBUF(9)
π PBUF(9) π β the kernel dev manual, but make it cute
okay bestie, gather round, because today we're talking about pbuf and friends β and this is from the Kernel Developer's Manual, so yes, we're in the big leagues now π
β¨ NAME β¨
pbuf, getpbuf, trypbuf, relpbuf β the girlies that manage physical buffers π
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bio.h>
#include <sys/buf.h>
struct buf *
getpbuf(int *pfreecnt);
struct buf *
trypbuf(int *pfreecnt);
void
relpbuf(struct buf *bp, int *pfreecnt);
so yeah, four headers, three functions, one little struct buf * situation going on π
β¨ DESCRIPTION β¨
okay so. These functions are literally the ones that allocate and release physical buffers. That's the whole job. That's the vibe. π
The physical buffers get allocated at system startup, and they live in their own separate pool, apart from the main system buffers. Not me being like "we're not like other buffers" β they literally aren't π¦ They're made for subsystems that cannot or should not depend on the main buffer pool (the swap pager is the iconic example, iykyk). Depending on how much memory the system has, it allocates between 16 and 256 physical buffers.
Every subsystem that allocates buffers through these calls is expected to manage its own percentage free counter, bestie β that's a you problem, not a pbuf problem π And here's the tea:
- If that value is initialized to
-1, the number of buffers available to the subsystem is limited only by the number of physical buffers available. - The number of buffers is stored in
nswbuf, which is defined in<sys/buf.h>and initialized incpu_startup(). - A recommended initialization value is
1/2 nswbuf. slay.
Now, the functions, one by one:
getpbuf()β returns the first available buffer to the user. If there are no buffers available, it will sleep waiting for one to become available π΄ And ifpfreecntis zero,getpbuf()will sleep until it increases.pfreecntgets decremented prior to returning.trypbuf()β returns the first available buffer. If there are no buffers available,NULLis returned. Also, ifpfreecntis zero,NULLis returned.pfreecntis decremented prior to returning a valid buffer. And ifNULLis returned,pfreecntis not modified. no cap, it just doesn't touch it π«Άrelpbuf()β releases the buffer back to the free list. If the buffer'sb_rcredorb_wcredstructures are notNULL, they are freed. Seecrfree(9)for that whole situation. Andpfreecntis incremented prior to returning β
β¨ RETURN VALUES β¨
getpbuf() and trypbuf() return a pointer to the buffer. In the case of trypbuf(), NULL can also be returned, which means there are no buffers available. that's it, that's the whole answer π
β¨ AUTHORS β¨
This manual page was written by Chad David <davidc@acns.ab.ca> β give him his flowers πΈ
and that's the page, bestie!! this one came from the Debian build of the manual, dated July 9, 2001 β so yeah, it's vintage, it's iconic, it's giving "before you were born" energy π go forth and manage your physical buffers responsibly, mwah πβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (C) 2001 Chad David <davidc@acns.ab.ca>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms