-- PackHeapImpl.Mesa  Last edited by Lewis on  1-Jan-81 12:17:57

DIRECTORY
  Heap USING [Create, Delete, FreeNode, MakeNode],
  PackHeap,
  Segments USING [Address],
  Strings USING [String];
 
PackHeapImpl: PROGRAM
    IMPORTS Heap
    EXPORTS PackHeap = 
  BEGIN

  privateHeap: UNCOUNTED ZONE ← NIL;

  GetSpace: PUBLIC PROC [nWords: CARDINAL] RETURNS [p: Segments.Address] =
    {RETURN[Heap.MakeNode[privateHeap, nWords]]};

  FreeSpace: PUBLIC PROC [p: Segments.Address] = 
    {Heap.FreeNode[privateHeap, p]};


  GetString: PUBLIC PROC [nChars: CARDINAL] RETURNS [s: Strings.String] =
    {RETURN[privateHeap.NEW[StringBody[nChars]]]};

  FreeString: PUBLIC PROC [s: Strings.String] = 
    BEGIN
    p: Strings.String ← s;
    privateHeap.FREE[@p];
    END;


  Initialize: PUBLIC PROC =
    BEGIN
    IF privateHeap # NIL THEN Finalize[];
    privateHeap ← Heap.Create[initial: 10, increment: 2];
    END;

  Finalize: PUBLIC PROC =   
    {Heap.Delete[privateHeap];  privateHeap ← NIL};

  END.