-- RTStorageAccountingImpl.Mesa
-- last edited May 18, 1983 9:09 am by Paul Rovner

DIRECTORY
PrincOps USING[PsbIndex],
RTRefCounts USING[nWordsReclaimed],
RTStorageAccounting USING[defaultCollectionInterval, PSBIMap, CellCount],
SafeStorage USING[],
SafeStoragePrivate USING[PermanentPageZone];

RTStorageAccountingImpl: MONITOR -- Protects the storage account books
IMPORTS RTRefCounts, SafeStoragePrivate
EXPORTS RTStorageAccounting, SafeStorage
= BEGIN OPEN RTStorageAccounting;

-- TYPES

-- VARIABLES
nWordsRequested: PUBLIC LONG CARDINAL ← 0;
nWordsAllocated: PUBLIC LONG CARDINAL ← 0; -- >= CollectionInterval implies collect
SUMnWordsAllocated: PUBLIC LONG CARDINAL ← 0;
nObjectsCreated: PUBLIC LONG CARDINAL ← 0;
MapPsbiToWordsAllocated: PUBLIC LONG POINTER TO PSBIMap;

CollectionInterval: PUBLIC LONG INTEGER ← defaultCollectionInterval;
SuspensionThreshold: PUBLIC LONG INTEGER ← ComputeSuspensionThreshold[];

--PROCS

ComputeSuspensionThreshold: PROC RETURNS[LONG INTEGER] =
{RETURN[CollectionInterval+CollectionInterval]};

NWordsAllocated: PUBLIC ENTRY SAFE PROC RETURNS[LONG CARDINAL] =
TRUSTED {ENABLE UNWIND => NULL;
RETURN[SUMnWordsAllocated + nWordsAllocated]};

NWordsReclaimed: PUBLIC SAFE PROC RETURNS[LONG CARDINAL] =
TRUSTED {RETURN[RTRefCounts.nWordsReclaimed]};

SetCollectionInterval: PUBLIC ENTRY SAFE PROC[newInterval: LONG CARDINAL]
RETURNS[oldInterval: LONG CARDINAL] =
TRUSTED {ENABLE UNWIND => NULL;
oldInterval ← CollectionInterval;
CollectionInterval ← newInterval;
SuspensionThreshold ← ComputeSuspensionThreshold[]};

ResetNWordsAllocated: PUBLIC ENTRY PROC =
{ENABLE UNWIND => NULL;
SUMnWordsAllocated ← SUMnWordsAllocated + nWordsAllocated;
nWordsAllocated ← 0;
FOR i: PrincOps.PsbIndex IN PrincOps.PsbIndex
DO MapPsbiToWordsAllocated.elements[i].total
← MapPsbiToWordsAllocated.elements[i].total
+ MapPsbiToWordsAllocated.elements[i].current;
MapPsbiToWordsAllocated.elements[i].current ← 0;
ENDLOOP};

ResetTotalNWordsAllocatedOnly: PUBLIC ENTRY PROC =
{SUMnWordsAllocated ← SUMnWordsAllocated + nWordsAllocated;
nWordsAllocated ← 0};


-- START HERE

MapPsbiToWordsAllocated
← SafeStoragePrivate.PermanentPageZone.NEW[PSBIMap[LAST[PrincOps.PsbIndex]+1]];

FOR i: PrincOps.PsbIndex IN PrincOps.PsbIndex
DO MapPsbiToWordsAllocated.elements[i] ← [total: 0, current: 0];
ENDLOOP;

END.