Internal OS memory
==================
This is a critical system resource.  It is allocated in "paragraphs"
(multiples of 16 bytes) from a pool defined in osmem.c, using the
function xmgetblk(); for simplicity, xmgetblk() is normally invoked
via the wrapper MGET().  Each allocated memory block is preceded by
a WORD containing the size of the block in paragraphs.  The maximum
size of block that can be allocated is MAXQUICK-1 paragraphs.

When freed, memory is not returned to the pool directly, but is added
to one of a number of chains of free memory blocks.  There are MAXQUICK
chains, one for each possible memory block size plus an unused chain
corresponding to blocks of zero paragraphs.  Free blocks are chained
together via standard address pointers occupying the first 4 bytes of
each free block.  When a memory block is requested, it is obtained from
the appropriate free chain if possible; otherwise from the pool.

As of July 2016, all memory blocks are the same size: 4 paragraphs,
i.e. 64 bytes.  This ensures that the system cannot run out of os
memory while free blocks are available in a separate pool.  In order
to avoid wasting space, MDs are allocated in a shared block known as
an MDBLOCK, each of which holds up to 3 MDs.

This method of allocation is almost the same as the one introduced by
TOS 1.04, and the TOS program FOLDRnnn.PRG can be used to extend the
amount of memory available.  It does this by adding blocks to the free
chain for 4-paragraph blocks.  Since allocation is always from the free
chain first, this provides additional space.

There are slight differences between memory block handling in EmuTOS and
TOS 1.04:

1. TOS 1.04 groups 4 MDs together rather than 3.  In order to do this,
the size of the memory block is increased to 68 bytes, and this means
that TOS has to reformat the pool of memory blocks added by FOLDRnnn.PRG.
With the normal ratio of MD allocations to non-MD allocations, there is
almost no benefit in memory usage from doing this, and there is added
complexity due to the reformatting required.

2. When TOS requires memory for a DND or OFD, and os memory is exhausted,
it halts with a message.  EmuTOS (as of March 2014) reclaims allocated
(but currently unused) DNDs at this point, and only halts with a message
if this fails.

Roger Burrows
8 July 2016
