home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / fp / ifp_unix.lzh / ifp / interp / cache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  4.6 KB  |  162 lines

  1.  
  2. /****** cache.c *******************************************************/
  3. /**                                                                  **/
  4. /**                    University of Illinois                        **/
  5. /**                                                                  **/
  6. /**                Department of Computer Science                    **/
  7. /**                                                                  **/
  8. /**   Tool: IFP                         Version: 0.5                 **/
  9. /**                                                                  **/
  10. /**   Author:  Arch D. Robison          Date:   May 1, 1985          **/
  11. /**                                                                  **/
  12. /**   Revised by: Arch D. Robison       Date: July 29, 1986          **/
  13. /**                                                                  **/
  14. /**   Principal Investigators: Prof. R. H. Campbell                  **/
  15. /**                            Prof. W. J. Kubitz                    **/
  16. /**                                                                  **/
  17. /**                                                                  **/
  18. /**------------------------------------------------------------------**/
  19. /**   (C) Copyright 1987  University of Illinois Board of Trustees   **/
  20. /**                       All Rights Reserved.                       **/
  21. /**********************************************************************/
  22.  
  23. /*
  24.  * NOTE: Function HashOb assumes a FPfloat is either 1x or 2x the size
  25.  *      of a long.
  26.  */
  27.  
  28. #include "struct.h"
  29. #include "cache.h"
  30. #include <stdio.h>
  31.  
  32. #if ECACHE
  33.  
  34. CacheEntry ECache[CACHE_SIZE];
  35.  
  36. CacheRec Cache [4] = {
  37.    {0,0,0,0,"Prim"},
  38.    {0,0,0,0,"User"},
  39.    {0,0,0,0,"PFO"},
  40.    {0,0,0,0,"Total"},
  41. };
  42.  
  43. #define ArraySize(A) (sizeof(A)/sizeof(A[0]))
  44.  
  45. /*
  46.  * Print the cache statistics on stdout and clear the cache statistics tallies.
  47.  */
  48. void ShowCache ()
  49.    {
  50.       CacheRec *C,*T= &Cache[CacheTotal];
  51.       CacheEntry *E;
  52.       int Tally=0;
  53.       
  54.       for (E=ECache; E < ArrayEnd(ECache); E++) 
  55.      if (E->EC_Fun != NULL) Tally++;
  56.  
  57.       printf ("%d/%d = %g full cache\n", 
  58.           Tally, ArraySize (ECache), (double) Tally / ArraySize (ECache));
  59.  
  60.       T->Enable = 0;
  61.       for (C= &Cache[0]; C<&Cache[4]; C++) {
  62.      if (C->Enable) {
  63.         Cache[CacheTotal].Enable=1;
  64.         printf ("%s:\t%d hits in %d looks = %g%% hit rate [%d evictions]\n",
  65.             C->Name,C->Hits,C->Looks,
  66.             100.0 * C->Hits / (C->Looks ? C->Looks : 1), C->Evictions);
  67.         T->Hits  += C->Hits; 
  68.         T->Looks += C->Looks; 
  69.         T->Evictions += C->Evictions; 
  70.         C->Hits = C->Looks = C->Evictions = 0;
  71.      }
  72.       }
  73.       if (!T->Enable) printf ("The cache is disabled\n");
  74.    }
  75.  
  76. #if DEBUG
  77. void PrintCache (Message,E)
  78.    char *Message;
  79.    CacheEntry *E;
  80.    {
  81.       printf ("ECache %s ",Message);    OutObject (&E->EC_In);
  82.       printf (" : ");            OutNode   (E->EC_Fun);
  83.       printf (" -> ");            OutObject (&E->EC_Out);
  84.       printf ("\n");
  85.    }
  86. #endif /* DEBUG */
  87.  
  88. /*
  89.  * HashOb
  90.  *
  91.  * HashOb computes an integer function (hash code) of an object.
  92.  *
  93.  * Input
  94.  *    X = object
  95.  * Output
  96.  *     result = hash code
  97.  */
  98. int HashOb (X)
  99.    ObjectPtr X;
  100.    {
  101.       register long H;
  102.       register ListPtr P;
  103.  
  104.       switch (X->Tag) {
  105.       case BOTTOM:  H = 2305;                       break;
  106.       case BOOLEAN: H = X->Bool;                    break;
  107.       case INT:     H = X->Int * 9;                 break;
  108.       case FLOAT:
  109.          if (sizeof (FPfloat) == 2*sizeof (long))
  110.         H = ((long *)&(X->Float))[0] + ((long *)&(X->Float))[1];
  111.          else if (sizeof (FPfloat) == sizeof (long))
  112.         H = ((long *)&(X->Float))[0];
  113.          else
  114.         fprintf (stderr,"HashOb: can't hash floats on this machine!\n");
  115.          break;
  116.       case STRING:  H = (long) X->String;           break;
  117.       case LIST:
  118.          H = 5298;
  119.          for (P=X->List; P!=NULL; P=P->Next)
  120.         H = H * 0x1243 + HashOb (&P->Val);
  121.          break;
  122.       case NODE:    H = (long) X->Node * 5;         break;
  123.       case CODE:    H = (long) X->Code.CodePtr +
  124.                 (long) X->Code.CodeParam;   break;
  125.       default:
  126.          fprintf (stderr,"HashOb: invalid tag (%d)\n",X->Tag);
  127.          break;
  128.       }
  129.       return H;
  130.    }
  131.  
  132. ClearCache ()            /* Clear all entries from the cache. */
  133.    {
  134.       CacheEntry *C;
  135.  
  136.       for (C=ECache+CACHE_SIZE; --C >= ECache; ) {
  137.      RepTag (&C->EC_In, BOTTOM);
  138.      C->EC_Fun = NULL;
  139.      RepTag (&C->EC_Out,BOTTOM);
  140.       }
  141.    }
  142.  
  143. InitCache ()            /* Initialize the cache */
  144.    {
  145.       register CacheEntry *E;
  146.       CacheRec *C;
  147.  
  148.       printf (" (cache");
  149.       for (C=Cache; C<&Cache[3]; C++)
  150.      if (C->Enable) printf (" %s",C->Name);
  151.       printf (")");
  152.  
  153.       for (E=ECache+CACHE_SIZE; --E >= ECache; ) {
  154.      E->EC_In. Tag = BOTTOM;
  155.      E->EC_Fun = NULL;
  156.      E->EC_Out.Tag = BOTTOM;
  157.       }
  158.    }
  159.  
  160. #endif /* ECACHE */
  161.  
  162.