home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / mach / amiga / scsi9091.lzh / cache.c next >
Encoding:
C/C++ Source or Header  |  1991-02-24  |  2.1 KB  |  90 lines

  1. #include <exec/types.h>
  2. #include <exec/execbase.h>
  3. #include <exec/nodes.h>
  4. #include <exec/resident.h>
  5. #include <exec/errors.h>
  6. #include <exec/semaphores.h>
  7. #include <devices/trackdisk.h>
  8.  
  9. /*
  10.  * Set SysBase to a local variable, that loads directly from 4 when it
  11.  * has to be reloaded
  12.  */
  13. #define BASE_EXT_DECL
  14. #define BASE_NAME (*(void **)4)
  15. #include <inline/exec.h>
  16.  
  17. #include "device.h"
  18.   
  19. /*
  20.  * read a block from the cache, if it's stored there. 
  21.  * On success, return 1, else return 0.
  22.  */
  23. int
  24. lookup_cache (struct scsi_unit *su, ulong block_num, ubyte *buf)
  25. {
  26.   int i;
  27.   struct cache_cb *ccb;
  28.   
  29.   for (i = 0, ccb = su->scu_ccb; i < CCB_PER_UNIT; ++i, ++ccb)
  30.     {
  31.       /* if the block is valid and the block number is right ? */
  32.       if (ccb->ccb_usage>=0 && ccb->ccb_block_num == block_num)
  33.     {
  34.       CopyMem(ccb->ccb_buf, buf, UNIT_SEC_SIZE);
  35.       ++ccb->ccb_usage;
  36.       return 1;
  37.         }
  38.     }
  39.   return 0;
  40. }
  41.  
  42. /*
  43.  * write a block into the cache. use the least used position 
  44.  */
  45. void
  46. update_cache (struct scsi_unit *su, long block_num, ubyte *buf)
  47. {
  48.   int i, min_usage;
  49.   struct cache_cb *ccb, *min_ccb;
  50.   
  51.   min_usage = 0x7fffffff; /* I'm pretty sure some ccb will beat this:-)) */
  52.   
  53.   for (i = 0, min_ccb = ccb = su->scu_ccb; i < CCB_PER_UNIT; ++i, ++ccb)
  54.     {
  55.       if (ccb->ccb_block_num == block_num)
  56.     {
  57.       /* in that case the usage-count doesn't matter, we update
  58.        * "our" block .*/
  59.       ++ccb->ccb_usage;
  60.       CopyMem(buf, ccb->ccb_buf, UNIT_SEC_SIZE);
  61.       return;
  62.     }
  63.       if (ccb->ccb_usage < min_usage)
  64.     {
  65.       min_usage = ccb->ccb_usage;
  66.       min_ccb = ccb;
  67.     }
  68.     }
  69.   /* so we'll have to overwrite the least used slot */
  70.   min_ccb->ccb_block_num = block_num;
  71.   min_ccb->ccb_usage = 0;
  72.   CopyMem(buf, min_ccb->ccb_buf, UNIT_SEC_SIZE);
  73. }
  74.  
  75. /*
  76.  * This invalidates all cache blocks of this unit. Use this, if 
  77.  * you have a multi-block write, as its faster to invalidate the cache
  78.  * than to split up the write to fit into our cache-management...
  79.  */
  80. void
  81. invalidate_cache (struct scsi_unit *su)
  82. {
  83.   int i;
  84.   struct cache_cb *ccb;
  85.   
  86.   for (i = 0, ccb = su->scu_ccb; i < CCB_PER_UNIT; ++i, ++ccb)
  87.     ccb->ccb_usage = -1;
  88. }
  89.  
  90.