Barrelfish
Functions
mm.c File Reference

Memory manager. More...

Functions

void mm_debug_print (struct mmnode *mmnode, int space)
 Debug printout of the status of all nodes. More...
 
errval_t mm_init (struct mm *mm, enum objtype objtype, genpaddr_t base, uint8_t sizebits, uint8_t maxchildbits, slab_refill_func_t slab_refill_func, slot_alloc_t slot_alloc_func, slot_refill_t slot_refill_func, void *slot_alloc_inst, bool delete_chunked)
 Initialise a memory manager instance. More...
 
void mm_destroy (struct mm *mm)
 Destroy a memory manager instance. More...
 
errval_t mm_add (struct mm *mm, struct capref cap, uint8_t sizebits, genpaddr_t base)
 Add a new region to the memory manager. More...
 
errval_t mm_add_multi (struct mm *mm, struct capref cap, gensize_t size, genpaddr_t base)
 Add a new region to the memory manager. The region does not need to be power-of-two sized or aligned. More...
 
errval_t mm_alloc (struct mm *mm, uint8_t sizebits, struct capref *retcap, genpaddr_t *retbase)
 Allocate an arbitrary memory region of a given size. More...
 
errval_t mm_alloc_range (struct mm *mm, uint8_t sizebits, genpaddr_t minbase, genpaddr_t maxlimit, struct capref *retcap, genpaddr_t *retbase)
 Allocate memory region of a given size within a given address range. More...
 
errval_t mm_realloc_range (struct mm *mm, uint8_t sizebits, genpaddr_t base, struct capref *retcap)
 Return cap to a specific region, that may be partially allocated. More...
 
errval_t mm_free (struct mm *mm, struct capref cap, genpaddr_t base, uint8_t sizebits)
 Free an allocated region. More...
 
size_t mm_relinquish_all (struct mm *mm, struct mem_cap *ret, size_t retlen)
 Fills an array with metadata for all free regions. More...
 
size_t mm_relinquish_range (struct mm *mm, genpaddr_t base, genpaddr_t limit, struct mem_cap *ret, size_t retlen)
 Fills an array with metadata for all free regions within a given range. More...
 

Detailed Description

Memory manager.

This code manages memory regions (which may refer to actual RAM, or physical address space, or other spaces) and the capabilities to those regions.

The meta-data is structured as a B-tree. Every node has a variable power-of-two-sized number of children (up to a limit fixed at initialisation time), which is stored in the node itself (as "childbits"). Nodes without any children have childbits = -1 (FLAGBITS).

The position of a node in the tree exactly determines its size and address relative to the size and address of the region being managed by the allocator (all sizes must be powers of two).

A node may be one of four types (see nodetype): 0. A "dummy" node, which exists structurally in the tree, but for which we do not have a capability (ie. the region is incomplete).

  1. A "chunked" node, for which we have a capability, but which has been split up into child nodes for smaller allocations.
  2. A free node, which is a regular free child node in the tree.
  3. An allocated node.

Function Documentation

errval_t mm_add ( struct mm mm,
struct capref  cap,
uint8_t  sizebits,
genpaddr_t  base 
)

Add a new region to the memory manager.

It is an error if any part of the region has already been added, or the region doesn't fit within the base and size specified for the allocator.

Parameters
mmMemory manager instance
capCapability to newly-added region
sizebitsSize of region
basePhysical base address of region
errval_t mm_add_multi ( struct mm mm,
struct capref  cap,
gensize_t  size,
genpaddr_t  base 
)

Add a new region to the memory manager. The region does not need to be power-of-two sized or aligned.

It is an error if any part of the region has already been added, or the region doesn't fit within the base and size specified for the allocator.

Parameters
mmMemory manager instance
capCapability to newly-added region
sizeSize of region
basePhysical base address of region
errval_t mm_alloc ( struct mm mm,
uint8_t  sizebits,
struct capref retcap,
genpaddr_t *  retbase 
)

Allocate an arbitrary memory region of a given size.

Parameters
mmMemory manager instance
sizebitsSize of requested region
retcapPointer to capref struct, to be filled-in
retbaseIf non-NULL, the base address of the allocated region is returned here
errval_t mm_alloc_range ( struct mm mm,
uint8_t  sizebits,
genpaddr_t  minbase,
genpaddr_t  maxlimit,
struct capref retcap,
genpaddr_t *  retbase 
)

Allocate memory region of a given size within a given address range.

If this call succeeds, it must be the case that: *retbase >= minbase && *retbase + (1UL << sizebits) <= maxlimit

Parameters
mmMemory manager instance
sizebitsSize of requested region
minbaseMinimum base address of region to allocate
maxlimitMaximum limit address of region to allocate
retcapPointer to capref struct, to be filled-in
retbaseIf non-NULL, the base address of the allocated region is returned here
void mm_debug_print ( struct mmnode mmnode,
int  space 
)

Debug printout of the status of all nodes.

Parameters
mmnodeStruct mmnode to print from
spaceCall with 0, used to track depth for pretty printing
void mm_destroy ( struct mm mm)

Destroy a memory manager instance.

Parameters
mmMemory manager instance
errval_t mm_free ( struct mm mm,
struct capref  cap,
genpaddr_t  base,
uint8_t  sizebits 
)

Free an allocated region.

Marks the region (which must previously have been allocated) as free.

Parameters
mmMemory manager instance
capCap to re-insert (specify NULL_CAP if delete_chunked == false)
basePhysical base address of region
sizebitsSize of region
errval_t mm_init ( struct mm mm,
enum objtype objtype  ,
genpaddr_t  base,
uint8_t  sizebits,
uint8_t  maxchildbits,
slab_refill_func_t  slab_refill_func,
slot_alloc_t  slot_alloc_func,
slot_refill_t  slot_refill_func,
void *  slot_alloc_inst,
bool  delete_chunked 
)

Initialise a memory manager instance.

Parameters
mmPointer to memory manager instance, to be filled-in
objtypeKernel object type to be managed
baseBase address of region to be managed
sizebitsSize (in bits) of region to be managed
maxchildbitsMaximum number of children (in bits) at each node
slab_refill_funcFunction to be used to refill slab allocator If this is NULL, the caller must provide static storage with slab_grow.
slot_alloc_funcSlot allocator function
slot_refill_funcSlot allocator refill function
slot_alloc_instSlot allocator opaque instance pointer
delete_chunkedWhether to delete chunked caps
Note
Setting maxchildbits > 1 saves substantial space, but may lead to the situation where an allocation request cannot be satisfied despite memory being available, because the memory has been chunked up into smaller caps that, while free, cannot be recombined without revoking existing allocations.
errval_t mm_realloc_range ( struct mm mm,
uint8_t  sizebits,
genpaddr_t  base,
struct capref retcap 
)

Return cap to a specific region, that may be partially allocated.

The parameters to this function specify a fixed memory region (base and size) that may overlap one or more already-allocated memory regions. It marks the entire region as allocated, returning a cap to it.

Warning
It is assumed that the caller knows what they are doing.
Parameters
mmMemory manager instance
sizebitsSize of region
baseBase address of region
retcapPointer to capref struct, to be filled-in
size_t mm_relinquish_all ( struct mm mm,
struct mem_cap ret,
size_t  retlen 
)

Fills an array with metadata for all free regions.

The regions returned are marked as allocated.

Parameters
mmMemory manager instance
retPointer to return array to be filled-in
retlenLength of the array, in slots
Returns
Number of caps that could have been stored in the return array. If this is less than or equal to #retlen, all caps have been returned.
size_t mm_relinquish_range ( struct mm mm,
genpaddr_t  base,
genpaddr_t  limit,
struct mem_cap ret,
size_t  retlen 
)

Fills an array with metadata for all free regions within a given range.

The regions returned are marked as allocated.

It must be the case for every returned cap (i) that: ret[i]->base >= base && ret[i]->base + (1UL << ret[i]->sizebits) <= limit

Parameters
mmMemory manager instance
baseBase address of range from which to relinquish memory
limitLimit address of range from which to relinquish memory
retPointer to return array to be filled-in
retlenLength of the array, in slots
Returns
Number of caps that could have been stored in the return array. If this is less than or equal to #retlen, all caps have been returned.