1

我对缓存很陌生,我想知道是否有人可以帮助我理解这种方法。如果它实际上返回整个缓存块指针,它如何获取标签?*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;
}
4

3 回答 3

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.

于 2012-11-09T16:01:44.397 回答
0

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

于 2012-11-09T15:58:33.227 回答
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;
}
于 2012-11-09T16:01:15.863 回答