VMEM(9)
β¨ NAME β¨
vmem β the general purpose resource allocator π
and yes bestie, this is page VMEM(9), section 9, kernel developer's manual, we're in the big leagues now π
π¦ SYNOPSIS π¦
#include <sys/vmem.h>
vmem_t *
vmem_create(const char *name, vmem_addr_t base, vmem_size_t size,
vmem_size_t quantum, vmem_size_t qcache_max, int flags);
int
vmem_add(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, int flags);
int
vmem_xalloc(vmem_t *vm, const vmem_size_t size, vmem_size_t align,
const vmem_size_t phase, const vmem_size_t nocross,
const vmem_addr_t minaddr, const vmem_addr_t maxaddr, int flags,
vmem_addr_t *addrp);
void
vmem_xfree(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);
int
vmem_alloc(vmem_t *vm, vmem_size_t size, int flags, vmem_addr_t *addrp);
void
vmem_free(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);
void
vmem_destroy(vmem_t *vm);
πΈ DESCRIPTION πΈ
So vmem is a general purpose resource allocator. And the way its name is literally lying to you β despite being called virtual memory, it can be used for arbitrary resources other than virtual memory. It's giving "the name is a vibe, not a contract" π
π vmem_create()
vmem_create() creates a new vmem arena. New arena, new me, main character energy fr fr β¨
nameβ the string to describe the vmem.baseβ the start address of the initial span. Pass0if no initial span is required.sizeβ the size of the initial span. Pass0if no initial span is required.quantumβ the smallest unit of allocation.qcache_maxβ the largest size of allocations which can be served by quantum cache. Ngl it is merely a hint and can be ignored πflagsβmalloc(9)wait flag.
π§ vmem_add()
vmem_add() adds a span of size size starting at addr to the arena. Returns 0 on success, ENOMEM on failure. And flags is a malloc(9) wait flag. Simple, iconic, no notes π
π« vmem_xalloc()
vmem_xalloc() allocates a resource from the arena. This is the fancy one, the one with opinions about where things go π
vmβ the arena which we allocate from.sizeβ specify the size of the allocation.alignβ if zero, don't care about the alignment of the allocation. Otherwise, request a resource segment starting at offset phase from an align aligned boundary.phaseβ see the above description of align. If align is zero, phase should be zero. Otherwise, phase should be smaller than align.nocrossβ request a resource which doesn't cross nocross aligned boundary.minaddrβ specify the minimum address which can be allocated, orVMEM_ADDR_MINif the caller does not care.maxaddrβ specify the maximum address which can be allocated, orVMEM_ADDR_MAXif the caller does not care.flagsβ a bitwise OR of an allocation strategy and amalloc(9)wait flag. The allocation strategy is one of:M_FIRSTFITβ prefer allocation performance.M_BESTFITβ prefer space efficiency.M_NEXTFITβ perform an address-ordered search for free addresses, beginning where the previous search ended.
addrpβ on success, if addrp is notNULL,vmem_xalloc()overwrites it with the start address of the allocated span.
π vmem_xfree()
vmem_xfree() frees resource allocated by vmem_xalloc() to the arena.
vmβ the arena which we free to.addrβ the resource being freed. It must be the one returned byvmem_xalloc(). Notably, it must not be the one fromvmem_alloc(). Otherwise, the behaviour is undefined. Bestie I am begging you, keep your pairs matched πsizeβ the size of the resource being freed. It must be the same as the size argument used forvmem_xalloc().
β vmem_alloc()
vmem_alloc() allocates a resource from the arena.
vmβ the arena which we allocate from.sizeβ specify the size of the allocation.flagsβ a bitwise OR of an vmem allocation strategy flag (see above) and amalloc(9)sleep flag.addrpβ on success, if addrp is notNULL,vmem_alloc()overwrites it with the start address of the allocated span.
π«Ά vmem_free()
vmem_free() frees resource allocated by vmem_alloc() to the arena.
vmβ the arena which we free to.addrβ the resource being freed. It must be the one returned byvmem_alloc(). Notably, it must not be the one fromvmem_xalloc(). Otherwise, the behaviour is undefined. Yes this is the mirror image of the other one, no you cannot mix and match, that's the whole bit π«sizeβ the size of the resource being freed. It must be the same as the size argument used forvmem_alloc().
π vmem_destroy()
vmem_destroy() destroys a vmem arena.
vmβ the vmem arena being destroyed. The caller should ensure that no one will use it anymore.
Okay so this is the "we're so over" moment. Make sure literally nobody is still holding onto it before you do this. No cap.
π RETURN VALUES π
vmem_create()returns a pointer to the newly allocatedvmem_t. Otherwise, it returnsNULL.- On success,
vmem_xalloc()andvmem_alloc()return0. Otherwise,ENOMEMis returned.
π₯ CODE REFERENCES π₯
The vmem subsystem is implemented within the file sys/kern/subr_vmem.c. That's the source of truth, go read it if you're feeling brave π«‘
π SEE ALSO π
malloc(9)- Jeff Bonwick and Jonathan Adams, βMagazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resourcesβ, 2001 USENIX Annual Technical Conference, 2001.
π¦ HISTORY π¦
The vmem allocator was originally implemented in NetBSD. It was introduced in FreeBSD 10.0. A whole glow-up timeline, honestly β¨
π AUTHORS π
Original implementation of vmem was written by YAMAMOTO Takashi. The FreeBSD port was made by Jeff Roberson. Kings π
π BUGS π
vmem relies on malloc(9), so it cannot be used as early during system bootstrap.
Fr fr. It's not a morning person. It needs malloc(9) awake first, so don't try to wake it up at 4am during bootstrap π
okay that's the whole page bestie, go allocate responsibly π
This was the Debian rendering of VMEM(9), dated May 17, 2019 β ty for coming to my TED talk, 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)2006 YAMAMOTO Takashi
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms