The
vmem is a general purpose resource allocator. Despite its name, it can be used for arbitrary resources other than virtual memory.
vmem_create() creates a new vmem arena.
name
The string to describe the vmem.
base
The start address of the initial span. It can be VMEM_ADDR_NULL if no initial span is required.
size
The size of the initial span.
quantum
The smallest unit of allocation.
allocfn
The callback function used to import spans from the backend arena. Set both allocfn and freefn to NULL to disable automatic imports. vmem calls (*allocfn)(source, size, &actualsize, flags); to import a span of size at least size. allocfn should accept the same flags as vmem_alloc(). allocfn must return VMEM_ADDR_NULL to indicate failure, or else the starting address of the imported span. If allocfn succeeds, it must write the actual size of the allocation to actualsize. The actual size will always be greater than or equal to the requested size.
freefn
The callback function used to free spans to the backend arena. freefn may not be NULL unless allocfn is NULL. vmem calls (*freefn)(source, addr, size) to return to source a span of size size, starting at addr, that was previously allocated by allocfn.
source
The backend arena. source may be NULL. vmem passes source as the first argument of allocfn and freefn.
qcache_max
The largest size of allocations which can be served by quantum cache. It is merely a hint and can be ignored.
flags
Either of:
VM_SLEEP
Can sleep until enough resources are available.
VM_NOSLEEP
Don't sleep. Immediately return NULL if there are not enough resources available.
ipl
Interrupt level to be blocked for allocating from vmem.
vmem_add() adds a span of size
size starting at
addr to the arena. Returns
addr on success.
flags should be one of:
VM_SLEEP
Can sleep until enough resources are available.
VM_NOSLEEP
Don't sleep. Immediately return VMEM_ADDR_NULL if there are not enough resources available.
vmem_xalloc() allocates a resource from the arena.
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
If non-zero, specify the minimum address which can be allocated.
maxaddr
If non-zero, specify the maximum address + 1 which can be allocated.
flags
A bitwise OR of an allocation strategy and a sleep flag.
The allocation strategy is one of:
VM_BESTFIT
Prefer space efficiency.
VM_INSTANTFIT
Prefer performance.
The sleep flag should be one of:
VM_SLEEP
Can sleep until enough resources are available.
VM_NOSLEEP
Don't sleep. Immediately return VMEM_ADDR_NULL if there are not enough resources available.
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 by vmem_xalloc(). Notably, it must not be the one from vmem_alloc(). Otherwise, the behaviour is undefined.
size
The size of the resource being freed. It must be the same as the size argument used for vmem_xalloc().
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 allocation strategy and a sleep flag.
The allocation strategy is one of:
VM_BESTFIT
Prefer space efficiency.
VM_INSTANTFIT
Prefer performance.
The sleep flag should be one of:
VM_SLEEP
Can sleep until enough resources are available.
VM_NOSLEEP
Don't sleep. Immediately return VMEM_ADDR_NULL if there are not enough resources available.
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 by vmem_alloc(). Notably, it must not be the one from vmem_xalloc(). Otherwise, the behaviour is undefined.
size
The size of the resource being freed. It must be the same as the size argument used for vmem_alloc().
vmem_destroy() destroys a vmem arena.
vm
The vmem arena being destroyed. The caller should ensure that no one will use it anymore.