www.pudn.com > ccache.rar > lrulist.c


/******************************************************************** 
	created:	2008/01/23 
	filename: 	lrulist.c 
	author:		Lichuang 
                 
	purpose:     
*********************************************************************/ 
 
#include "lock.h" 
#include "lrulist.h" 
#include "node.h" 
 
void linktolrulisthead(int index, ccache_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, ccache_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; 
}