⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cache.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
        r_val = -1;
      C->pool[i].dirty = CLEAN;
    }
  }
  if (written && !r_val)
    r_val = 1;

  /*-------------------------------------------------------------------*/
  /* Reset the dirty flags.                                            */
  /*-------------------------------------------------------------------*/
  C->dirty_old = C->dirty_new = FALSE;

  return r_val;
}

/***********************************************************************/
/*  FreeSector: Unpin the specified cache entry                        */
/*                                                                     */
/*      Inputs: ep = pointer to sector's cache entry                   */
/*              C = cache                                              */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
int FreeSector(CacheEntry **ep, Cache *C)
{
  int r_val = 0;
  CacheEntry *entry = *ep;

  /*-------------------------------------------------------------------*/
  /* Pin count can never be less than one.                             */
  /*-------------------------------------------------------------------*/
  PfAssert(entry->pin_cnt > 0);

  /*-------------------------------------------------------------------*/
  /* Decrement pin_count and if 0, put it on the LRU list.             */
  /*-------------------------------------------------------------------*/
  if (--entry->pin_cnt == 0)
    put_into_tail(entry, C);

  /*-------------------------------------------------------------------*/
  /* Clear the entry.                                                  */
  /*-------------------------------------------------------------------*/
  *ep = NULL;

#if FFS_CACHE_WRITE_THROUGH
  /*-------------------------------------------------------------------*/
  /* If write through cache and sector dirty, write it now.            */
  /*-------------------------------------------------------------------*/
  if (entry->dirty != CLEAN)
  {
    if (C->write(entry, TRUE))
      r_val = -1;
    entry->dirty = CLEAN;
  }
#endif

  return r_val;
}

/***********************************************************************/
/* RemoveFromCache: Remove specified sector from cache, if present     */
/*                                                                     */
/*      Inputs: C = cache to look into                                 */
/*              sector_number = sector to look for                     */
/*              new_sector = new value for sector_number if it matches */
/*                           the sector to be removed                  */
/*                                                                     */
/***********************************************************************/
void RemoveFromCache(Cache *C, int sector_number, int new_sector)
{
  int location = hash(sector_number, C->pool_size);
  CacheEntry *entry;

  /*-------------------------------------------------------------------*/
  /* If getting sector_number, update it with the new value.           */
  /*-------------------------------------------------------------------*/
  if (new_sector != -1 && C->sector_number == sector_number)
  {
    C->sector_number = new_sector;
    return;
  }

  /*-------------------------------------------------------------------*/
  /* Loop through hash bucket, looking for specified entry.            */
  /*-------------------------------------------------------------------*/
  for (entry = C->hash_tbl[location]; entry; entry = entry->next_hash)
  {
    /*-----------------------------------------------------------------*/
    /* If matching entry is found, remove it from the cache.           */
    /*-----------------------------------------------------------------*/
    if (entry->sect_num == sector_number)
    {
      /*---------------------------------------------------------------*/
      /* If entry is not on LRU list, add it.                          */
      /*---------------------------------------------------------------*/
      if (entry->pin_cnt)
        put_into_tail(entry, C);

      /*---------------------------------------------------------------*/
      /* Reset the entry.                                              */
      /*---------------------------------------------------------------*/
      entry->sect_num = -1;
      entry->dirty = CLEAN;
      entry->pin_cnt = 0;
      entry->hash_loc = NULL;

      /*---------------------------------------------------------------*/
      /* Remove entry from hash bucket list.                           */
      /*---------------------------------------------------------------*/
      if (entry->prev_hash)
        entry->prev_hash->next_hash = entry->next_hash;
      else
        C->hash_tbl[location] = entry->next_hash;
      if (entry->next_hash)
        entry->next_hash->prev_hash = entry->prev_hash;
      entry->next_hash = entry->prev_hash = NULL;
      entry->hash_loc = NULL;

      break;
    }
  }
}

/***********************************************************************/
/* UpdateFilePtrs: Adjust all cache entry file_ptrs that point to old  */
/*              location                                               */
/*                                                                     */
/*      Inputs: C = pointer to cache to be used                        */
/*              old_fptr = old file_ptr value                          */
/*              new_fptr = new file_ptr value                          */
/*                                                                     */
/***********************************************************************/
void UpdateFilePtrs(Cache *C, const void *old_fptr, void *new_fptr)
{
  int i;

  /*-------------------------------------------------------------------*/
  /* Loop over every sector in cache.                                  */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < C->pool_size; ++i)
  {
    /*-----------------------------------------------------------------*/
    /* If a cache entry file_ptr points to old, change to new.         */
    /*-----------------------------------------------------------------*/
    if (C->pool[i].file_ptr == old_fptr)
      C->pool[i].file_ptr = new_fptr;
  }
}

/***********************************************************************/
/* UpdateCache: Reposition the specified entry in the cache with a new */
/*              sector number                                          */
/*                                                                     */
/*      Inputs: entry = entry to sector in cache                       */
/*              new_sector = new sector value                          */
/*              C = pointer to cache to be used                        */
/*                                                                     */
/***********************************************************************/
void UpdateCache(CacheEntry *entry, int new_sector, const Cache *C)
{
  int new_location = hash(new_sector, C->pool_size);

  /*-------------------------------------------------------------------*/
  /* Update the sector value to new one.                               */
  /*-------------------------------------------------------------------*/
  entry->sect_num = new_sector;

  /*-------------------------------------------------------------------*/
  /* Take entry for old sector out of hash table.                      */
  /*-------------------------------------------------------------------*/
  if (entry->prev_hash)
    entry->prev_hash->next_hash = entry->next_hash;
  else
    *entry->hash_loc = entry->next_hash;
  if (entry->next_hash)
    entry->next_hash->prev_hash = entry->prev_hash;

  /*-------------------------------------------------------------------*/
  /* Put entry for new sector in hash table.                           */
  /*-------------------------------------------------------------------*/
  entry->next_hash = C->hash_tbl[new_location];
  entry->prev_hash = NULL;
  if (entry->next_hash)
    C->hash_tbl[new_location]->prev_hash = entry;
  C->hash_tbl[new_location] = entry;
  entry->hash_loc = &C->hash_tbl[new_location];
}

/***********************************************************************/
/* UpdateCacheSectNum: Update an entry's sector number                 */
/*                                                                     */
/*      Inputs: C = pointer to cache structure                         */
/*              old_sector = sector to be replaced                     */
/*              new_sector = replace value                             */
/*                                                                     */
/***********************************************************************/
void UpdateCacheSectNum(Cache *C, int old_sector, int new_sector)
{
  CacheEntry *entry;

  /*-------------------------------------------------------------------*/
  /* If sector being replaced is the current sector being cached,      */
  /* adjust current sector being cached.                               */
  /*-------------------------------------------------------------------*/
  if (old_sector == C->sector_number)
    C->sector_number = new_sector;

  /*-------------------------------------------------------------------*/
  /* Get head of list where entry for old sector would be in cache.    */
  /*-------------------------------------------------------------------*/
  entry = C->hash_tbl[hash(old_sector, C->pool_size)];

  /*-------------------------------------------------------------------*/
  /* Look if sector is in the cache.                                   */
  /*-------------------------------------------------------------------*/
  for (; entry; entry = entry->next_hash)
    if (entry->sect_num == old_sector)
      UpdateCache(entry, new_sector, C);
}

/***********************************************************************/
/*     InCache: Check if a sector is in cache                          */
/*                                                                     */
/*      Inputs: C = pointer to cache structure                         */
/*              sector = sector to check for                           */
/*              buf = pointer to data if sector in cache               */
/*                                                                     */
/*     Returns: Cache entry if sector in cache, NULL otherwise         */
/*                                                                     */
/***********************************************************************/
CacheEntry* InCache(const Cache *C, int sector, ui8 **buf)
{
  CacheEntry *entry = C->hash_tbl[hash(sector, C->pool_size)];

  /*-------------------------------------------------------------------*/
  /* Look if sector is in the cache.                                   */
  /*-------------------------------------------------------------------*/
  for (; entry; entry = entry->next_hash)
    if (entry->sect_num == sector)
    {
      *buf = entry->sector;
      return entry;
    }

  return NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -