cache.c - #include#include#include#include ...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <assert.h> #include <stdio.h> #include <stdlib.h> #include "cache.h" // initialize the cache by allocating space for all of the cache // blocks and initializing them to be invalid and with 0 last access // times. void init_cache(cache_t *cache) { // initialize the cache blocks to invalid for (int i = 0 ; i < NUM_SETS ; i ++) { for (int j = 0 ; j < 2 ; j ++) { cache->sets[i].blocks[j].valid = false; } cache->sets[i].lru = 0; } cache->accesses = 0; cache->hits = 0; cache->writebacks = 0; } // given an address, extract the tag field unsigned address_to_tag(unsigned address) { return (address >> (OFFSET_SZ+INDEX_SZ)); } // given an address, extract the index field unsigned address_to_index(unsigned address) { unsigned mask = (1 << INDEX_SZ) - 1; return (address >> OFFSET_SZ) & mask; } // Given an address, look up in cache "cache" to see if that // address hits. If it does update the LRU. // Include necessary updates to the access, hit, writeback counters bool find_block_and_update_lru(cache_t *cache, unsigned address, bool write) { unsigned tag = address_to_tag(address); unsigned index = address_to_index(address);
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern