redis
最近在学习redis中的双向链表实现, 感受了一下redis结构设计的精巧, 写篇笔记总结一下自己的学习
双向链表
redis 中的双向泛型链表实现, 下面我们重点分析一下redis是如何实现它的双向和泛型, 具体文件为adlist.h
和adlist.c
typedef struct listNode {
struct listNode *prev;
struct listNode *next;
void *value;
} listNode;
typedef struct listIter {
listNode *next;
int direction;
} listIter;
typedef struct list {
listNode *head;
listNode *tail;
void *(*dup)(void *ptr);
void (*free)(void *ptr);
int (*match)(void *ptr, void *key);
unsigned long len;
} list;
- listNode结构体
- listNode结构体中同时包含前向和后向指针, 方便向前或向后遍历整个链表
- listNode结构体中的void* value用来实现伪泛型
- list结构体
- list结构体中包含整个链表的
头部指针head
和尾部指针tali
- list结构体中的dup是函数指针, 该函数原型为void* function(void *ptr), 用来定义如何复制链表中的元素, 由于在listNode中存储的是value指针, 所以定义的dup函数可以用来决定拷贝的方式是
深拷贝
或浅拷贝
- list结构体中的free是函数指针, 该函数原型为void function(void *ptr), 用来定义如何释放链表中的节点的value指针
- list结构体中的match是函数指针, 该函数原型为int function(void *ptr, void *key), 用来定义如何查找匹配相应的元素, 因为listNode中存储的是指针值, 所以我们不可以直接通过
ptr == key
来判定两个键相等, match函数可以用来定义如何使用ptr指针和key指针来决定其内容是否相等
- list结构体中包含整个链表的
关于链表的宏
#define listLength(l) ((l)->len) // 返回链表长度
#define listFirst(l) ((l)->head) // 返回链表头部指针
#define listLast(l) ((l)->tail) // 返回链表尾部指针
#define listPrevNode(n) ((n)->prev) // 返回n节点的上一个结点指针
#define listNextNode(n) ((n)->next) // 返回n节点的下一个结点指针
#define listNodeValue(n) ((n)->value) // 返回n节点的value指针
#define listSetDupMethod(l,m) ((l)->dup = (m)) // 设置链表的dup函数
#define listSetFreeMethod(l,m) ((l)->free = (m)) // 设置链表的free函数
#define listSetMatchMethod(l,m) ((l)->match = (m)) // 设置链表的match函数
#define listGetDupMethod(l) ((l)->dup) // 返回链表的dup函数指针
#define listGetFree(l) ((l)->free) // 返回链表的free函数指针, 强迫症表示不知道为啥没有Method后缀...
#define listGetMatchMethod(l) ((l)->match) // 返回链表的match函数指针
关于链表的函数
listCreate
list *listCreate(void)
{
struct list *list;
if ((list = zmalloc(sizeof(*list))) == NULL)
return NULL;
list->head = list->tail = NULL;
list->len = 0;
list->dup = NULL;
list->free = NULL;
list->match = NULL;
return list
}
listEmpty
listEmpty不是用来判断链表是否为空的函数, 它是用来将一个链表清空的函数, 它会将链表所有节点和节点中value指针指向的内存释放
/* Remove all the elements from the list without destroying the list itself. */
listEmpty(list *list)
{
unsigned long len;
listNode *current, *next;
current = list->head;
len = list->len;
while(len--) {
next = current->next;
if (list->free) list->free(current->value);
zfree(current);
current = next;
}
list->head = list->tail = NULL;
list->len = 0;
}
listRelease
释放整个链表包括list指针本身指向的内存
/* Free the whole list
*
* This function can't fail. */
void listRelease(list *list)
{
listEmpty(list);
zfree(list);
}
listAddNodeHead
在头部添加一个节点
/* Add a new node to the list, to head, containing the specified 'value'
* pointer as value.
*
* On error, NULL is returned and no operation is performed (i.e. the
* list remains unaltered).
* On success the 'list' pointer you pass to the function is returned. */
list *listAddNodeHead(list *list, void *value)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
/* 如果在链表为空的时候插入新节点, 则需要同时改变list->head 和 list->tail */
list->head = list->tail = node;
node->prev = NULL;
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len++;
return list;
}
listAddNodeTail
在尾部添加一个节点
list *listAddNodeTail(list *list, void *value)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = NULL;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++;
return list;
}
listInsertNode
在old_node
节点之前或之后插入新节点, 当after为0时在old_node
之前插入, 当after为1时在old_node
之后插入
list *listInsertNode(list *list, listNode *old_node, void *value, int after)
{
listNode *node;
if ((node = zmalloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
/* after为0时, 插入到old_node之前. after为1时, 插入到old_node之后. */
if (after) {
node->prev = old_node;
node->next = old_node->next;
/* 当old_node为list的尾结点时, 需要更换list->tail */
if (list->tail == old_node) {
list->tail = node;
}
} else {
node->next = old_node;
node->prev = old_node->prev;
/* 当old_node为list的头结点时, 需要更换list->head */
if (list->head == old_node) {
list->head = node;
}
}
if (node->prev != NULL) {
node->prev->next = node
}
if (node->next != NULL) {
node->next->prev = node;
}
list->len++;
return list;
}
listDelNode
删除节点value指针指向的内存和节点本身的内存
void listDelNode(list *list, listNode *node)
{
if (node->prev) {
node->prev->next = node->next;
} else {
/* 如果node->prev为空, 代表node为list的头节点, 删除node后, 需要更改list->head */
list->head = node->next;
}
if (node->next) {
node->next->prev = node->prev;
} else {
/* 如果node->next为空, 代表node为list的尾节点, 删除node后, 需要更改list->tail */
list->tail = node->prev;
}
/* 调用list中的free函数释放value指针指向的内存 */
if (list->free) list->free(node->value);
/* 释放node节点内存 */
zfree(node);
list->len--;
}