-- Compiler GriffinMemoryFns/n
--m.stone October 7, 1980  3:12 PM
--Tiberi December 11, 1979  12:46 AM

DIRECTORY
	GriffinMemoryDefs: FROM "GriffinMemoryDefs",
	GriffinDefs: FROM "GriffinDefs",
	ObjectDefs: FROM "ObjectDefs",
	SegmentDefs: FROM "SegmentDefs",
	SystemDefs: FROM "SystemDefs";

GriffinMemoryFns: PROGRAM
	IMPORTS SystemDefs, GriffinDefs, SegmentDefs, ObjectDefs
	EXPORTS GriffinMemoryDefs =
BEGIN

numEmergencyPages: CARDINAL ← 10;
emergencyStorage: POINTER ← NIL;
freed: BOOLEAN ← FALSE;

FreeEmergencyStorage: PROCEDURE =
BEGIN
IF emergencyStorage#NIL THEN SystemDefs.FreePages[emergencyStorage];
emergencyStorage ← NIL;
ObjectDefs.EmergencyDeleteEncodings;
freed ← SystemDefs.PruneHeap[];
SIGNAL GriffinDefs.GriffinFull;
END;

Allocate: PUBLIC PROCEDURE[nwords: CARDINAL] RETURNS[POINTER] =
BEGIN
RETURN[SystemDefs.AllocateHeapNode[nwords
	!SegmentDefs.InsufficientVM => FreeEmergencyStorage]];
END;

Free: PUBLIC PROCEDURE[ptr: POINTER TO UNSPECIFIED]  =
BEGIN IF ptr#NIL THEN SystemDefs.FreeHeapNode[ptr] END;

AllocateString: PUBLIC PROCEDURE [nchars: CARDINAL] RETURNS [STRING] =
BEGIN
RETURN [SystemDefs.AllocateHeapString [nchars
	!SegmentDefs.InsufficientVM => FreeEmergencyStorage]];
END;

FreeString: PUBLIC PROCEDURE [string: STRING] =
BEGIN SystemDefs.FreeHeapString [string] END;

AllocateSegment: PUBLIC PROCEDURE [len: CARDINAL] RETURNS  [POINTER TO UNSPECIFIED] =
BEGIN
RETURN [SystemDefs.AllocateSegment [len
	!SegmentDefs.InsufficientVM => FreeEmergencyStorage]];

END;

FreeSegment: PUBLIC PROCEDURE [p: POINTER TO UNSPECIFIED] = 
BEGIN SystemDefs.FreeSegment [p] END;

emergencyStorage ← SystemDefs.AllocatePages[numEmergencyPages];

END.