29. Ranges of addresses

29.1. Introduction

.intro: This is the design of the Range module, which implements objects representing address ranges.

.readership: This document is intended for any MPS developer.

29.2. Requirements

.req.range: A range object must be able to represent an arbitrary range of addresses that neither starts at NULL nor includes the top grain of the address space.

.req.empty: A range object must be able to represent the empty range.

.req.stack-alloc: It must be possible to allocate range objects on the stack: that is, they do not require any heap resource.

29.3. Interface

RangeStruct *Range

Range is the type of a range. It is an alias for RangeStruct*. RangeStruct is defined in the header so that it can be inlined in client structures or allocated on the stack. Clients must not depend on its implementation details.

void RangeInit(Range range, Addr base, Addr limit)

Initialize a range object to represent the half-open address range between base (inclusive) and limit (exclusive). It must be the case that base <= limit. If base == limit then the range is empty.

void RangeCopy(Range dest, Range src)

Initialize dest to be a copy of src.

void RangeInitSize(Range range, Addr base, Size size)

Initialize a range object to represent the half-open address range between base (inclusive) and base + size (exclusive). If size == 0 then the range is empty.

void RangeFinish(Range range)

Finish a range object. Because a range object uses no heap resources (.req.stack-alloc) it is not necessary to call this. However, clients may wish to do so in order to ensure that the range object is invalid.

Addr RangeBase(Range range)

Return the base of the range. (This is implemented as a macro, but there is a function too.)

Addr RangeLimit(Range range)

Return the limit of the range. (This is implemented as a macro, but there is a function too.)

void RangeSetBase(Range range, Addr addr)

Set the base of the range. addr must not be greater than the range limit. To set them both at once, use RangeInit(). (This is implemented as a macro, but there is a function too.)

void RangeSetLimit(Range range, Addr addr)

Set the limit of the range. addr must not be less than the range base. To set the both at once, use RangeInit(). (This is implemented as a macro, but there’s a function too.)

Size RangeSize(Range range)

Return the size of the range. (This is implemented as a macro, but there is a function too. The macro evaluates its argument twice.)

Bool RangeContains(Range range, Addr addr)

Return TRUE if addr belongs to the range, or FALSE if it does not. (This is implemented as a macro, but there is a function too. The macro evaluates its arguments twice.)

Bool RangeIsEmpty(Range range)

Return TRUE if the range is empty (contains no addresses), FALSE otherwise. (This is implemented as a macro, but there is a function too. The macro evaluates its argument twice.)

Bool RangeIsAligned(Range range, Align alignment)

Return TRUE if the base and limit of the range are both aligned to the given alignment, or FALSE if either is not.

Bool RangesOverlap(Range range1, Range range2)

Return TRUE if the two ranges overlap (have at least one address in common), or FALSE if they do not. Note that ranges [A, B) and [B, C) do not overlap.

Bool RangesNest(Range outer, Range inner)

Return TRUE if all addresses in inner are also in outer, or FALSE otherwise.