-- File: BackgroundAlto.mesa,  Last Edit: HGM  March 31, 1981  8:30 PM

DIRECTORY
  FSPDefs USING [
    FreeNodePointer, NodePointer, NodeOverhead, ZoneHeader, ZonePointer],
  Process USING [Detach, MsecToTicks, SetTimeout],
  SystemDefs USING [HeapZone];

BackgroundAlto: MONITOR LOCKS heap.lock USING heap: FSPDefs.ZonePointer
IMPORTS Process, SystemDefs =
  BEGIN

  SmashRover: ENTRY PROCEDURE [heap: FSPDefs.ZonePointer] =
    BEGIN OPEN FSPDefs;
    sleep: CONDITION;
    rover: FreeNodePointer ← NIL;
    Process.SetTimeout[@sleep, Process.MsecToTicks[250]];
    DO  -- forever
      WAIT sleep;
      FOR z: ZonePointer ← heap, z.restOfZone UNTIL rover = heap.rover OR z = NIL DO
        FOR node: NodePointer ← LOOPHOLE[z + SIZE[ZoneHeader]], node + node.length DO
          IF node.length = NodeOverhead THEN EXIT;
          WITH n: node SELECT FROM
            free =>
              BEGIN
              heap.rover ← rover ← @n;
              EXIT;
              END;
            ENDCASE;
          ENDLOOP;
        ENDLOOP;
      ENDLOOP;
    END;

  Process.Detach[FORK SmashRover[SystemDefs.HeapZone[]]];
  END.