home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume44 / toy_os / part04 / io_requests.cc < prev    next >
C/C++ Source or Header  |  1994-09-05  |  3KB  |  125 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               UNT Virtual Machine
  6.  *
  7.  *              Managing input/output requests
  8.  *    by which the CPU and I/O managers communicate with each other
  9.  *
  10.  ************************************************************************
  11.  */
  12.  
  13. #include "io_manager.h"
  14. #include "myenv.h"
  15.  
  16. /*
  17.  *------------------------------------------------------------------------
  18.  *            Elementary operations on the contents of a
  19.  *                single request
  20.  */
  21.  
  22. IORequest::IORequest(void)
  23. {
  24.   pid = NIL_pid;
  25.   device_no = 0;
  26.   buffer_ptr = return_code_vptr = return_code_pptr = 0;
  27.   rec_count = disk_address = 0;
  28.   return_code = 0;
  29.  
  30.   register int i;
  31.   for(i=0; i<max_no_recs; i++)
  32.     io_pages[i] = 0;
  33. }
  34.  
  35. void IORequest::dump(void) const
  36. {
  37.   message("\nI/O request block #%d for %s operation of process #%d"
  38.       "\n\ton device #%d, to record buffer %o, record count %d, "
  39.       "from sector %d."
  40.       "\n\tReturn code is to be written at %o. Return code so far is %d\n",
  41.       id, (operation == IO_READ ? "read" : "write"), pid,
  42.       device_no,buffer_ptr,rec_count,disk_address,return_code_vptr,
  43.       return_code);
  44. }
  45.  
  46.                 // Dispose of the request block
  47. void IORequest::dispose(void)
  48. {
  49.   pid = NIL_pid;
  50.   device_no = 0;
  51.   buffer_ptr = return_code_vptr = return_code_pptr = 0;
  52.   rec_count = disk_address = 0;
  53.   return_code = 0;
  54.  
  55.   assert( io_pages[0] == 0 );        // Just to make sure the pages were
  56.                     // unlocked
  57. }
  58.  
  59. /*
  60.  *------------------------------------------------------------------------
  61.  *            Constructing the request table
  62.  */
  63.  
  64. IORequestTable::IORequestTable(const int no_slots)
  65.     : nslots(no_slots)
  66. {
  67.   assure(nslots > 4,"Number of I/O request slots must be at least 5");
  68.   reqs = new IORequest[nslots];
  69.  
  70.   register RID i;
  71.   for(i=1; i<=nslots; i++)
  72.   {
  73.     IORequest& req = (*this)[i];
  74.     req.id = i;
  75.     req.key = 0;
  76.     freeIORs.append(req);
  77.   }
  78. }
  79.  
  80. /*
  81.  *------------------------------------------------------------------------
  82.  *             Elementary request table operations
  83.  */
  84.  
  85.                 // Get a request given its no
  86. IORequest& IORequestTable::operator [] (const RID rid) const
  87. {
  88.   if( rid == NIL_rid )
  89.     _error("FATAL: Requested IORequest id is NIL");
  90.   if( rid > nslots )
  91.     _error("FATAL: IORequest id %d is too big",rid);
  92.   return reqs[rid-1];
  93. }
  94.  
  95.                 // Print info about requests
  96. void IORequestTable::dump(void) const
  97. {
  98.   register int i;
  99.   for(i=1; i<=nslots; i++)
  100.   {
  101.     IORequest& req = (*this)[i];
  102.     if( req.pid != NIL_pid )
  103.       req.dump();
  104.   }
  105. }
  106.  
  107.                 // Allocate a new request, return NIL
  108.                 // if the system run out of templates
  109. RID IORequestTable::new_request(void)
  110. {
  111.   if( freeIORs.is_empty() )
  112.     return NIL_rid;
  113.   return freeIORs.get_from_head()->id;
  114. }
  115.  
  116.  
  117.                 // Dispose of the given IORequest
  118. void IORequestTable::dispose(const RID rid)
  119. {
  120.   IORequest& req = (*this)[rid];
  121.   assert( rid == req.id );
  122.   req.dispose();
  123.   freeIORs.append(req);
  124. }
  125.