--file GPsortDefs.mesa
--last modified by Sandman on  July 8, 1980  9:03 AM 

DIRECTORY
  AltoDefs USING [Address],
  StreamDefs USING [DiskHandle];

-- Copyright  Xerox Corporation 1979, 1980

GPsortDefs: DEFINITIONS =
  BEGIN
  
  CompareProcType: TYPE = PROCEDURE [p1: POINTER, p2: POINTER] RETURNS [INTEGER];
  Fd: TYPE = RECORD [
    dh: StreamDefs.DiskHandle,
    endOfRun: BOOLEAN,
    dummyRuns: CARDINAL,
    totalRuns: CARDINAL,
    len: ItemLength,
    buffer: ItemHandle,
    head: CARDINAL,
    tail: INTEGER, -- CARDINAL ?
    record: ItemHandle];
  FdHandle: TYPE = POINTER TO Fd;
  GetProcType: TYPE = PROCEDURE [p: POINTER] RETURNS [CARDINAL];
  Item: TYPE = ARRAY AltoDefs.Address OF UNSPECIFIED; -- the item being sorted 
  
  ItemHandle: TYPE = POINTER TO Item;
  ItemHeader: TYPE = RECORD [len: ItemLength, rec: Item];
  ItemHeaderHandle: TYPE = POINTER TO ItemHeader;
  ItemLength: TYPE = INTEGER;
  PutProcType: TYPE = PROCEDURE [p: POINTER, len: CARDINAL];
  
  EOR: INTEGER = -1; -- end of run
  
  EQ: INTEGER = 0;
  GT: INTEGER = 1;
  LT: INTEGER = -1;
  
  Sort: PROCEDURE [
    get: GetProcType, put: PutProcType, compare: CompareProcType,
    expectedItemSize: CARDINAL, maxItemSize: CARDINAL, reservedPages: CARDINAL];
  RecordTooLong: ERROR;
  
  -- private procedures
  
  Alloc: PROCEDURE [nwords: CARDINAL] RETURNS [p: POINTER];
  DeleteFile: PROCEDURE [dh: StreamDefs.DiskHandle];
  EraseHeap: PROCEDURE;
  FillBuffer: PROCEDURE [file: FdHandle, size: INTEGER];
  FlushBuffer: PROCEDURE [file: FdHandle];
  Free: PROCEDURE [p: POINTER];
  InitHeap: PROCEDURE [npages: CARDINAL];
  
  END...