-- NewReadRTNumbers.mesa
--  last edit                February 25, 1983 1:06 pm  Sturgis

DIRECTORY
   IO USING[char, CR, int, Put, rope, STREAM],
   RTMicrocode USING[RTMOVESTATUS],
   RTRefCounts USING[GCState],
	UserExec USING[CommandProc, GetStreams, RegisterCommand];
  

NewReadRTNumbers: PROGRAM IMPORTS IO, RTMicrocode, RTRefCounts, UserExec =

BEGIN

HTArray: TYPE = ARRAY[0..77777b] OF INTEGER;

Read: PROCEDURE RETURNS[nHTEmpty: INTEGER, nHTEntries: INTEGER, nHTChains: INTEGER, nFreeOE: INTEGER] =
	BEGIN
	theHTArray: LONG POINTER TO HTArray = LOOPHOLE[LOOPHOLE[RTRefCounts.GCState, LONG CARDINAL] + 400B];
	nHTEntries ← 0;
	nHTEmpty ← 0;
	nHTChains ← 0;
	[] ← RTMicrocode.RTMOVESTATUS[toMemory, 0];
	FOR I: CARDINAL IN [0..77777b] DO
		entry: INTEGER ← theHTArray[I];
		IF entry > 0 THEN nHTEntries ← nHTEntries + 1;
		IF entry = -1 THEN nHTEmpty ← nHTEmpty + 1;
		IF entry < -1 THEN nHTChains ← nHTChains + 1;
		ENDLOOP;
	RETURN[nHTEmpty, nHTEntries, nHTChains, RTRefCounts.GCState.GCStateBasic.nOiFree]
	END;

oldNHTEntries: INTEGER ← 0;
oldNFreeOE: INTEGER ← 0;
oldNHTChains: INTEGER ← 0;

ShowRefCounts: PROCEDURE[out: IO.STREAM] =
	BEGIN
	nHTEmpty: INTEGER;
	nHTEntries: INTEGER;
	nHTChains: INTEGER;
	nFreeOE: INTEGER;
	deltaEntries: INTEGER;
	[nHTEmpty, nHTEntries, nHTChains, nFreeOE] ← Read[];
	deltaEntries ← nHTEntries-oldNHTEntries+ oldNFreeOE-nFreeOE+nHTChains-oldNHTChains;
	oldNHTEntries ← nHTEntries;
	oldNFreeOE ← nFreeOE;
	oldNHTChains ← nHTChains;
	
	IO.Put[out, IO.rope["nHTEmpty: "], IO.int[nHTEmpty], IO.rope[", "]];
	IO.Put[out, IO.rope["nHTEntries: "], IO.int[nHTEntries], IO.rope[", "]];
	IO.Put[out, IO.rope["nHTChains: "], IO.int[nHTChains], IO.rope[", "]];
	IO.Put[out, IO.rope["nFreeOE: "], IO.int[nFreeOE], IO.rope[", "]];
	IO.Put[out, IO.rope["deltaEntries: "], IO.int[deltaEntries], IO.char[IO.CR]];
	END;

CreateDeadChain: PROCEDURE =
	BEGIN
	chain1: Cell ← NIL;
	chain2: Cell ← NIL;
	FOR I: INTEGER IN [1..1000] DO
			new: Cell ← NEW[CellBody ← [chain1, chain2]];
			chain1 ← chain2 ← new;
			ENDLOOP;
	END;

Cell: TYPE = REF CellBody;
CellBody: TYPE = RECORD[c1: Cell, c2: Cell];	

FillRefCounts: UserExec.CommandProc = TRUSTED
	BEGIN
	chain1: Cell ← NIL;
	chain2: Cell ← NIL;
	ShowRefCounts[UserExec.GetStreams[exec].out];
	FOR I: INTEGER IN [1..10] DO
		FOR J: INTEGER IN [1..1000] DO
			new: Cell ← NEW[CellBody ← [chain1, chain2]];
			chain1 ← chain2 ← new;
			ENDLOOP;
		CreateDeadChain[];
		ShowRefCounts[UserExec.GetStreams[exec].out];
		ENDLOOP;
	END;
	
CountRefCounts: UserExec.CommandProc = TRUSTED
	BEGIN
	ShowRefCounts[UserExec.GetStreams[exec].out];
	END;

-- main code

[nHTEntries: oldNHTEntries, nFreeOE: oldNFreeOE] ← Read[];
UserExec.RegisterCommand["FillRefCounts", FillRefCounts, ""];
UserExec.RegisterCommand["CountRefCounts", CountRefCounts, ""];
END..


--  February 17, 1983 8:46 am: Sturgis, started ReadRTNumbers.mesa

-- February 18, 1983 11:04 am: add exec commands
-- February 25, 1983 1:06 pmT: convert to 4.0, call it NewReadRTNumbers