VerifyImpl.mesa
Copyright © 1984, 1985 by Xerox Corporation. All rights reserved.
Levin on December 19, 1983 4:20 pm
Russ Atkinson on February 13, 1985 12:04:20 pm PST
DIRECTORY
BasicTime USING [FromPupTime, nullGMT],
BcdDefs USING [Base, BcdBase, CTHandle, CTIndex, CTNull, FTHandle, FTIndex, FTNull, FTSelf, MTHandle, MTIndex, NameRecord, NameString, NullName, NullVersion, VersionID, VersionStamp],
BcdOps USING [ProcessConfigs, ProcessFiles, ProcessModules],
DFInternal USING [AbortDF, CheckAbort, Client, ClientDescriptor, DefaultInteractionProc, GetFileInfo, LocalFile, LocalFileInfo, RemoteFileInfo, ReportFSError, RetryFSOperation, SimpleInteraction],
DFOperations USING [AbortInteraction, DFInfoInteraction, InfoInteraction, InteractionProc],
DFUtilities USING [Date, DateToRope, DirectoryItem, FileItem, Filter, ImportsItem, IncludeItem, ParseFromStream, ProcessItemProc, RemoveVersionNumber, SyntaxError],
FS USING [Close, Error, GetInfo, Open, OpenFile, PagesForBytes, Read, StreamOpen],
List USING [CompareProc, UniqueSort],
IO USING [Close, GetIndex, PutFR, STREAM],
Rope USING [Compare, Concat, Equal, Fetch, Find, FromProc, Index, Length, ROPE, Substr],
RopeHash USING [FromRope],
VM USING [AddressForPageNumber, Allocate, Free, Interval, nullInterval, PageCount];
VerifyImpl:
CEDAR
PROGRAM
IMPORTS BasicTime, BcdOps, DFInternal, DFUtilities, FS, List, IO, Rope, RopeHash, VM
EXPORTS DFOperations = BEGIN
ROPE: TYPE = Rope.ROPE;
STREAM: TYPE = IO.STREAM;
FileDesc:
TYPE =
RECORD [
shortName: ROPE ← NIL,
path: ROPE ← NIL,
importedFrom:
REF FileDesc ←
NIL,
highest level Imports with Using containing 'shortName'
parent:
ROPE ←
NIL,
DF file containing defining occurrence of 'shortName'
date: DFUtilities.Date ← [],
version: BcdDefs.VersionStamp ← BcdDefs.NullVersion,
bcd: BOOL ← FALSE,
needed: Needed ← $no
];
Needed: TYPE = {no, yes, wrongVersion};
htSize: NAT = 97;
HashIndex: TYPE = [0..htSize);
HashTable: TYPE = REF HashTableArray;
HashTableArray: TYPE = ARRAY HashIndex OF LIST OF REF FileDesc;
cacheSize: NAT = 64;
Omission:
TYPE =
RECORD [
missing: ROPE,
neededBy: ROPE
];
Verify:
PUBLIC
PROC [dfFile:
ROPE, interact: DFOperations.InteractionProc ←
NIL, clientData:
REF
ANY ←
NIL, log:
STREAM ←
NIL]
RETURNS [errors, warnings, filesActedUpon:
INT ← 0] = {
client: DFInternal.Client =
NEW[DFInternal.ClientDescriptor ← [
(interact ← IF interact = NIL THEN DFInternal.DefaultInteractionProc ELSE interact),
clientData,
log
]];
rootList: LIST OF REF FileDesc ← NIL;
hashTable: HashTable = NEW[HashTableArray ← ALL[NIL]];
bcdCache: BcdCache = CreateBcdCache[cacheSize];
omissions: LIST OF --REF Omission--REF ANY ← NIL;
Hash:
PROC [name:
ROPE]
RETURNS [HashIndex] = {
RETURN[RopeHash.FromRope[rope: name, case: FALSE] MOD htSize];
};
EnterInHashTable:
PROC [desc:
REF FileDesc]
RETURNS [
BOOL ←
TRUE] = {
hi: HashIndex = Hash[desc.shortName];
FOR l:
LIST
OF
REF FileDesc ← hashTable[hi], l.rest
UNTIL l =
NIL
DO
IF desc.shortName.Equal[l.first.shortName,
FALSE]
THEN {
IF desc.date ~= l.first.date
THEN {
duplicate short file name with different file creation dates
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR["'%g' appears more than once (via '%g' and '%g').",
[rope[desc.shortName]],
[rope[
IF l.first.importedFrom = NIL THEN l.first.parent
ELSE FullName[l.first.importedFrom]
]],
[rope[
IF desc.importedFrom = NIL THEN desc.parent
ELSE FullName[desc.importedFrom]
]]
]
]]
];
};
RETURN[FALSE]
};
ENDLOOP;
hashTable[hi] ← CONS[desc, hashTable[hi]];
filesActedUpon ← filesActedUpon.SUCC;
};
LookupInHashTable:
PROC [shortName:
ROPE]
RETURNS [desc:
REF FileDesc ←
NIL] = {
FOR l:
LIST
OF
REF FileDesc ← hashTable[Hash[shortName]], l.rest
UNTIL l =
NIL
DO
IF shortName.Equal[l.first.shortName, FALSE] THEN RETURN[l.first];
ENDLOOP;
};
EnumerateHashTable:
PROC [proc:
PROC [
REF FileDesc]] = {
FOR hi: HashIndex
IN HashIndex
DO
FOR l:
LIST
OF
REF FileDesc ← hashTable[hi], l.rest
UNTIL l =
NIL
DO
proc[l.first];
ENDLOOP;
ENDLOOP;
};
WorthRemembering:
PROC [desc:
REF FileDesc]
RETURNS [
BOOL ←
FALSE] = {
ext: ROPE = desc.shortName.Substr[start: desc.shortName.Index[s2: "."]];
exts: ARRAY [0..3) OF ROPE = [".bcd", ".mesa", ".config"];
FOR i:
NAT
IN [0..exts.
LENGTH)
DO
IF ext.Equal[exts[i], FALSE] THEN {desc.bcd ← i = 0; RETURN[TRUE]};
ENDLOOP;
};
Extant:
PROC [desc:
REF FileDesc]
RETURNS [
BOOL] =
INLINE {
RETURN[desc.date.gmt ~= BasicTime.nullGMT]
};
importedFrom: REF FileDesc ← NIL;
VerifyInner:
PROC [dfFile:
ROPE, date: DFUtilities.Date, filter: DFUtilities.Filter]
RETURNS [finished:
BOOL ←
FALSE] = {
directoryPath: ROPE ← NIL;
DoOneItem: DFUtilities.ProcessItemProc = {
DFInternal.CheckAbort[client];
WITH item
SELECT
FROM
directory:
REF DFUtilities.DirectoryItem =>
directoryPath ← directory.path1;
file:
REF DFUtilities.FileItem => {
desc:
REF FileDesc =
NEW[FileDesc ← [
shortName: DFUtilities.RemoveVersionNumber[file.name],
path: directoryPath,
parent: dfFile,
importedFrom: importedFrom
]];
remoteInfo:
REF DFInternal.RemoteFileInfo =
NEW[DFInternal.RemoteFileInfo ← [
name: FullName[desc],
date: file.date
]];
DFInternal.GetFileInfo[info: remoteInfo, client: client, errorLevel: $warning
! FS.Error => CONTINUE
];
desc.date ← remoteInfo.date;
IF (filter.list ~=
NIL
OR WorthRemembering[desc])
AND EnterInHashTable[desc]
AND
file.verifyRoot AND importedFrom = NIL THEN
rootList ← CONS[desc, rootList];
};
imports:
REF DFUtilities.ImportsItem =>
IF imports.form ~= $list
AND filter.list =
NIL
THEN {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR["'%g' appears in an Imports statement (in '%g') without a Using list; it will be ignored.",
[rope[imports.path1]],
[rope[dfFile]]
]
]]
];
}
ELSE {
outerMostImports: BOOL = (filter.list = NIL);
newFilter: DFUtilities.Filter = [
comments: filter.comments, -- comments processing is unaffected by imports
filterA: filter.filterA, -- source/derived distinction is unaffected by imports
filterB: IF imports.form = $exports THEN $public ELSE filter.filterB,
filterC: $all, -- if the top level passes imports, they can come from anywhere
list: IF imports.form = $list THEN imports.list ELSE filter.list
];
IF outerMostImports
THEN
importedFrom ←
NEW[FileDesc ← [
path: imports.path1, -- hack: shortName is NIL, but only CheckIfNeeded cares.
date: imports.date,
parent: dfFile
]];
IF VerifyInner[imports.path1, imports.date, newFilter]
AND outerMostImports
THEN {
FOR i:
NAT
IN [0..imports.list.nEntries)
DO
desc: REF FileDesc = LookupInHashTable[imports.list.u[i].name];
SELECT
TRUE
FROM
desc =
NIL => {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR[
"'%g' could not be found inside '%g' (or any nested DF file).",
[rope[imports.list.u[i].name]],
[rope[imports.path1]]
]
]]
];
};
imports.list.u[i].verifyRoot => rootList ← CONS[desc, rootList];
ENDCASE;
ENDLOOP;
importedFrom ← NIL;
};
};
include: REF DFUtilities.IncludeItem => [] ← VerifyInner[include.path1, include.date, filter];
ENDCASE;
};
dfInfo:
REF DFInternal.RemoteFileInfo
= NEW[DFInternal.RemoteFileInfo ← [name: dfFile, date: date]];
dfStream: STREAM;
DFInternal.GetFileInfo[info: dfInfo, client: client
! FS.Error => {errors ← errors.SUCC; GO TO skip}];
dfStream ←
FS.StreamOpen[fileName: dfInfo.name
!
FS.Error => {
errors ← errors.SUCC; DFInternal.ReportFSError[error, dfInfo, client]; GO TO skip}
];
dfInfo.name ← DFUtilities.RemoveVersionNumber[dfInfo.name];
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.DFInfoInteraction ← [action: $start, dfFile: dfInfo.name]]
];
DFUtilities.ParseFromStream[dfStream, DoOneItem, filter !
DFUtilities.SyntaxError
-- [reason: ROPE]-- => {
errors ← errors.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $error,
message:
IO.PutFR[
"Syntax error in '%g'[%d]: %g\NProcessing of this DF file aborted.",
[rope[dfInfo.name]], [cardinal[dfStream.GetIndex[]]], [rope[reason]]
]
]]
];
CONTINUE
};
DFInternal.AbortDF => dfStream.Close[];
];
dfStream.Close[];
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.DFInfoInteraction ← [action: $end, dfFile: dfInfo.name]]
];
RETURN[TRUE];
};
VerifyDependencies:
PROC = {
VerifyDependenciesInner:
PROC [parent:
REF FileDesc] = {
This procedure checks for consistency and completeness of the DF input with respect to the file identified by its parameter. There are essentially two cases.
(1) If the parameter file is not a BCD or if it is a BCD that was imported, the file is a leaf of the dependency tree. It sufficies, therefore, to check that the file exists, which was already done in the course of building the hash table. Therefore, VerifyDependenciesInner does nothing for non-BCDs.
(2) If the parameter is a BCD, the file table is enumerated and, for each file, the DF input is checked to see that (a) there exists a file with the same short name, (b) that the file is extant, and (c) that the BCD version stamp matches the stamp in the file table entry. If (a) fails, a necessary file has been omitted from the input and its (short) name is placed on a list for future printing. If (b) fails, the file appeared in the DF input but doesn't exist on the server. This error was reported during hash table construction, so no further action is required here. If (c) fails, the file specified in the DF input exists, but is the wrong version. An error is reported (unless it was already reported.) After the file table enumeration is complete, the source file for 'parent' is checked to be sure it exists.
parentBcd: BcdDefs.BcdBase ← NIL;
parentFileTable: BcdDefs.Base;
parentSource: REF FileDesc ← NIL;
parentFileName: ROPE ← NIL;
RopeForNameRecord:
PROC [bcd: BcdDefs.BcdBase, name: BcdDefs.NameRecord]
RETURNS [r:
ROPE] =
TRUSTED {
ssb: BcdDefs.NameString = LOOPHOLE[bcd + bcd.ssOffset];
len: NAT;
i: INT ← name;
GetFromNameString:
SAFE
PROC
RETURNS [char:
CHAR] =
TRUSTED {
char ← ssb.string[i]; i ← i + 1};
r ← Rope.FromProc[ssb.size[name], GetFromNameString];
len ← r.Length[];
IF len > 0 AND r.Fetch[len-1] = '. THEN r ← r.Substr[len: len-1];
};
CheckDependentFile:
PROC [fth: BcdDefs.FTHandle, recur:
BOOL ←
TRUE] =
TRUSTED {
file: ROPE ← RopeForNameRecord[parentBcd, fth.name];
child: REF FileDesc;
DFInternal.CheckAbort[client];
IF file.Find["."] < 0 THEN file ← file.Concat[".bcd"];
IF (child ← LookupInHashTable[file]) =
NIL
THEN
omissions ←
CONS[
NEW[Omission ← [missing: file, neededBy: parentFileName]],
omissions]
ELSE
IF Extant[child]
THEN {
IF child.needed = $wrongVersion THEN RETURN;
IF child.version = BcdDefs.NullVersion
THEN {
childBcd: BcdDefs.BcdBase;
childBcd ← GetBcd[bcdCache, child
!
FS.Error =>
IF DFInternal.RetryFSOperation[error, client] THEN RETRY
ELSE {
errors ← errors.SUCC;
DFInternal.ReportFSError[
error,
NEW[DFInternal.RemoteFileInfo ← [name: parentFileName, date: parent.date]],
client
];
GO TO proceedWithoutBCD
}
];
IF childBcd.versionIdent = BcdDefs.VersionID
THEN
child.version ← childBcd.version;
ReleaseBcd[bcdCache, childBcd];
};
SELECT child.needed
FROM
$no => {
IF fth.version = child.version
THEN {
child.needed ← $yes;
IF child.importedFrom ~=
NIL
AND child.importedFrom.needed = $no
THEN
child.importedFrom.needed ← $yes;
IF recur THEN VerifyDependenciesInner[child];
RETURN
};
};
$yes => IF fth.version = child.version THEN RETURN;
ENDCASE;
child.needed ← $wrongVersion;
errors ← errors.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $error,
message:
IO.PutFR[
"'%g' {%g} depends on '%g'; '%g' {%g} is the wrong version.",
[rope[parentFileName]],
[rope[DFUtilities.DateToRope[parent.date]]],
[rope[file]],
[rope[FullName[child]]],
[rope[DFUtilities.DateToRope[child.date]]]
]
]]
];
EXITS
proceedWithoutBCD => NULL;
};
};
parentSourceName: BcdDefs.NameRecord ← BcdDefs.NullName;
parentSourceVersion: BcdDefs.VersionStamp;
IF ~parent.bcd OR parent.importedFrom ~= NIL THEN RETURN;
parentFileName ← FullName[parent];
parentBcd ← GetBcd[bcdCache, parent
!
FS.Error =>
IF DFInternal.RetryFSOperation[error, client] THEN RETRY
ELSE {
errors ← errors.SUCC;
DFInternal.ReportFSError[
error,
NEW[DFInternal.RemoteFileInfo ← [name: parentFileName, date: parent.date]],
client
];
GO TO skipThisBCD
}
];
TRUSTED{
parentSourceName ← parentBcd.source;
parentSourceVersion ← parentBcd.sourceVersion;
parentFileTable ← LOOPHOLE[parentBcd + parentBcd.ftOffset];
};
IF parentSourceName = BcdDefs.NullName
THEN {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR[
"'%g' {%g} does not specify a source file.",
[rope[parentFileName]],
[rope[DFUtilities.DateToRope[parent.date]]]
]
]]
];
}
ELSE {
sourceFileName: ROPE = RopeForNameRecord[parentBcd, parentSourceName];
IF (parentSource ← LookupInHashTable[sourceFileName]) =
NIL
THEN
omissions ←
CONS[
NEW[Omission ← [
missing: sourceFileName,
neededBy: parentFileName
]],
omissions]
ELSE
IF Extant[parentSource]
AND parentSource.needed ~= $wrongVersion
THEN {
sourceDate: DFUtilities.Date =
[$explicit, BasicTime.FromPupTime[LOOPHOLE[parentSourceVersion.time]]];
IF sourceDate = parentSource.date THEN parentSource.needed ← $yes
ELSE {
parentSource.needed ← $wrongVersion;
errors ← errors.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $error,
message:
IO.PutFR[
"'%g' {%g} expects source of {%g}, but DF file specifies '%g' {%g}.",
[rope[parentFileName]],
[rope[DFUtilities.DateToRope[parent.date]]],
[rope[DFUtilities.DateToRope[sourceDate]]],
[rope[FullName[parentSource]]],
[rope[DFUtilities.DateToRope[parentSource.date]]]
]
]]
];
};
};
};
TRUSTED {
IF parentBcd.nConfigs > 0
THEN {
remoteSubConfig: BOOL ← FALSE;
ctb: BcdDefs.Base = LOOPHOLE[parentBcd + parentBcd.ctOffset];
sgb: BcdDefs.Base = LOOPHOLE[parentBcd + parentBcd.sgOffset];
DoOneModule:
PROC [mth: BcdDefs.MTHandle, mti: BcdDefs.MTIndex]
RETURNS [
BOOL ←
FALSE] =
TRUSTED {
cti: BcdDefs.CTIndex ← mth.config;
UNTIL cti = BcdDefs.CTNull
DO
cth: BcdDefs.CTHandle = @ctb[cti];
IF cth.file ~= BcdDefs.FTSelf THEN {remoteSubConfig ← TRUE; RETURN};
cti ← cth.config;
ENDLOOP;
This module belongs the configuration directly defined by parentBcd's source.
IF mth.file ~= BcdDefs.FTSelf
AND mth.file ~= BcdDefs.FTNull
THEN
CheckDependentFile[@parentFileTable[mth.file]];
};
DoOneConfig:
PROC [cth: BcdDefs.CTHandle, cti: BcdDefs.CTIndex]
RETURNS [
BOOL ←
FALSE] =
TRUSTED {
IF cth.file ~= BcdDefs.FTSelf
THEN {
This config is a remote one. If all of its enclosing configs are local, we treat it as a dependency.
outerCti: BcdDefs.CTIndex ← cth.config;
UNTIL outerCti = BcdDefs.CTNull
DO
outerCth: BcdDefs.CTHandle = @ctb[outerCti];
IF outerCth.file ~= BcdDefs.FTSelf THEN RETURN;
outerCti ← outerCth.config;
ENDLOOP;
CheckDependentFile[@parentFileTable[cth.file]];
};
};
[] ← BcdOps.ProcessModules[parentBcd, DoOneModule];
IF remoteSubConfig THEN [] ← BcdOps.ProcessConfigs[parentBcd, DoOneConfig];
}
ELSE {
DoOneFile:
PROC [fth: BcdDefs.FTHandle, fti: BcdDefs.FTIndex]
RETURNS [
BOOL ←
FALSE] =
TRUSTED {
CheckDependentFile[fth];
};
[] ← BcdOps.ProcessFiles[parentBcd, DoOneFile];
};
};
ReleaseBcd[bcdCache, parentBcd];
EXITS
skipThisBCD => NULL;
};
IF rootList =
NIL
THEN {
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message: IO.PutFR["No files in '%g' are marked with '+'.", [rope[dfFile]]]
]]
];
}
ELSE
FOR l:
LIST
OF
REF FileDesc ← rootList, l.rest
UNTIL l =
NIL
DO
rootDesc: REF FileDesc = l.first;
IF rootDesc.needed = $no
THEN {
rootDesc.needed ← $yes;
IF rootDesc.importedFrom ~=
NIL
AND rootDesc.importedFrom.needed = $no
THEN
rootDesc.importedFrom.needed ← $yes;
IF Extant[rootDesc]
THEN
VerifyDependenciesInner[rootDesc ! DFInternal.AbortDF => FlushBcdCache[bcdCache]];
};
ENDLOOP;
FlushBcdCache[bcdCache];
};
ReportOmissions:
PROC = {
Compare: List.CompareProc = {
RETURN[Rope.Compare[
NARROW[ref1, REF Omission].missing,
NARROW[ref2, REF Omission].missing]
]
};
nFiles: NAT ← 0;
msg: ROPE ← "The following should appear in the DF input:\N";
IF omissions = NIL THEN RETURN;
FOR omissions ← List.UniqueSort[omissions, Compare], omissions.rest
UNTIL omissions =
NIL
DO
omission: REF Omission = NARROW[omissions.first];
msg ← msg.Concat[
IO.PutFR[" '%g', needed by '%g'\N",
[rope[omission.missing]],
[rope[omission.neededBy]]
]
];
nFiles ← nFiles.SUCC;
ENDLOOP;
errors ← errors + nFiles;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $error,
message: msg
]]
];
};
CheckIfNeeded:
PROC [desc:
REF FileDesc] = {
IF desc.needed ~= $no OR ~WorthRemembering[desc] THEN RETURN;
IF desc.importedFrom ~=
NIL
THEN
IF desc.importedFrom.needed = $no
THEN
The entire imports item is superfluous. We print the warning here, then flag the 'importedFrom' descriptor as 'needed' to prevent repeated messages, either for the imports item or anything in its Using list.
(desc ← desc.importedFrom).needed ← $yes
ELSE RETURN;
warnings ← warnings.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $warning,
message:
IO.PutFR[
"'%g' (%g) is superfluous.",
[rope[IF desc.shortName = NIL THEN desc.path ELSE desc.shortName]],
[rope[
IF desc.importedFrom = NIL THEN IO.PutFR["in '%g'", [rope[desc.parent]]]
ELSE IO.PutFR["via '%g'", [rope[FullName[desc.importedFrom]]]]
]]
]
]]
];
};
{
ENABLE
{
ABORTED => {
DFInternal.SimpleInteraction[client, NEW[DFOperations.AbortInteraction ← [TRUE]]];
We would like to RESUME at this point, but until ABORTED is redeclared as a SIGNAL, it won't work.
};
DFInternal.AbortDF => {
errors ← errors.SUCC;
DFInternal.SimpleInteraction[client, NEW[DFOperations.DFInfoInteraction ← [action: $abort, dfFile: dfFile]]];
CONTINUE
};
};
IF DFInternal.LocalFile[dfFile]
THEN {
dfInfo: REF DFInternal.LocalFileInfo = NEW[DFInternal.LocalFileInfo ← [name: dfFile]];
DFInternal.GetFileInfo[info: dfInfo, client: client, errorLevel: $abort];
IF (dfFile ← dfInfo.attachedTo) =
NIL
THEN {
errors ← errors.SUCC;
DFInternal.SimpleInteraction[
client,
NEW[DFOperations.InfoInteraction ← [
class: $abort,
message:
IO.PutFR[
"'%g' isn't a remote file and therefore can't be verified.",
[rope[dfInfo.name]]
]
]]
];
RETURN
};
};
The peculiar date specification has the effect of treating a version number on 'dfFile' as truth. See comment in DFInternal.GetFileInfo.
IF VerifyInner[dfFile, [format: $explicit], []]
THEN {
The hash table has now been constructed. Process the roots list.
VerifyDependencies[];
Report any omitted names.
ReportOmissions[];
All inconsistencies have been reported. Pick up extra entries and report them.
EnumerateHashTable[CheckIfNeeded];
};
};
};
Internal procedures
BcdCache: TYPE = REF BcdCacheObject;
BcdCacheObject:
TYPE =
RECORD [
locked: CachedBcdList ← NIL, -- linear list
available: CachedBcdList ← NIL, -- circularly chained
size: NAT ← 0,
replacementSize: NAT
];
CachedBcdList: TYPE = LIST OF CachedBcd;
CachedBcd: TYPE = REF CachedBcdEntry;
CachedBcdEntry:
TYPE =
RECORD [
buffer: VM.Interval ← VM.nullInterval,
desc: REF FileDesc ← NIL
];
initialVM: VM.PageCount = 10;
A bcd cache is managed as two singly-linked lists of cache entries. The 'available' list consists of cached bcd's that are eligible for replacement. The 'locked' list consists of cached bcd's that are actually being touched; they cannot be replaced until they are moved to the 'available' list. The maximum size of the two lists is (almost) bounded by a creation-time parameter; however, if the 'available' list is empty when a new entry is needed, the entry will be created regardless of the size bound.
Cache replacement occurs in the 'available' list in LRU order. New cache entries appear at the tail of the list; victims are taken from the head. BcdCache.available points at the logical tail of the list, that is, the MRU entry, and BcdCache.list.rest points at the logical head, the LRU entry.
CreateBcdCache:
PROC [replacementSize:
NAT]
RETURNS [bcdCache: BcdCache] = {
RETURN[NEW[BcdCacheObject ← [replacementSize: replacementSize]]]
};
GetBcd:
PROC [bcdCache: BcdCache, desc:
REF FileDesc]
RETURNS [bcd: BcdDefs.BcdBase ←
NIL] = {
prev: CachedBcdList ← bcdCache.available;
new: CachedBcd ← NIL;
list: CachedBcdList ← NIL;
NewEntry:
PROC
RETURNS [CachedBcdList] = {
bcdCache.size ← bcdCache.size.SUCC;
RETURN[CONS[NEW[CachedBcdEntry ← []], NIL]]
};
SELECT
TRUE
FROM
prev =
NIL =>
'available' list is empty. Create a new cache entry regardless of present cache size.
list ← NewEntry[];
prev = prev.rest => {
'available' list has precisely one entry, which may or may not be the file of interest.
list ← bcdCache.available;
bcdCache.available ← NIL;
IF list.first.desc ~= desc THEN list.first.desc ← NIL;
};
ENDCASE => {
'available' list has at least two entries.
list ← prev.rest;
DO
assert: list = prev.rest
IF list.first.desc = desc THEN GO TO dequeue; -- 'list.first' is a cache hit
prev ← list;
IF (list ← list.rest) = bcdCache.available.rest
THEN {
cache miss.
IF bcdCache.size < bcdCache.replacementSize THEN {list ← NewEntry[]; EXIT}
ELSE {list.first.desc ← NIL; GO TO dequeue};
};
REPEAT
dequeue => {
prev.rest ← list.rest;
IF bcdCache.available = list THEN bcdCache.available ← list.rest;
};
ENDLOOP;
};
'list' is a single element list (although list.rest may be garbage) containing the CachedBcd to be (re)used. We link it on the 'locked' list.
list.rest ← bcdCache.locked;
bcdCache.locked ← list;
We now have a cache entry, which either is empty or has the desired file.
IF (new ← list.first).desc =
NIL
THEN {
ENABLE
UNWIND => {
bcdCache.locked ← bcdCache.locked.rest;
bcdCache.size ← bcdCache.size.PRED;
};
name: ROPE = FullName[desc];
file: FS.OpenFile;
nPages: INT;
file ← FS.Open[name: name, wantedCreatedTime: desc.date.gmt];
IF new.buffer.count = 0 THEN new.buffer ← VM.Allocate[initialVM];
nPages ← MIN[FS.PagesForBytes[FS.GetInfo[file].bytes], new.buffer.count];
TRUSTED {
lp: LONG POINTER ← VM.AddressForPageNumber[new.buffer.page];
bcd ← LOOPHOLE[lp];
FS.Read[file: file, from: 0, nPages: nPages, to: lp];
IF bcd.nPages > nPages
THEN {
buffer too small; grow it.
nPages ← bcd.nPages;
VM.Free[new.buffer];
new.buffer ← VM.Allocate[nPages];
lp ← VM.AddressForPageNumber[new.buffer.page];
bcd ← LOOPHOLE[lp];
FS.Read[file: file, from: 0, nPages: nPages, to: lp];
};
};
FS.Close[file];
new.desc ← desc;
}
ELSE TRUSTED {bcd ← LOOPHOLE[VM.AddressForPageNumber[new.buffer.page]]};
};
ReleaseBcd:
PROC [bcdCache: BcdCache, bcd: BcdDefs.BcdBase] = {
list: CachedBcdList ← bcdCache.locked;
prev: CachedBcdList ← NIL;
UNTIL list =
NIL
DO
TRUSTED {IF VM.AddressForPageNumber[list.first.buffer.page] = bcd THEN EXIT};
prev ← list;
list ← list.rest;
REPEAT
FINISHED => ERROR;
ENDLOOP;
dequeue from 'locked' list.
IF prev = NIL THEN bcdCache.locked ← list.rest ELSE prev.rest ← list.rest;
enqueue on 'available' list.
IF bcdCache.available = NIL THEN list.rest ← list
ELSE {list.rest ← bcdCache.available.rest; bcdCache.available.rest ← list};
bcdCache.available ← list;
};
FlushBcdCache:
PROC [bcdCache: BcdCache] = {
list: CachedBcdList;
flush 'locked' list.
FOR list ← bcdCache.locked, list.rest
UNTIL list =
NIL
DO
TRUSTED {VM.Free[list.first.buffer]};
ENDLOOP;
bcdCache.locked ← NIL;
IF bcdCache.available = NIL THEN RETURN;
list ← bcdCache.available.rest; -- head of 'available' list
bcdCache.available.rest ← NIL; -- break circular chain
bcdCache.available ← NIL;
UNTIL list =
NIL
DO
TRUSTED {VM.Free[list.first.buffer]};
list ← list.rest;
ENDLOOP;
};
FullName:
PROC [desc:
REF FileDesc]
RETURNS [
ROPE] =
INLINE {
RETURN[desc.path.Concat[desc.shortName]]
};
END.