home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / Stuart's Tech Notes / Disk Cache problems / WriteThrough.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-10  |  1.6 KB  |  63 lines  |  [TEXT/KAHL]

  1. // Disk cache bypass INIT
  2. // Copyright (C) April 1994 Stuart Cheshire <cheshire@cs.stanford.edu>
  3.  
  4. #include <Traps.h>
  5.  
  6. // I know an address should be defined as a pointer type (eg 'void *'),
  7. // not a 'long', but Apple's Get & Set TrapAddress calls are defined to
  8. // use longs, so it's not my fault
  9.  
  10. typedef struct { unsigned short opcode; long addr; } JmpInstruction;
  11.  
  12. typedef void ShowIconFamily(short iconId);
  13.  
  14. void main(void);
  15.  
  16. static void entry_point(void)    /* no stack frame please ! */
  17.     {
  18.     asm    {    
  19.     @0        move.l    a4, -(sp)
  20.             move.l    a0, -(sp)
  21.             DetachResource
  22.             lea        @0, a0
  23.             move.l    a0, a4
  24.             bsr        main
  25.     exit:    move.l (sp)+, a4
  26.         }
  27.     }
  28.  
  29. void my_write(void);  void sys_write(void);
  30. static void my_write(void)
  31.     {
  32.     asm {
  33.         tst.w    IOParam.ioRefNum(a0)            ; Is this a file write?
  34.         bmi.s    @sys_write
  35.         cmp.l    #1024, IOParam.ioReqCount(a0)    ; Is it at leask 1K?
  36.         blo.s    @sys_write
  37.         ori.w    #0x20, IOParam.ioPosMode(a0)    ; Set "Don't cache" bit
  38.     extern sys_write:
  39.         jmp        0x12345678                        ; Resume the system call
  40.         }
  41.     }
  42.  
  43. static Boolean TrapAvailable(unsigned long trap)
  44.     {
  45.     TrapType tType = (trap & 0x800 ? ToolTrap : OSTrap);
  46.     if (trap & 0x800)                // if it is a ToolBox Trap
  47.         {
  48.         unsigned long n = 0x400;    // number of toolbox traps
  49.         if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  50.             n = 0x200;
  51.         if ((trap &= 0x7FF) >= n) trap = _Unimplemented;
  52.         }
  53.     return(NGetTrapAddress(trap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  54.     }
  55.  
  56. void main(void)
  57.     {
  58.     register long *p = &((JmpInstruction*)sys_write)->addr;
  59.     *p = (long)GetOSTrapAddress(_Write);
  60.     SetOSTrapAddress((ProcPtr)&my_write,_Write);
  61.     if (TrapAvailable(_HWPriv)) FlushInstructionCache();
  62.     }
  63.