Skip to content

Instantly share code, notes, and snippets.

@kingdido999
Last active August 29, 2015 14:17
Show Gist options
  • Save kingdido999/29b1fc713037828b1864 to your computer and use it in GitHub Desktop.
Save kingdido999/29b1fc713037828b1864 to your computer and use it in GitHub Desktop.
cs460-pa1
/*
* CS460
*
* This will put a buffer into MRU list. This function should be only called by
* unpinning a buffer. Since it is MRU, we always put at head position. This
* is consistent with StrategyGetBuffer where we pick also at head position.
* This is not the same as InvalidateBuffer where a buffer is surely not useful
* any more and should be put into free buffer instead.
*
* MRU是将buf插入头部,LRU是将buf插入尾部
*/
void StrategyMRUEnqueue(volatile BufferDesc *buf)
{
// 如果buf已经存在于list,则返回
if (buf->mruNext != MRU_NOT_IN_LIST)
{
return;
}
// link to prev
buf->mruPrev = MRUPREV_HEAD_OF_LIST;
// link to next
buf->mruNext = StrategyControl->mruHead;
StrategyControl->mruHead = buf->buf_id;
// 若list不为空
if (buf->mruNext != MRUNEXT_END_OF_LIST)
{
// 确认list head指向的buf的mruPrev指向list head
Assert((BufferDescriptors + buf->mruNext)->mruPrev == MRUPREV_HEAD_OF_LIST);
// 将list head的mruPrev指向需要enque的buf
(BufferDescriptors + buf->mruNext)->mruPrev = buf->buf_id;
}
}
/*
* CS460
*/
void StrategyMRUDequeue(volatile BufferDesc *buf)
{
/* if it is already dequeued. this may happen if buf comes from free list */
// 所有被deque过的buffer都会指向MRU_NOT_IN_LIST (e.g., line 68, 69)
if (buf->mruNext == MRU_NOT_IN_LIST)
{
return;
}
// StrategyControl的mruHead已经mruTail分别指向buffer list的头部和尾部
// Assert用来debug,如果mruHead指向MRUNEXT_END_OF_LIST,表示这个list里面已经没有buf可以deque
Assert(StrategyControl->mruHead != MRUNEXT_END_OF_LIST);
/* unlink from prev */
if (buf->mruPrev == MRUPREV_HEAD_OF_LIST)
StrategyControl->mruHead = buf->mruNext;
else
// 这行代码我在piazza上问过,你可以去看
(BufferDescriptors + buf->mruPrev)->mruNext = buf->mruNext;
/* unlink from next */
if (buf->mruNext == MRUNEXT_END_OF_LIST)
StrategyControl->mruTail = buf->mruPrev;
else
(BufferDescriptors + buf->mruNext)->mruPrev = buf->mruPrev;
// buf已经被deque,所以放入MRU_NOT_IN_LIST
buf->mruPrev = MRU_NOT_IN_LIST;
buf->mruNext = MRU_NOT_IN_LIST;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment