-- Copyright (C) 1982  by Xerox Corporation. All rights reserved. 
-- ServerHeaps.mesa, HGM,  9-Mar-82 19:57:24 

DIRECTORY
  Heap USING [Create, Delete],

  ServerHeap USING [];

ServerHeaps: MONITOR IMPORTS Heap EXPORTS ServerHeap =
  BEGIN

  DummyType: TYPE = RECORD [SEQUENCE COMPUTED CARDINAL OF WORD];

  ZoneAlreadyCreated: PUBLIC ERROR = CODE;
  ZoneNotCreated: PUBLIC ERROR = CODE;
  ZoneNotEmpty: PUBLIC ERROR = CODE;
  ZoneScrambled: PUBLIC ERROR = CODE;

  nodeCount: CARDINAL ← 0;
  z: UNCOUNTED ZONE ← NIL;

  Create: PUBLIC ENTRY PROCEDURE =
    BEGIN
    IF z # NIL THEN ERROR ZoneAlreadyCreated;
    z ← Heap.Create[initial: 1, threshold: 8];
    END;

  Destroy: PUBLIC ENTRY PROCEDURE =
    BEGIN
    IF z = NIL THEN ERROR ZoneNotCreated;
    IF nodeCount > 0 THEN ERROR ZoneNotEmpty;
    Heap.Delete[z];
    z ← NIL;
    END;

  Node: PUBLIC ENTRY PROCEDURE [n: CARDINAL] RETURNS [p: LONG POINTER] =
    BEGIN
    IF z = NIL THEN ERROR ZoneNotCreated;
    nodeCount ← nodeCount + 1;
    p ← z.NEW[DummyType [n]];
    END;

  CopyString: PUBLIC PROCEDURE [s: LONG STRING] RETURNS [copy: LONG STRING] =
    BEGIN
    IF z = NIL THEN ERROR ZoneNotCreated;
    nodeCount ← nodeCount + 1;
    copy ← z.NEW[StringBody [s.length]];
    FOR i: CARDINAL IN [0..s.length) DO copy[i] ← s[i] ENDLOOP;
    copy.length ← s.length;
    END;

  Free, FreeString: PUBLIC ENTRY PROCEDURE [p: LONG POINTER] =
    BEGIN
    IF z = NIL THEN ERROR ZoneNotCreated;
    IF nodeCount = 0 THEN ERROR ZoneScrambled;
    nodeCount ← nodeCount - 1;
    z.FREE[@p];
    END;


  END.