35. The low-memory reservoir

35.1. Introduction

.intro: The low-memory reservoir provides client support for implementing handlers for low-memory situations which allocate. The reservoir is implemented inside the arena as a pool of unallocatable segments.

35.2. Architecture

struct ReservoirStruct *Reservoir

.adt: The reservoir interface looks (almost) like an abstract data type of type Reservoir. It’s not quite abstract because the arena embeds the structure of the reservoir (of type ReservoirStruct) into its own structure, for simplicity of initialization.

.align: The reservoir is implemented as a pool of available tracts, along with a size and limit which must always be aligned to the arena alignment. The size corresponds to the amount of memory currently maintained in the reservoir. The limit is the maximum amount that it is desired to maintain.

.wastage: When the reservoir limit is set by the client, the actual limit should be increased by one arena grain for every active mutator buffer.

.really-empty: When the reservoir limit is set to 0, assume that the client really doesn’t have a need for a reservoir at all. In this case, the client won’t even want an allowance to be made for wastage in active buffers.

35.3. Implementation

.interface: The following functions comprise the interface to the reservoir module:

Bool ReservoirCheck(Reservoir reservoir)

.interface.check: ReservoirCheck() checks the reservoir for consistency.

Res ReservoirInit(Reservoir reservoir, Arena arena)

.interface.init: ReservoirInit() initializes the reservoir and its associated pool, setting the size and limit to 0.

void ReservoirFinish(Reservoir reservoir)

.interface.finish: ReservoirFinish() de-initializes the reservoir and its associated pool:

Size ReservoirLimit(Reservoir reservoir)

.interface.limit: ReservoirLimit() returns the limit of the reservoir:

void ReservoirSetLimit(Reservoir reservoir, Size size)

.interface.set-limit: ReservoirSetLimit() sets the limit of the reservoir, making an allowance for wastage in mutator buffers:

Size ReservoirAvailable(Reservoir reservoir)

.interface.available: ReservoirAvailable() returns the available size of the reservoir:

Res ReservoirEnsureFull(Reservoir reservoir)

.interface.ensure-full: ReservoirEnsureFull() attempts to fill the reservoir with memory from the arena, until it is full:

void ReservoirDeposit(Reservoir reservoir, Addr base, Size size)

.interface.deposit: ReservoirDeposit() attempts to fill the reservoir with memory in the supplied range, until it is full. This is called by the arena from ArenaFree() if the reservoir is not known to be full. Any memory which is not added to the reservoir (because the reservoir is full) is freed via the arena class’s free method.

Res ReservoirWithdraw(Addr *baseReturn, Tract *baseTractReturn, Reservoir reservoir, Size size, Pool pool)

.interface.withdraw: ReservoirWithdraw() attempts to allocate memory of the specified size to the specified pool to the reservoir. If no suitable memory can be found it returns ResMEMORY.

.interface.withdraw.align: Currently, ReservoirWithdraw() can only withdraw a single arena grain at a time. This is because the reservoir doesn’t attempt to coalesce adjacent memory blocks. This deficiency should be fixed in the future.

.pool: The memory managed by the reservoir is owned by the reservoir pool. This memory is never sub-allocated. Each tract belonging to the pool is linked onto a list. The head of the list is in the Reservoir object. Links are stored in the TractP fields of each tract object.