📄 ccache.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 + -