Coalescing block structures =========================== .. mps:prefix:: design.mps.cbs Introduction ------------ :mps:tag:`intro` This is the design for impl.c.cbs, which implements a data structure for the management of non-intersecting memory ranges, with eager coalescence. :mps:tag:`readership` This document is intended for any MM developer. :mps:tag:`source` design.mps.poolmvt_, design.mps.poolmvff_. .. _design.mps.poolmvt: poolmvt .. _design.mps.poolmvff: poolmvff :mps:tag:`overview` The "coalescing block structure" is a set of addresses (or a subset of address space), with provision for efficient management of contiguous ranges, including insertion and deletion, high level communication with the client about the size of contiguous ranges, and detection of protocol violations. Requirements ------------ In addition to the generic land requirements (see design.mps.land_), the CBS must satisfy: .. _design.mps.land: land :mps:tag:`req.fast` Common operations must have a low amortized cost. :mps:tag:`req.small` Must have a small space overhead for the storage of typical subsets of address space and not have abysmal overhead for the storage of any subset of address space. Interface --------- :mps:tag:`land` CBS is an implementation of the *land* abstract data type, so the interface consists of the generic functions for lands. See design.mps.land_. External types .............. .. c:type:: struct CBSStruct *CBS :mps:tag:`type.cbs` The type of coalescing block structures. A :c:type:`CBSStruct` may be embedded in another structure, or you can create it using :c:func:`LandCreate()`. External functions .................. .. c:function:: LandClass CBSLandClassGet(void) :mps:tag:`function.class` The function :c:func:`CBSLandClassGet()` returns the CBS class, a subclass of :c:type:`LandClass` suitable for passing to :c:func:`LandCreate()` or :c:func:`LandInit()`. .. c:function:: LandClass CBSFastLandClassGet(void) :mps:tag:`function.class` Returns a subclass of :c:type:`CBSLandClass` that maintains, for each subtree, the size of the largest block in that subtree. This enables the :c:func:`LandFindFirst()`, :c:func:`LandFindLast()`, and :c:func:`LandFindLargest()` generic functions. .. c:function:: LandClass CBSZonedLandClassGet(void) :mps:tag:`function.class` Returns a subclass of :c:type:`CBSFastLandClass` that maintains, for each subtree, the union of the zone sets of all ranges in that subtree. This enables the :c:func:`LandFindInZones()` generic function. Keyword arguments ................. When initializing a CBS, :c:func:`LandCreate()` and :c:func:`LandInit()` take the following optional keyword arguments: * ``CBSBlockPool`` (type :c:type:`Pool`) is the pool from which the CBS block descriptors will be allocated. If omitted, a new MFS pool is created for this purpose. * :c:macro:`MPS_KEY_CBS_EXTEND_BY` (type :c:type:`Size`; default 4096) is passed as the :c:macro:`MPS_KEY_EXTEND_BY` keyword argument to :c:func:`PoolCreate()` if a block descriptor pool is created. It specifies the size of segment that the block descriptor pool will request from the arena. * ``MFSExtendSelf`` (type :c:type:`Bool`; default :c:macro:`TRUE`) is passed to :c:func:`PoolCreate()` if a block descriptor pool is created. If :c:macro:`TRUE`, the block descriptor pool automatically extends itself when out of space; if :c:macro:`FALSE`, the pool returns ``ResLIMIT`` in this case. (This feature is used by the arena to bootstrap its own CBS of free memory.) Limitations ........... :mps:tag:`limit.find` :c:type:`CBSLandClass` does not support the :c:func:`LandFindFirst()`, :c:func:`LandFindLast()`, and :c:func:`LandFindLargest()` generic functions (the subclasses do support these operations). :mps:tag:`limit.zones` :c:type:`CBSLandClass` and :c:type:`CBSFastLandClass` do not support the :c:func:`LandFindInZones()` generic function (the subclass :c:type:`CBSZonedLandClass` does support this operation). :mps:tag:`limit.iterate` CBS does not provide an implementation for the :c:func:`LandIterateAndDelete()` generic function. This is because :c:func:`TreeTraverse()` does not permit modification, for speed and to avoid perturbing the splay tree balance. :mps:tag:`limit.flush` CBS cannot be used as the source in a call to :c:func:`LandFlush()`. (Because of :mps:ref:`.limit.iterate`.) Implementation -------------- Splay tree .......... :mps:tag:`impl.splay` The CBS is implemented using a splay tree (see design.mps.splay_). Each splay tree node is embedded in a block structure that represents a semi-open address range. The key passed for comparison is the base of another range. .. _design.mps.splay: splay :mps:tag:`impl.splay.fast-find` In the :c:type:`CBSFastLandClass` class, :c:func:`cbsFindFirst()` and :c:func:`cbsFindLast()` use the update/refresh facility of splay trees to store, in each block, an accurate summary of the maximum block size in the tree rooted at the corresponding splay node. This allows rapid location of the first or last suitable block, and very rapid failure if there is no suitable block. :mps:tag:`impl.find-largest` :c:func:`cbsFindLargest()` simply finds out the size of the largest block in the CBS from the root of the tree, using :c:func:`SplayRoot()`, and does :c:func:`SplayFindFirst()` for a block of that size. This takes time proportional to the logarithm of the size of the free list, so it's about the best you can do without maintaining a separate priority queue, just to do :c:func:`cbsFindLargest()`. :mps:tag:`impl.splay.zones` In the :c:type:`CBSZonedLandClass` class, :c:func:`cbsFindInZones()` uses the update/refresh facility of splay trees to store, in each block, the union of the zones of the ranges in the tree rooted at the corresponding splay node. This allows rapid location of a block in a set of zones. Low memory behaviour .................... :mps:tag:`impl.low-mem` When the CBS tries to allocate a new ``CBSBlock`` structure for a new isolated range as a result of either :c:func:`LandInsert()` or :c:func:`LandDelete()`, and there is insufficient memory to allocate the block structure, then the range is not added to the CBS or deleted from it, and the call to :c:func:`LandInsert()` or :c:func:`LandDelete()` returns ``ResMEMORY``. The CBS block ............. :mps:tag:`impl.cbs.block` The block contains a base-limit pair and a splay tree node. :mps:tag:`impl.cbs.block.special` The base and limit may be equal if the block is halfway through being deleted. :mps:tag:`impl.cbs.block.special.just` This conflates values and status, but is justified because block size is very important. Testing ------- :mps:tag:`test` The following testing will be performed on this module: :mps:tag:`test.land` A generic test for land implementations. See design.mps.land.test. :mps:tag:`test.pool` The arena and two pools (MVT_ and MVFF_) are implemented on top of a CBS. These are subject to testing in development, QA, and are heavily exercised by customers. .. _MVT: poolmvt .. _MVFF: poolmvff Notes for future development ---------------------------- :mps:tag:`future.not-splay` The implementation of CBSs is based on splay trees. It could be revised to use other data structures that meet the requirements (especially :mps:ref:`.req.fast`). :mps:tag:`future.hybrid` It would be possible to attenuate the problem of :mps:ref:`.risk.overhead` (below) by using a single word bit set to represent the membership in a (possibly aligned) word-width of grains. This might be used for block sizes less than a word-width of grains, converting them when they reach all free in the bit set. Note that this would make coalescence slightly less eager, by up to ``(word-width - 1)``. :mps:tag:`future.iterate.and.delete` It would be possible to provide an implementation for the :c:func:`LandIterateAndDelete()` generic function by calling :c:func:`TreeToVine()` first, and then iterating over the vine (where deletion is straightforward). Risks ----- :mps:tag:`risk.overhead` Clients should note that the current implementation of CBSs has a space overhead proportional to the number of isolated contiguous ranges. [Four words per range.] If the CBS contains every other grain in an area, then the overhead will be large compared to the size of that area. [Four words per two grains.] The CBS structure is thus suitable only for managing large enough ranges.