home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Tickle-4.0.sit.hqx / Tickle-4.0 / cbtree / src / lruhash.c < prev    next >
Text File  |  1993-05-22  |  3KB  |  136 lines

  1.  
  2. #include <stdlib.h>
  3. #include "cdefs.h"
  4. #include "lrucache.h"
  5.  
  6. #define HASH(l, pgno)    (pgno % l->lru_csize)
  7.  
  8. /*
  9.  *  LRUHASHGET -- Look up a block in the LRU cache by page number.
  10.  *
  11.  *    Parameters:
  12.  *        l -- LRU cache
  13.  *        pgno -- number of the logical page to get
  14.  *
  15.  *    Returns:
  16.  *        (CACHE_ENT *) pointer to the associated hash table entry
  17.  *        (if any), or NULL (if none).
  18.  */
  19.  
  20. CACHE_ENT *
  21. lruhashget(l, pgno)
  22.     LRUCACHE *l;
  23.     int pgno;
  24. {
  25.     int hashind;
  26.     CACHE_ENT *ce;
  27.  
  28.     hashind = HASH(l, pgno);
  29.  
  30.     /* walk the chain */
  31.     for (ce = l->lru_cache[hashind];
  32.          ce != (CACHE_ENT *) NULL;
  33.          ce = ce->c_chain) {
  34.         if (ce->c_pgno == pgno)
  35.             return (ce);
  36.     }
  37.  
  38.     return ((CACHE_ENT *) NULL);
  39. }
  40.  
  41. /*
  42.  *  LRUHASHPUT -- Add an entry for a given page to the cache hash table.
  43.  *
  44.  *    This routine assumes that the given page does not yet have an entry
  45.  *    in the table.
  46.  *
  47.  *    Parameters:
  48.  *        l -- LRU cache
  49.  *        pgno -- logical page number for this entry
  50.  *        lruent -- LRU list entry at which hash table entry should
  51.  *              point
  52.  *
  53.  *    Returns:
  54.  *        (CACHE_ENT *) pointer to the new hash table entry on success,
  55.  *        or NULL on failure.
  56.  *
  57.  *    Side Effects:
  58.  *        Allocates memory which should be freed when the hash table
  59.  *        entry is removed.
  60.  */
  61.  
  62. CACHE_ENT *
  63. lruhashput(l, pgno, lruent)
  64.     LRUCACHE *l;
  65.     int pgno;
  66.     LRU_ENT *lruent;
  67. {
  68.     int hashind;
  69.     CACHE_ENT *ce;
  70.  
  71.     if ((ce = (CACHE_ENT *) cbMALLOC((unsigned) sizeof(CACHE_ENT)))
  72.         == (CACHE_ENT *) NULL)
  73.         return ((CACHE_ENT *) NULL);
  74.  
  75.     hashind = HASH(l, pgno);
  76.  
  77.     ce->c_pgno = pgno;
  78.     ce->c_lruent = lruent;
  79.     ce->c_chain = l->lru_cache[hashind];
  80.     l->lru_cache[hashind] = ce;
  81.  
  82.     return (ce);
  83. }
  84.  
  85. /*
  86.  *  LRUHASHDEL -- Delete the entry for a given page from the LRU cache
  87.  *          hash table.
  88.  *
  89.  *    Parameters:
  90.  *        l -- LRU cache
  91.  *        pgno -- page number for which we should delete htable entry
  92.  *
  93.  *    Returns:
  94.  *        Zero on success, -1 on failure.
  95.  *
  96.  *    Side Effects:
  97.  *        Releases the memory occupied by the hash table entry.
  98.  */
  99.  
  100. int
  101. lruhashdel(l, pgno)
  102.     LRUCACHE *l;
  103.     int pgno;
  104. {
  105.     CACHE_ENT *ce;
  106.     CACHE_ENT *sce;
  107.     int hashind;
  108.  
  109.     sce = (CACHE_ENT *) NULL;
  110.     hashind = HASH(l, pgno);
  111.  
  112.     /* find the entry we want to delete */
  113.     for (ce = l->lru_cache[hashind];
  114.          ce != (CACHE_ENT *) NULL;
  115.          ce = ce->c_chain) {
  116.         if (ce->c_pgno == pgno)
  117.             break;
  118.         sce = ce;
  119.     }
  120.  
  121.     if (ce == (CACHE_ENT *) NULL)
  122.         return (-1);
  123.  
  124.     /* remove it from the chain */
  125.     if (sce == (CACHE_ENT *) NULL)
  126.         l->lru_cache[hashind] = ce->c_chain;
  127.     else
  128.         sce->c_chain = ce->c_chain;
  129.  
  130.     /* release it */
  131.     free ((char *) ce);
  132.  
  133.     /* success */
  134.     return (0);
  135. }
  136.