10. Roots¶
Roots tell the garbage collector where to start tracing. The garbage collector determines which blocks are reachable from the roots, and (in automatically managed pools) reclaims the unreachable blocks. This is quite efficient and can be a very good approximation to liveness.
It is therefore important that all references that the client program can directly access are registered as roots, otherwise the garbage collector might recycle an object that would be used in the future. Some collectors, for example Boehm’s, assume that all references stored in static data are roots; the Memory Pool System is more flexible, but requires the client program to declare which references are roots.
10.1. Registering roots¶
You can register a root at any time by calling one of the
mps_root_create
functions. Roots may not be registered twice, and
no two roots may overlap (that is, each reference is fixed by
at most one root). Roots may be:
in registers;
on the program’s control stack;
in the program’s static data;
in heap not managed by the MPS (provided that you destroy the root before freeing it; see the Scheme interpreter’s global symbol table for an example);
in manually managed pools (provided that you remove the root before freeing it).
Roots must not be in memory that is subject to garbage collection (and so roots must not be in automatically managed pools).
When you register a root you describe to the MPS how to scan
it for references, providing your own scanning function in the cases
of mps_root_create()
and mps_root_create_fmt()
. Such a
root scanning function must follow the Scanning protocol.
All the references in a root are of the same rank (just as in a formatted object). So they are all exact, ambiguous or weak.
Note
If the rank of the root is exact, or weak, the references in the root must always be valid while the root is registered: that is, they must be references to actual objects or null pointers. This could be immediately after the root is registered, so the root must be valid before it is registered.
Note
As with scanning in general, it’s safe to fix references that point to memory not managed by the MPS. These will be ignored.
Roots can be deregistered at any time by calling
mps_root_destroy()
. All roots registered in an arena
must be deregistered before the arena is destroyed.
There are five ways to register a root, depending on how you need to scan it for references:
mps_root_create()
if you need a custom root scanning function (of typemps_root_scan_t
);mps_root_create_fmt()
if the root consists of a block of objects belonging to an object format, which can be scanned by the format’s scan method (of typemps_fmt_scan_t
);mps_root_create_table()
if the root consists of a table of references;mps_root_create_table_masked()
if the root consists of a table of tagged references;mps_root_create_reg()
if the root consists of the registers and control stack of a thread. See Thread roots below.
10.2. Cautions¶
Creating a root and then registering is similar to reserving a block and then committing it (in the Allocation point protocol), and similar cautions apply. Before registering a root:
The root must be valid (that is, the appropriate root scanning function can scan it).
All exact references in the root (references that are fixed by the root scanning function) must contain valid references or null pointers.
You must not store a reference in the root to a block in an automatically managed pool (such a reference is hidden from the MPS until you register the root, and may become invalid).
So the typical sequence of operations when creating a root is:
Initialize references in the root with null pointers or other safe values.
Register the root.
Fill in the references in the root.
10.3. Thread roots¶
Every thread’s registers and control stack potentially contain
references to allocated objects, so should be registered as a root by
calling mps_root_create_reg()
. It’s not easy to write a
scanner for the registers and the stack: it depends on the operating
system, the processor architecture, and in some cases on the compiler.
For this reason, the MPS provides mps_stack_scan_ambig()
(and
in fact, this is the only supported stack scanner).
A stack scanner needs to know how to find the bottom of the part of the stack to scan. The bottom of the relevant part of stack can be found by taking the address of a local variable in the function that calls the main work function of your thread. You should take care to ensure that the work function is not inlined so that the address is definitely in the stack frame below any potential roots.
For example, here’s the code from the toy Scheme interpreter that registers a thread root and then calls the program:
mps_thr_t thread;
mps_root_t reg_root;
int exit_code;
void *marker = ▮
res = mps_thread_reg(&thread, arena);
if (res != MPS_RES_OK) error("Couldn't register thread");
res = mps_root_create_reg(®_root,
arena,
mps_rank_ambig(),
0,
thread,
mps_stack_scan_ambig,
marker,
0);
if (res != MPS_RES_OK) error("Couldn't create root");
exit_code = start(argc, argv);
mps_root_destroy(reg_root);
mps_thread_dereg(thread);
10.4. Ranks¶
-
mps_rank_t
¶ The type of ranks. It is a transparent alias for
unsigned int
, provided for convenience and clarity.
-
mps_rank_t
mps_rank_ambig
(void)¶ Return the rank of ambiguous roots.
-
mps_rank_t
mps_rank_exact
(void)¶ Return the rank of exact roots.
-
mps_rank_t
mps_rank_weak
(void)¶ Return the rank of weak roots.
10.5. Root modes¶
The root mode provides a way for the client to declare various facts about a root that allow the MPS to make optimizations. Roots that are declared to be constant need not be re-scanned, and roots that are declared to be protectable may have barriers placed on them, allowing the MPS to detect whether they have changed.
Note
The MPS does not currently perform either of these optimizations, so root modes have no effect. These features may be added in a future release.
-
mps_rm_t
¶ The type of root modes.
It should be zero (meaning neither constant or protectable), or the sum of some subset of
MPS_RM_CONST
andMPS_RM_PROT
.
-
MPS_RM_CONST
¶ Deprecated
starting with version 1.111.
This was introduced in the hope of being able to maintain a remembered set for the root without needing a write barrier, but it can’t work as described, since you can’t reliably create a valid registered constant root that contains any references. (If you add the references before registering the root, they may have become invalid; but you can’t add them afterwards because the root is supposed to be constant.)
The root mode for constant roots. This tells the MPS that the client program will not change the root after it is registered: that is, scanning the root will produce the same set of references every time. Furthermore, for roots registered by
mps_root_create_fmt()
andmps_root_create_table()
, the client program will not write to the root at all.
-
MPS_RM_PROT
¶ The root mode for protectable roots. This tells the MPS that it may place a barrier (1) on any page containing any part of the root. No format method or scan method (except for the one for this root) may write data in this root. They may read it.
Note
You must not specify
MPS_RM_PROT
on a root allocated by the MPS.No page may contain parts of two or more protectable roots. You mustn’t specify
MPS_RM_PROT
if the client program or anything other than (this instance of) the MPS is going to protect or unprotect the relevant pages.This mode may not be suitable if the client program wants the operating system to be able to access the root. Many operating systems can’t cope with writing to protected pages.
10.6. Root interface¶
-
mps_root_t
¶ The type of root descriptions.
The arena uses root descriptions to find references within the client program’s roots.
-
mps_res_t
mps_root_create
(mps_root_t *root_o, mps_arena_t arena, mps_rank_t rank, mps_rm_t rm, mps_root_scan_t root_scan, void *p, size_t s)¶ Register a root that consists of the references fixed by a scanning function.
root_o
points to a location that will hold the address of the new root description.arena
is the arena.rank
is the rank of references in the root.rm
is the root mode.root_scan
is the root scanning function. Seemps_root_scan_t
.p
ands
are arguments that will be passed toroot_scan
each time it is called. This is intended to make it easy to pass, for example, an array and its size as parameters.Returns
MPS_RES_OK
if the root was registered successfully,MPS_RES_MEMORY
if the new root description could not be allocated, or another result code if there was another error.The registered root description persists until it is destroyed by calling
mps_root_destroy()
.
-
mps_res_t
(*mps_root_scan_t)
(mps_ss_t ss, void *p, size_t s)¶ The type of root scanning functions for
mps_root_create()
.ss
is the scan state. It must be passed toMPS_SCAN_BEGIN()
andMPS_SCAN_END()
to delimit a sequence of fix operations, and to the functionsMPS_FIX1()
andMPS_FIX2()
when fixing a reference.p
ands
are the corresponding values that were passed tomps_root_create()
.Returns a result code. If a fix function returns a value other than
MPS_RES_OK
, the scan method must return that value, and may return without fixing any further references. Generally, it is better if it returns as soon as possible. If the scanning is completed successfully, the function should returnMPS_RES_OK
.
-
mps_res_t
mps_root_create_fmt
(mps_root_t *root_o, mps_arena_t arena, mps_rank_t rank, mps_rm_t rm, mps_fmt_scan_t fmt_scan, mps_addr_t base, mps_addr_t limit)¶ Register a root that consists of the references fixed by a scanning function in a block of formatted objects.
root_o
points to a location that will hold the address of the new root description.arena
is the arena.rank
is the rank of references in the root.rm
is the root mode.fmt_scan
is a scanning function. Seemps_fmt_scan_t
.base
is the address of the base of the block of formatted objects.limit
is the address just beyond the end of the block of formatted objects.Returns
MPS_RES_OK
if the root was registered successfully,MPS_RES_MEMORY
if the new root description could not be allocated, or another result code if there was another error.The registered root description persists until it is destroyed by calling
mps_root_destroy()
.
-
mps_res_t
mps_root_create_reg
(mps_root_t *root_o, mps_arena_t arena, mps_rank_t rank, mps_rm_t rm, mps_thr_t thr, mps_reg_scan_t reg_scan, void *p, size_t s)¶ Register a root that consists of the references fixed in a thread’s stack by a scanning function.
root_o
points to a location that will hold the address of the new root description.arena
is the arena.rank
is the rank of references in the root.rm
is the root mode.thr
is the thread.reg_scan
is a scanning function. Seemps_reg_scan_t
.p
ands
are arguments that will be passed toreg_scan
each time it is called. This is intended to make it easy to pass, for example, an array and its size as parameters.Returns
MPS_RES_OK
if the root was registered successfully,MPS_RES_MEMORY
if the new root description could not be allocated, or another result code if there was another error.The registered root description persists until it is destroyed by calling
mps_root_destroy()
.Note
It is not supported for client programs to pass their own scanning functions to this function. The built-in MPS function
mps_stack_scan_ambig()
must be used.This function is intended as a hook should we ever need to allow client-specific extension or customization of stack and register scanning. If you’re in a position where you need this, for example, if you’re writing a compiler and have control over what goes in the registers, contact us.
-
mps_res_t
(*mps_reg_scan_t)
(mps_ss_t ss, mps_thr_t thr, void *p, size_t s)¶ The type of a root scanning function for roots created with
mps_root_create_reg()
.ss
is the scan state. It must be passed toMPS_SCAN_BEGIN()
andMPS_SCAN_END()
to delimit a sequence of fix operations, and to the functionsMPS_FIX1()
andMPS_FIX2()
when fixing a reference.thr
is the thread.p
ands
are the corresponding values that were passed tomps_root_create_reg()
.Returns a result code. If a fix function returns a value other than
MPS_RES_OK
, the scan method must return that value, and may return without fixing any further references. Generally, it is better if it returns as soon as possible. If the scanning is completed successfully, the function should returnMPS_RES_OK
.A root scan method is called whenever the MPS needs to scan the root. It must then indicate references within the root by calling
MPS_FIX1()
andMPS_FIX2()
.See also
Note
Client programs are not expected to write scanning functions of this type. The built-in MPS function
mps_stack_scan_ambig()
must be used.
-
mps_reg_scan_t
mps_stack_scan_ambig
()¶ A root scanning function for ambiguous scanning of threads, suitable for passing to
mps_root_create_reg()
.It scans all integer registers and everything on the stack of the thread given, and can therefore only be used with ambiguous roots. It only scans locations that are at, or higher on the stack (that is, more recently added), the stack bottom that was passed to
mps_thread_reg()
. References are assumed to be represented as machine words, and are required to be 4-byte-aligned; unaligned values are ignored.Note
The MPS provides this function because it’s hard to write: it depends on the operating system, the processor architecture, and in some cases on the compiler.
-
mps_res_t
mps_root_create_table
(mps_root_t *root_o, mps_arena_t arena, mps_rank_t rank, mps_rm_t rm, mps_addr_t *base, size_t count)¶ Register a root that consists of a vector of references.
root_o
points to a location that will hold the address of the new root description.arena
is the arena.rank
is the rank of references in the root.rm
is the root mode.base
points to a vector of references.count
is the number of references in the vector.Returns
MPS_RES_OK
if the root was registered successfully,MPS_RES_MEMORY
if the new root description could not be allocated, or another result code if there was another error.The registered root description persists until it is destroyed by calling
mps_root_destroy()
.Warning
The
base
argument has typemps_addr_t*
(a typedef forvoid**
) but the table of references most likely has some other pointer type,my_object*
say. It is tempting to write:mps_root_create_table(..., (mps_addr_t *)my_table, ...)
but this is type punning, and its behaviour is not defined in ANSI/ISO Standard C. (GCC and Clang have a warning flag
-Wstrict-aliasing
which detects some errors of this form.)To ensure well-defined behaviour, the pointer must be converted via
void*
(or viamps_addr_t
, which is a typedef forvoid*
), like this:mps_addr_t base = my_table; mps_root_create_table(..., base, ...)
-
mps_res_t
mps_root_create_table_masked
(mps_root_t *root_o, mps_arena_t arena, mps_rank_t rank, mps_rm_t rm, mps_addr_t *base, size_t count, mps_word_t mask)¶ Register a root that consists of a vector of tagged references.
root_o
points to a location that will hold the address of the new root description.arena
is the arena.rank
is the rank of references in the root.rm
is the root mode.base
points to a vector of tagged references.count
is the number of tagged references in the vector.mask
is a bitmask whose set bits specify the location of the tag. References are assumed to have a tag of zero: any value in the vector with a non-zero tag is ignored.Returns
MPS_RES_OK
if the root was registered successfully,MPS_RES_MEMORY
if the new root description could not be allocated, or another result code if there was another error.The registered root description persists until it is destroyed by calling
mps_root_destroy()
.For example:
#define TAG_MASK 0x3 /* bottom two bits */ /* Global symbol table. */ size_t symtab_size; struct { obj_t symbol; obj_t value; } *symtab; mps_res_t res; mps_root_t root; mps_addr_t base = symtab; res = mps_root_create_table_masked(&root, arena, mps_rank_exact(), (mps_rm_t)0, base, symtab_size * 2, (mps_word_t)TAG_MASK); if (res != MPS_RES_OK) errror("can't create symtab root");
Warning
See the warning for
mps_root_create_table()
above.
-
void
mps_root_destroy
(mps_root_t root)¶ Deregister a root and destroy its description.
root
is the root.
10.7. Root introspection¶
-
void
mps_arena_roots_walk
(mps_arena_t arena, mps_roots_stepper_t f, void *p, size_t s)¶ Deprecated
starting with version 1.111.
If you think you need this, there’s probably a better way to achieve what you’re trying to do. Contact us.
Visit references in registered roots in an arena.
arena
is the arena whose roots you want to visit.f
is a function that will be called for each reference to an object in an automatically managed pool class that was found in a registered root belonging to the arena. It takes four arguments:ref
is the address of a reference to an object in the arena,root
is the root in whichref
was found, andp
ands
are the corresponding arguments that were passed tomps_arena_roots_walk()
.p
ands
are arguments that will be passed tof
each time it is called. This is intended to make it easy to pass, for example, an array and its size as parameters.This function may only be called when the arena is in the parked state.
See also
Note
If a root is ambiguous then the reference might not be to the start of an object; the client program should handle this case. There is no guarantee that the reference corresponds to the actual location that holds the pointer to the object (since this might be a register, for example), but the actual location will be passed if possible. This may aid analysis of roots via a debugger.
-
void
(*mps_roots_stepper_t)
(mps_addr_t *ref, mps_root_t root, void *p, size_t s)¶ The type of a root stepper function.
A function of this type can be passed to
mps_arena_roots_walk()
, in which case it will be called for each reference into the arena from a root registered with the arena. It receives four arguments:ref
points to a reference in a root. The reference points to something in the arena. If the root is exact then the reference points to the start of an allocated block, but if the root is ambiguous it might point to somewhere in the middle of an allocated block.root
is the description of the root which containsref
.p
ands
are the corresponding values that were passed tomps_arena_roots_walk()
.