//LRU算法返回该表所包含的所有文件中最老的块的指针(最近最少使用的块) BlockInfo* FileHandle::LRUAlgorithm() { FileInfo* fp = first_file_; //最老的块的前一块 BlockInfo* oldestbefore = NULL; //最老的块 BlockInfo* oldest = fp->GetFirstBlock(); //找到最老的块 while (fp != NULL) { BlockInfo* bpbefore = NULL; BlockInfo* bp = fp->GetFirstBlock(); while (bp != NULL) { if (bp->get_age() > oldest->get_age()) { oldestbefore = bpbefore; oldest = bp; } bpbefore = bp; bp = bp->GetNext(); } fp = fp->GetNext(); } //如果最老的块被修改过,则把它的内容写回文件 if (oldest->get_dirty()) oldest->WriteInfo(path_); //如果最老的块的前一位置是空块,则说明第一块就是最老的块。这时因为要移除oldest,所有该文件第一块就变成了oldest->next if (oldestbefore == NULL) oldest->GetFile()->SetFirstBlock(oldest->GetNext()); else oldestbefore->SetNext(oldest->GetNext()); //将最老的块年龄重置为0 oldest->ResetAge(); //oldest的next置为null oldest->SetNext(NULL); //返回最老的块的指针 return oldest; }