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

📄 ccache.c

📁 一种用于编译时头文件的缓存处理缓存源代码,使用起来就像C/C++编译器的缓存预处理器,简化该功能相应的环境改变时对内核的影响。
💻 C
字号:
/********************************************************************
	created:	2008/01/23
	filename: 	ccache.c
	author:		Lichuang
                
	purpose:    
*********************************************************************/

#include "ccache.h"
#include "lock.h"
#include "shm.h"
#include "hash.h"
#include "operator.h"
#include "lrulist.h"
#include "node.h"
#include <string.h>

ccache_t* create_cache(int nodenum, int datasize, int hashitemnum, int keysize, const char* mapfilename, int attach)
{
    return create_shm(nodenum, datasize, hashitemnum, keysize, mapfilename, attach);
}

int destroy_cache(ccache_t* cache)
{
    return destroy_shm(cache);
}

int insert_data(const void* key, const void* data, ccache_t* cache, cmpfun cmp, delfun del)
{
    int hashindex = hash(key, cache), ret;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = INSERT_NODE(hashindex, key, data, cache, cmp, del);

    if (0 <= ret)
        linktolrulisthead(ret, cache);

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int find_data(const void* key, void* data, ccache_t* cache, cmpfun cmp)
{
    int hashindex = hash(key, cache), ret;
    void *nodedata;
    node_t *node;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = FIND_NODE(hashindex, key, cache, cmp);

    if (0 <= ret)
    {
        if (NULL != data)
        {
            node = NODE(cache, ret);
            nodedata = NODE_DATA(node);
            memcpy(data, nodedata, node->datasize);
        }
        linktolrulisthead(ret, cache);
    }

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int update_data(const void* key, const void* data, ccache_t* cache, cmpfun cmp)
{
    int hashindex = hash(key, cache), ret;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = UPDATE_NODE(hashindex, key, data, cache, cmp);

    if (0 <= ret)
        linktolrulisthead(ret, cache);

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int delete_data(const void* key, void* data, ccache_t* cache, cmpfun cmp)
{
    int hashindex = hash(key, cache), ret;
    node_t *node;
    void* nodedata;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = FIND_NODE(hashindex, key, cache, cmp);
    if (0 > ret)
    {
        unlock(&(cache->mutex));
        return -1;
    }

    ret = DELETE_NODE(hashindex, ret, cache, cmp);
    if (0 <= ret)
    {
        node = NODE(cache, ret);
        if (NULL != data)
        {
            nodedata = NODE_DATA(node);
            memcpy(data, nodedata, node->datasize);
        }
        node->next = cache->firstfreenode;
        node->prev = -1;
        if (0 <= cache->firstfreenode)
        {
            node = NODE(cache, cache->firstfreenode);
            node->prev = ret;
        }
        cache->firstfreenode = ret;
    }

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int update_or_insert_data(const void* key, void* data, ccache_t* cache, cmpfun cmp, delfun del, updatefun update)
{
    int hashindex = hash(key, cache), ret;
    node_t *node;
    void *orgdata;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    ret = FIND_NODE(hashindex, key, cache, cmp);

    if (0 <= ret)
    {
        node = NODE(cache, ret);
        orgdata = NODE_DATA(node);

        if (NULL != update)
        {
            update(orgdata, data);
        }
        memcpy(orgdata, data, node->datasize);
    }
    else
    {
        ret = INSERT_NODE(hashindex, key, data, cache, cmp, del);
    }

    if (0 <= ret)
        linktolrulisthead(ret, cache);

    if (0 > unlock(&(cache->mutex)) || 0 > ret)
    {
        return -1;
    }

    return ret;
}

int visit_cache(ccache_t* cache, visitfun visit)
{
    int hashitem;

    if (0 > lock(&(cache->mutex)))
    {
        return -1;
    }

    for (hashitem = 0; hashitem < cache->hashitemnum; ++hashitem)
    {
        VISIT_NODES(cache, hashitem, visit);
    }

    if (0 > unlock(&(cache->mutex)))
    {
        return -1;
    }

    return 0;
}

⌨️ 快捷键说明

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