Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. public class AssocCache
  2.   extends Cache
  3. {
  4.   //declaring global variables used
  5.   int[] cache_entries;
  6.   boolean[] is_valid_object;
  7.   Object[] object_set_cache;
  8.   int valid_entry_position;
  9.   int cache_size;
  10.   int line_size;
  11.  
  12.   AssocCache(int new_cache_size, int new_line_size)
  13.   {
  14.     //initialsing variables
  15.     cache_size = new_cache_size;
  16.     line_size = new_line_size;
  17.     cache_entries = new int[cache_size / line_size];
  18.     object_set_cache = new Object[cache_size / line_size];
  19.     is_valid_object = new boolean[cache_size / line_size];
  20.     //initialise boolean valid object check values
  21.     for (int index = 0; index < cache_size / line_size; index++) is_valid_object[index] = false;
  22.   }
  23.  
  24.   public Object cacheSearch(int addr) {
  25.  
  26.     for(int index = 0; index < cache_size/line_size; index++) {
  27.       //checking if there is a valid cache entry at the given address
  28.       if ((is_valid_object[index] != false) && (cache_entries[index] == (addr/ line_size))) {
  29.         valid_entry_position = index;    //remembering cache entry position globally for later use
  30.         return object_set_cache[index];  //returning the object set to the value of the cache entry
  31.       }
  32.     }
  33.  
  34.     return null; //return null if no valid entry has been found
  35.   }
  36.  
  37.   public oldCacheLineInfo cacheNewEntry(int addr) {
  38.     for(int index = 0; index < cache_size/line_size; index++) {
  39.       oldCacheLineInfo localOldCacheLineInfo = new oldCacheLineInfo();
  40.       localOldCacheLineInfo.old_valid = is_valid_object[index]; //store old valid value
  41.       cache_entries[index] = (addr / line_size); //set new cache entry
  42.       is_valid_object[index] = true; //set if valid entry to true
  43.       localOldCacheLineInfo.data = (object_set_cache[index] = new Integer(-1)); //set new data
  44.       return localOldCacheLineInfo;
  45.     }
  46.     return null;
  47.   }
  48.  
  49.   public void cacheWriteData(Object indata) {
  50.     object_set_cache[valid_entry_position] = indata; //update data field of object set cache using the position remembered
  51.   }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement