- 论坛徽章:
- 1
|
- 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;
- }
复制代码
搂住这个函数有问题。。。
在调用这个函数时,对返回值进行判断。
>=0...说明节点存在。。
但是对于-1,就不能确定了:
可以是还没判断就因为锁出错返回-1;也可以是没有这个节点返回-1(因为链表里的最后一个节点的next是-1)。
(算了,不愿回答就算了。。。)
- int list_findnode(int hashindex, const void* key, ccache_t* cache, cmpfun cmp)
- {
- hashitem_t *hashitem = HASH_ITEM(cache, hashindex);
- int index = hashitem->first;
- node_t *node;
- void* nodekey;
- while (0 <= index)
- {
- node = NODE(cache, index);
- nodekey = NODE_KEY(node);
- if (!cmp(key, nodekey))
- {
- break;
- }
- index = node->next;
- }
- return index;
- }
复制代码
[ 本帖最后由 yangsf5 于 2009-1-5 13:06 编辑 ] |
|