//DiskFindHole.bcpl -- For creating sequential files
// Copyright Xerox Corporation 1979

//	Last modified February 23, 1979  7:55 PM

get "AltoFileSys.d"
get "Disks.d"

//outgoing procedure
external DiskFindHole

//incoming procedures
external [
	AssignDiskPage
	Usc
	]

let DiskFindHole(disk, n) = valof
 [
	let kd=disk>>DSK.diskKd
	let top=kd>>KDH.diskBTsize*16
	let cp=nil

	[
	if Usc(n, top) gr 0 then resultis -1 //Not enough remaining pages
	let cp=top-1			//Start looking at cp

	test AssignDiskPage(disk, top-n-1, nil) then	// Fast check
	   [
	   unless AssignDiskPage(disk, cp-1, nil) then break // cp not free
	   if top-cp eq n then resultis cp
	   cp=cp-1
	   ] repeat
	or cp=top-n
	top=cp
	] repeat

 ]