// M E T E R   

get "Spruce.d"
get "SpruceFiles.d"

// outgoing procedures
external
	[
	MeterInit
	MeterClose
	MeterBlock
	]

// incoming procedures
external
	[
//ISF
	IndexedPageIO
//SPRUCE
	FSGetX
	]

// incoming statics
external
	[
	MeterFile
	DoFileMeter
	DoMeter
	]

// internal statics
static
	[
	meterBuffer
	meterPage
	meterCount
	]

// File-wide structure and manifest declarations.


// Procedures

let MeterInit() be
[
	meterBuffer=FSGetX(256)
	meterCount=0
	meterPage=0
]

and MeterClose() be if DoMeter then
[
	let p=meterPage
	while p eq meterPage do MeterBlock(METEREnd)
]

and MeterBlock(op, buf, siz; numargs n) be
[
	unless DoMeter then return
	if op eq METERRead % op eq METERWrite then
		[
		if buf>>FLStat.File eq FILEMeter then return
		unless DoFileMeter then return
		]
	if n eq 1 then siz=0
	let a=vec size M/16
	a>>M.type=op
	a>>M.length=siz
	a>>M.time=@#430
	for i=0 to size M/16-1 do MeterEnter(a!i)
	for i=0 to siz-1 do MeterEnter(buf!i)
]

and MeterEnter(val) be
[
	meterBuffer!meterCount=val
	meterCount=meterCount+1
	if meterCount eq 256 then
		[
		IndexedPageIO(MeterFile>>SPruceFile.map,
			meterPage, meterBuffer, 1, isfWrite)
		meterPage=meterPage+1
		meterCount=0
		]
]
// DCS, July 27, 1977  11:36 PM, very minor mods (ISF instead of FileWritePage)