// BFSFindHole.bcpl -- For creating sequential files
// Copyright Xerox Corporation 1979
// Last modified March 19, 1979  1:52 PM by Boggs

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

external
[
// outgoing procedure
BFSFindHole

// incoming procedures
AssignDiskPage
]

//----------------------------------------------------------------------------
let BFSFindHole(disk, nPages) = valof
//----------------------------------------------------------------------------
// Attempts to find a contiguous hole 'nPages' long in 'disk'.
// Returns the VDA of the first page if successful, -1 if not.
[
let top = disk>>DSK.diskKd>>KDH.diskBTsize*16

   [
   if nPages gr top resultis -1  //Not enough remaining pages
   let cp = top-1
   
   //Start looking at cp
   test AssignDiskPage(disk, top-nPages-1, nil)
      ifso  // Fast check
         [
         unless AssignDiskPage(disk, cp-1, nil) break // cp not free
         if top-cp eq nPages resultis cp
         cp = cp-1
         ] repeat
      ifnot cp = top-nPages
   top = cp
   ] repeat
]