The need for some means of describing objects in the client program comes from tracing and moving. During tracing, when an object is scanned, all the references in the object must be identified so that the objects they point to can be scanned in their turn. When an object has moved, references to that object must be identified so that they can be updated to point to the new location of the object.
In general, only the client program can say which fields in an object are references, and only the client program knows how references are represented (for example, are they tagged?). Object formats provide the means by which the client program communicates this information to the MPS.
An object format is a collection of format methods and other (usually scalar) values which together describe programatically the layout of objects belonging to the format. Format methods include the skip method (which calculates an object’s size), the scan method (which fixes references in the object), and the forward method (which replaces an object that has moved with a forwarding object).
Not every pool class supports formatted objects.
The type of an object format.
Create an object format.
fmt_o points to a location that will hold the address of the new object format.
arena is the arena in which to create the format.
args are keyword arguments describing the format. Each pool class requires a particular subset of these keyword arguments: see the documentation for that pool class.
mps_fmt_create_k() returns MPS_RES_OK if successful. The MPS may exhaust some resource in the course of mps_fmt_create_k() and will return an appropriate result code if so.
The object format pointed to by fmt_o persists until it is destroyed by calling mps_fmt_destroy().
For example:
MPS_ARGS_BEGIN(args) {
MPS_ARGS_ADD(args, MPS_KEY_FMT_ALIGN, ALIGNMENT);
MPS_ARGS_ADD(args, MPS_KEY_FMT_SCAN, obj_scan);
MPS_ARGS_ADD(args, MPS_KEY_FMT_SKIP, obj_skip);
MPS_ARGS_ADD(args, MPS_KEY_FMT_FWD, obj_fwd);
MPS_ARGS_ADD(args, MPS_KEY_FMT_ISFWD, obj_isfwd);
MPS_ARGS_ADD(args, MPS_KEY_FMT_PAD, obj_pad);
res = mps_fmt_create_k(&obj_fmt, arena, args);
} MPS_ARGS_END(args);
if (res != MPS_RES_OK) error("Couldn't create obj format");
Destroy an object format.
fmt is the object format to destroy.
It is an error to destroy an object format if there exists a pool using the format. The pool must be destroyed first.
There are use cases in which it is convenient for the client program’s pointers to point some distance into the memory block containing the object. This typically happens when the objects have a common in-band header used for memory management or class system purposes, but this situation also arises when the low bits of a pointer are used for a tag. The MPS does not care what the reason is, only about the offset of the pointer in relation to the memory block.
If you have one of these use cases, you should pass the MPS_KEY_FMT_HEADER_SIZE keyword argument to mps_fmt_create_k(), specifying the size of the header: that is, the offset of a client pointer from the base of the memory block.
There are some cautions to be observed when using in-band headers:
The format methods (other than the padding method) receive client pointers (that is, pointers past the header) but all other MPS functions expect to receive and return base pointers (that is, pointers to the base of the block where the header is stored).
In particular, mps_reserve() and mps_alloc() always hand out base pointers, and mps_free() expects to receive one.
Formatted objects must be longer than the header. In other words, objects consisting of only a header are not supported.
Even if the header size is larger than or equal to alignment, the padding method must still be able to create padding objects down to the alignment size.
Not all pool classes support objects with in-band headers. See the documentation for the pool class.
Note
A client program that allocates objects with in-band headers has to make a choice about how to represent references to those objects. It can represent them using base pointers (which is convenient for allocation, since mps_reserve() returns a base pointer, but requires decoding when scanning) or using client pointers (which is convenient for scanning, since the scan method takes a client pointer, but requires encoding on allocation). Either approach will work, but client pointers are normally the better choice, since scanning is normally more performance-critical than allocation.
The MPS guarantees that format methods have exclusive access to the object for the duration of the call. This guarantee may entail suspending arbitrary threads. The methods that manipulate the object must not perform any sort of inter-thread locking or communication.
The MPS may call format methods in the context of an exception handler or a signal handler. For example, the following sequence of events is common:
Therefore, the format methods must be able to be run at any time, including asynchronously or in parallel with the rest of the program.
Format methods must be re-entrant.
Format methods must use no more than 64 words of stack space.
This restriction is necessary to avoid stack overflow in the MPS; see Stack probe for details. If your application has format methods that need more stack space than this, contact us.
Format methods must not:
It’s permissible to call other functions in the client program, but see MPS_FIX_CALL() for a restriction on passing the scan state.
Subject to the above constraints, format methods can freely access:
They must not access other memory managed by the MPS.
The type of the class method of an object format.
addr is the address of the object whose class is of interest.
Returns an address that is related to the class or type of the object, or a null pointer if this is not possible.
It is recommended that a null pointer be returned for padding objects and forwarding objects.
The type of the forward method of an object format.
old is the address of an object.
new is the address to where the object has been moved.
The MPS calls the forward method for an object format when it has relocated an object belonging to that format. The forward method must replace the object at old with a forwarding marker that points to the address ‘new’. The forwarding marker must meet the following requirements:
Note
This method is never invoked by the garbage collector on an object in a non-moving pool.
The type of the is-forwarded method of an object format.
addr is the address of a candidate object.
If the addr is the address of a forwarding object, return the address where the object was moved to. This must be the value of the new argument supplied to the forward method when the object was moved. If not, return a null pointer.
Note
This method is never invoked by the garbage collector on an object in a non-moving pool.
The type of the padding method of an object format.
addr is the address at which to create a padding object.
size is the size of the padding object to be created.
The MPS calls a padding method when it wants to create a padding object. Typically the MPS creates padding objects to fill in otherwise unused gaps in memory; they allow the MPS to pack objects into fixed-size units (such as operating system pages).
The padding method must create a padding object of the specified size at the specified address. The size can be any aligned (to the format alignment) size. A padding object must be acceptable to other methods in the format (the scan method, the skip method, and so on).
Note
The padding method always receives a base pointer, even if the object format has a non-zero MPS_KEY_FMT_HEADER_SIZE.
Note
The MPS will ask for padding objects of any size aligned to the pool alignment, no matter what size objects the pool holds. For example, a pool holding only two-word objects may still be asked to create padding objects 2048 bytes long.
The type of the scan method of an object format.
ss is the scan state. It must be passed to MPS_SCAN_BEGIN() and MPS_SCAN_END() to delimit a sequence of fix operations, and to the functions MPS_FIX1() and MPS_FIX2() when fixing a reference.
base points to the first formatted object in the block of memory to be scanned.
limit points to the location just beyond the end of the block to be scanned. Note that there might not be any object at this location.
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 return MPS_RES_OK.
The scan method for an object format is called when the MPS needs to scan objects in a block of memory containing objects belonging to that format. The scan method is called with a scan state and the base and limit of the block of objects to scan. It must then indicate references within the objects by calling MPS_FIX1() and MPS_FIX2().
If the object format is capable of creating forwarding objects or padding objects, the scan method must be able to scan these objects. (In the case of the forwarding object, the scan method should not fix the pointer to the new location.)
See also
The type of the skip method of an object format.
addr is the address of the object to be skipped.
Returns the address of the “next object”. In an object format without in-band headers, this is the address just past the end of this object. In an object format with in-band headers, it’s the address just past where the header of next object would be, if there were one.
Note
In either case, the result is the sum of addr and the size of the block containing the object.
If the object format is capable of creating forwarding objects or padding objects, the skip method must be able to skip these objects.
A skip method is not allowed to fail.
Note
The MPS uses this method to determine the size of objects (by subtracting addr from the result) as well as skipping over them.
Determine the object format to which an address belongs.
fmt_o points to a location that will hold the address of the object format, if one is found.
arena is the arena whose object formats will be considered.
addr is the address.
If addr is the address of a location inside a block allocated from a pool in arena, and that pool has an object format, then update the location pointed to by fmt_o with the address of the object format, and return true.
If addr is the address of a location inside a block allocated from a pool in arena, but that pool has no object format, return false.
If addr points to a location that is not managed by arena, return false.
If none of the above conditions is satisfied, mps_addr_fmt() may return either true or false.
Note
This function might return a false positive by returning true if you ask about an address that happens to be inside memory managed by a pool with an object format, but which is not inside a block allocated by that pool. It never returns a false negative.
Visit all formatted objects in an arena.
arena is the arena whose formatted objects you want to visit.
f is a formatted objects stepper function. It will be called for each formatted object in the arena. See mps_formatted_objects_stepper_t.
p and s are arguments that will be passed to f each time it is called. This is intended to make it easy to pass, for example, an array and its size as parameters.
Each pool class determines for which objects the stepper function is called. Typically, all validly formatted objects are visited. Padding objects may be visited at the pool class’s discretion: the stepper function must handle this case.
Note
This function is intended for heap analysis, tuning, and debugging, not for frequent use in production.
Warning
If a garbage collection is currently in progress (that is, if the arena is in the clamped or unclamped state), then only objects that are known to be currently valid are visited.
For the most reliable results, ensure the arena is in the parked state by calling mps_arena_park() before calling this function (and release it by calling mps_arena_release() afterwards, if desired).
The type of a formatted objects stepper function.
A function of this type can be passed to mps_arena_formatted_objects_walk(), in which case it will be called for each formatted object in an arena. It receives five arguments:
addr is the address of the object.
fmt is the object format for that object.
pool is the pool to which the object belongs.
p and s are the corresponding values that were passed to mps_arena_formatted_objects_walk().
The function may not call any function in the MPS. It may access:
It must not access other memory managed by the MPS.