[comp.lang.forth] ANS Forth memory allocation

wmb@pi.Eng.Sun.COM (Mitch Bradley) (06/04/90)

> Although I consider dynamic allocation to be one of the highest
> priorities in the new standard, I have yet to read ANYTHING about it.

Many other people also considered it a high priority.  I almost resigned
from the committee when it appeared that this was not going to make it
into the standard, but in the end, it made it in.  The following words
comprise 2 extension (i.e. optional) wordsets: Memory Allocation
and Far Memory.

Mitch Bradley


   -- The following words allocate "data memory", addressable using the
   normal memory access operators such as @, !, and MOVE

ALLOCATE  ( u -- a-addr ior )			MEMORY ALLOCATION
	Allocate u address units of contiguous data memory.  The dictionary
	pointer is unaffected by this operation.  The initial content of
	the allocated memory is undefined.

	If the allocation succeeds, a-addr is the aligned starting address
	of the allocated memory and ior is 0.

	If the operation fails, a-addr does not represent a valid address
	and ior is the implementation-defined non-zero error code.

FREE  ( a-addr -- ior )				MEMORY ALLOCATION
	Return the contiguous region of data memory indicated by a-addr to 
	the system for later allocation.  a-addr must indicate a region of
	data memory that was previously obtained by ALLOCATE or RESIZE .

	ior is 0 (success) or the implementation-defined non-zero error code.

AVAILABLE  ( -- u )				MEMORY ALLOCATION
	Return the number of address units contained in the largest
	contiguous region of data memory that may be allocated by ALLOCATE
	or RESIZE .

RESIZE  ( a-addr1 u -- a-addr2 ior )		MEMORY ALLOCATION
	Change the memory allocation of the contiguous data memory starting
	at the address a-addr1 allocate by ALLOCATE or RESIZE to u address
 	units.  u may be either larger or smaller than the current size of
	the memory region.

	If the operation succeeds, a-addr2 is the aligned starting address
	of u address units of allocated memory and ior is 0.  a-addr2 may
	or may not be the same as a-addr1.  If they are not the same, the
	values contained in the region at a-addr1 are copied to a-addr2,
	up to the minimum size of either of the two regions.  If they are
	the same, the values contained in the region are preserved to the
	minimum of u or the original size.  If a-addr2 is not the same as
	a-addr1, the region of memory at a-addr1 is returned to the system
	according to the operation of FREE .

	If the operation fails, a-addr2 does not represent a valid address
	and ior is the implementation-defined non-zero error code.


    -- The following words operate on "far memory", which is not necessarily
    accessible using the normal operators @, !, MOVE.  "far memory" might,
    for instance, be in a different segment, or in a relocatable heap, or it
    might even be virtual memory of some type.

FAR-ALLOC  ( ud -- memid ior )			FAR MEMORY
	Allocate ud address units of far memory.  The initial content of
	the allocated memory is undefined.

	If the allocation succeeds, memid is the cell pair specifying the
	start of the allocated memory and ior is 0.

	If the allocation fails, memid is undefined and ior is the
	implementation-defined non-zero error code.

FAR-FREE  ( memid -- ior )			FAR MEMORY
	Return the region of far memory indicated by memid to the system
	for later allocation.  memid must indicate a region of far memory
	that was previously obtained by FAR-ALLOC.

	If the operation succeeds, ior is 0.  	If the operation fails,
 	ior is the implementation-defined non-zero error code.

FAR-IN  ( memid ud addr u -- )			FAR MEMORY
	Copy from far memory into local memory.  Copy the contents of
	u consecutive address units at the far memory starting at offset
	ud from memid to the local memory starting at addr.

	An exception exists if either the source region or the destination
	region is inaccessible or u is larger than the amount of memory
	available at addr.

FAR-OUT  ( memid ud addr u -- )			FAR MEMORY
	Copy from local memory into far memory.  Copy the contents of
	u consecutive address units at the local memory starting at addr
	to the far memory starting at offset ud from memid.

	An exception exists if either the source region or the destination
	region is inaccessible or u is larger than the amount of memory
	available at memid and ud.

andrew@idacom.uucp (Andrew Scott) (06/05/90)

Mitch Bradley writes:
> FAR-IN  ( memid ud addr u -- )			FAR MEMORY
> FAR-OUT  ( memid ud addr u -- )			FAR MEMORY

Far out, dude.  :-)  Perhaps these words might read better as ">FAR" (to far)
and "FAR>" (far from).
-- 
Andrew Scott	| mail:		andrew@idacom.uucp
		| - or -	{att, watmath, ubc-cs}!alberta!idacom!andrew
		| - or -	uunet!myrias!aunro!idacom!andrew
Edmonton Oilers: 1984, 1985, 1987, 1988 and 1990 Stanley Cup Champions