VMEM(9)
π VMEM(9) β the kernel dev manual, bestie edition π
okay so you're reading the VMEM(9) page, which is the Kernel Developer's Manual entry for vmem, and honestly? this one's a bop. let's get into it π«Ά
β¨ NAME β¨
vmem β the general purpose resource allocator, aka the friend who can hold anything for you π
πΈ 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 like β despite the name, it's not just for virtual memory. it can do arbitrary resources. it's giving versatility, it's giving main character energy β¨
π vmem_create()
creates a new vmem arena. slay. here's the tea on the args:
nameβ the string that describes thevmem. its whole identity, honestly.baseβ the start address of the initial span. pass0if you don't need an initial span, no pressure.sizeβ the size of the initial span. pass0if no initial span is required.quantumβ the smallest unit of allocation. tiny but mighty.qcache_maxβ the largest size of allocations that can be served by the quantum cache. heads up: it's merely a hint and can be ignored. lowkey optional, fr.flagsβ themalloc(9)wait flag.
π§ vmem_add()
adds a span of size size starting at addr to the arena. returns 0 on success, ENOMEM on failure. and flags? that's the malloc(9) wait flag. π
π₯ vmem_xalloc()
allocates a resource from the arena. this is the fancy one, buckle up:
vmβ the arena 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 description of align right above π if align is zero, phase should be zero. otherwise, phase should be smaller than align.nocrossβ request a resource that doesn't cross a nocross aligned boundary.minaddrβ specify the minimum address that can be allocated, orVMEM_ADDR_MINif the caller does not care.maxaddrβ specify the maximum address that 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 these three icons: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()
frees a resource allocated by vmem_xalloc() back to the arena.
vmβ the arena we free to.addrβ the resource being freed. it must be the one returned byvmem_xalloc(). and notably, it must not be the one fromvmem_alloc(). otherwise the behaviour is undefined. π¨ no mixing, bestie, i'm serious.sizeβ the size of the resource being freed. it must be the same as the size argument used forvmem_xalloc().
π vmem_alloc()
allocates a resource from the arena. the chill everyday version:
vmβ the arena we allocate from.sizeβ specify the size of the allocation.flagsβ a bitwise OR of avmemallocation 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()
frees a resource allocated by vmem_alloc() back to the arena.
vmβ the arena 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. π same rule, opposite direction, don't do it.sizeβ the size of the resource being freed. it must be the same as the size argument used forvmem_alloc().
π vmem_destroy()
destroys a vmem arena. RIP era.
vmβ thevmemarena being destroyed. the caller should ensure that no one will use it anymore. like, nobody. not even a little. π
β RETURN VALUES β
vmem_create() returns a pointer to the newly allocated vmem_t. otherwise, it returns NULL. π
on success, vmem_xalloc() and vmem_alloc() return 0. otherwise, ENOMEM is returned. that's the whole story, ngl.
π§ CODE REFERENCES π§
the vmem subsystem is implemented within the file sys/kern/subr_vmem.c. that's where the magic lives, iykyk π«
π SEE ALSO π
malloc(9)libuvmem(3)β for the userspace port of the allocator. yes, there's a userspace version, we love a spin-off π«Ά- Jeff Bonwick and Jonathan Adams, βMagazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resourcesβ, 2001 USENIX Annual Technical Conference, 2001. iconic academic behavior, honestly π
πΈ HISTORY πΈ
the vmem allocator was originally implemented in NetBSD. it was introduced in FreeBSD 10.0. glow-up journey fr fr β¨
π AUTHORS π
original implementation of vmem was written by YAMAMOTO Takashi. the FreeBSD port was made by Jeff Roberson. legends, both of them π
π BUGS π
vmem relies on malloc(9), so it cannot be used as early during system bootstrap. that's the one limitation, and it's real β plan around it, bestie.
okay that's the whole page, you ate that π go allocate something responsibly π¦
Debian Β· May 17, 2019 Β· that's the source, the date, and the version, all wrapped up with a bow π mwah π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c)2006 YAMAMOTO Takashi
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms