.. mode: -*- rst -*-

The low-memory reservoir
========================

:Tag: design.mps.reservoir
:Author: Tony Mann
:Date: 1998-07-30
:Status: incomplete design
:Revision: $Id: //info.ravenbrook.com/project/mps/version/1.113/design/reservoir.txt#1 $
:Copyright: See `Copyright and License`_.
:Index terms:   pair: reservoir; design


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.


Architecture
------------

``typedef 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 an arena alignment amount 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.


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 memory in chunks of the size of the arena alignment.
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.


Document History
----------------

- 1998-07-30 Tony Mann. Incomplete design.

- 2002-06-07 RB_ Converted from MMInfo database design document.

- 2013-05-22 GDR_ Converted to reStructuredText.

.. _RB: http://www.ravenbrook.com/consultants/rb/
.. _GDR: http://www.ravenbrook.com/consultants/gdr/


Copyright and License
---------------------

Copyright © 2013-2014 Ravenbrook Limited. All rights reserved.
<http://www.ravenbrook.com/>. This is an open source license. Contact
Ravenbrook for commercial licensing options.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

#. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

#. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

#. Redistributions in any form must be accompanied by information on how
   to obtain complete source code for this software and any
   accompanying software that uses this software.  The source code must
   either be included in the distribution or be available for no more than
   the cost of distribution plus a nominal fee, and must be freely
   redistributable under reasonable conditions.  For an executable file,
   complete source code means the source code for all modules it contains.
   It does not include source code for modules or files that typically
   accompany the major components of the operating system on which the
   executable file runs.

**This software is provided by the copyright holders and contributors
"as is" and any express or implied warranties, including, but not
limited to, the implied warranties of merchantability, fitness for a
particular purpose, or non-infringement, are disclaimed.  In no event
shall the copyright holders and contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or
services; loss of use, data, or profits; or business interruption)
however caused and on any theory of liability, whether in contract,
strict liability, or tort (including negligence or otherwise) arising in
any way out of the use of this software, even if advised of the
possibility of such damage.**
