了解C语言中的缓存方法

时间:2012-11-09 15:44:25

标签: c caching

我对缓存很新,我想知道是否有人可以帮我理解这种方法。如果它实际返回整个缓存块指针,它如何获取标记?什么是* bIndex在这里做什么,它的缩写是什么意思?

cacheBlock* getTag(int index, int tag, int *bIndex) {
  int i;

  for (i = 0; i < assoc; i ++ ) {   
    if (cache[index].block[i].tag == tag) {
      cacheBlock *targetBlock = &cache[index].block[i];
      *bIndex = i;
      return targetBlock;
    }
  }
  *bIndex = -1;
  return NULL;
}

3 个答案:

答案 0 :(得分:1)

下面的分析:

cacheBlock* getTag(int index, int tag, int *bIndex) 
{
  int i;

  // walk all blocks in cache[index] block[] table
  for (i = 0; i < assoc; i ++ ) 
  {
    // if the block association at this index matches this tag,
    //  then the block we're looking for is in the cache.   
    if (cache[index].block[i].tag == tag) 
    {
      // setup return value (this is unneeded, btw. simply setting
      //  *bIndex and returning (cache[index].block+i) would work).
      cacheBlock *targetBlock = &cache[index].block[i];

      // set out-param to inform them which block[i].tag matched in the
      //  block being returned by address. either this is actually not
      //  needed, or this is a bug, since we already return the precise
      //  block[i] entry by address (see above). the caller may like
      //  knowing *which* block[] entry matched for whatever reason.
      *bIndex = i;

      // return the matching cache[index].block[i] address
      return targetBlock;
    }
  }

  // no match condition. set offset to (-1) and return NULL.
  *bIndex = -1;
  return NULL;
}

话虽如此,我相信您应该检查此代码的调用者,因为他们收到的block []条目已经偏移到他们正在寻找的完全匹配。即返回的指针不需要* bIndex偏移量。如果他们使用它来从返回地址索引偏移,即他们的代码看起来像这样:

int bIndex = 0;
cacheBlock *pEntry = getTag(cache, tag, &bIndex);
if (pEntry != NULL)
{
    // do something with pEntry[bIndex]
}

这可能是一个错误,也许他们打算返回cache[index].block,而不是cache[index].block+i

答案 1 :(得分:0)

似乎有一个'全局'缓存数组,static cacheControl cache[SOMESIZE];或等价的(我们无法从代码中告诉cache的类型;它可能是指向已分配数据的指针一个固定的数组)。要搜索的缓存中的条目由index标识。在该缓存条目中,存在标记的缓存块。此代码对缓存块进行线性搜索,该缓存块由作为tag传递给函数的标记值标识。代码对缓存条目中的缓存块列表进行线性搜索(由代码中未定义的大小assoc控制 - 神秘)。退出函数时,bIndex指向的整数包含缓存中块的索引号(或-1);返回值是指向缓存块的指针。

答案 2 :(得分:0)

该功能的作用是通过一系列缓存块执行线性搜索。如果基于给定的整数标记值找到匹配,则返回指向一个的指针。也许在线评论可能会有所帮助:

cacheBlock* getTag(int index, int tag, int *bIndex) {
 int i;

 // The following iterates/increments through all? blocks for
 // the given cache index.  The variable assoc is global to this
 // function and I assume represents the number of entries for
 // each cache index.
 for (i = 0; i < assoc; i ++ ) {   
   // Check to see if a given block matches the one identified
   // by the passed in variable tag.
   if (cache[index].block[i].tag == tag) {
     // It did match so we will grab the address of that block
     cacheBlock *targetBlock = &cache[index].block[i];
     // And return (via parameter) the position at which it was found
     *bIndex = i;
     // And now return the address of (pointer to) the block
     return targetBlock;
   }
 }
 // we did not find the given entry, so pass back -1 as the
 // index to signify that.  And return NULL also to
 // signify that it was not found.
 *bIndex = -1;
 return NULL;
}