lrulist.c

来自「cache库 别人写的」· C语言 代码 · 共 82 行

C
82
字号
/********************************************************************
	created:	2008/01/23
	filename: 	lrulist.c
	author:		Lichuang
                
	purpose:    
*********************************************************************/

#include "lock.h"
#include "lrulist.h"
#include "node.h"

void linktolrulisthead(int index, cache_t* cache)
{
    if (-1 == cache->lrufirst && -1 == cache->lrulast)
    {
        cache->lrufirst = cache->lrulast = index;
        return;
    }

    if (index == cache->lrufirst)
    {
        return;
    }

    node_t* node = NODE(cache, index), *tmp;

    if (index == cache->lrulast)
    {
        cache->lrulast = node->lruprev;
    }

    int lrunext = node->lrunext, lruprev = node->lruprev;
    if (-1 != lrunext)
    {
        tmp = NODE(cache, lrunext);
        tmp->lruprev = lruprev;
    }
    if (-1 != lruprev)
    {
        tmp = NODE(cache, lruprev);
        tmp->lrunext = lrunext;
    }

    node->lrunext = cache->lrufirst;
    node->lruprev = -1;
    if (-1 != cache->lrufirst)
    {
        tmp = NODE(cache, cache->lrufirst);
        tmp->lruprev = index;
    }
    cache->lrufirst = index;
}

int  freefromlrulist(int index, cache_t* cache)
{
    node_t* node = NODE(cache, index), *tmp;
    int lrunext = node->lrunext, lruprev = node->lruprev;
    if (0 <= lrunext)
    {
        tmp = NODE(cache, lrunext);
        tmp->lruprev = lruprev;
    }
    if (0 <= lruprev)
    {
        tmp = NODE(cache, lruprev);
        tmp->lrunext = lrunext;
    }

    if (index == cache->lrufirst)
    {
        cache->lrufirst = lrunext;
    }
    if (index == cache->lrulast)
    {
        cache->lrulast = lruprev; 
    }

    return 0;
}

⌨️ 快捷键说明

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