Redis3.0源码阅读笔记 (一)
源码文件 |
作用 |
adlist.c ,adlist.h |
双端链表数据结构的实现 |
znalloc.c ,zmalloc.h |
内存管理程序 |
adlist.h
双端链表节点实现
1 2 3 4 5 6 7 8 9
| typedef struct listNode {
struct listNode *prev;
struct listNode *next;
void *value;
} listNode;
|
双端链表迭代器实现
1 2 3 4 5 6 7
| typedef struct listIter {
listNode *next;
int direction;
} listIter;
|
双端链表结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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;
|
宏定义的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#define listLength(l) ((l)->len)
#define listFirst(l) ((l)->head)
#define listLast(l) ((l)->tail)
#define listPrevNode(n) ((n)->prev)
#define listNextNode(n) ((n)->next)
#define listNodeValue(n) ((n)->value)
#define listSetDupMethod(l,m) ((l)->dup = (m))
#define listSetFreeMethod(l,m) ((l)->free = (m))
#define listSetMatchMethod(l,m) ((l)->match = (m))
#define listGetDupMethod(l) ((l)->dup)
#define listGetFree(l) ((l)->free)
#define listGetMatchMethod(l) ((l)->match)
|
宏定义的迭代器描述
1 2 3 4 5
| #define AL_START_HEAD 0
#define AL_START_TAIL 1
|
adlist.c
函数声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
list *listCreate(void);
void listRelease(list *list);
list *listAddNodeHead(list *list, void *value);
list *listAddNodeTail(list *list, void *value);
list *listInsertNode(list *list, listNode *old_node, void *value, int after);
void listDelNode(list *list, listNode *node);
listIter *listGetIterator(list *list, int direction);
listNode *listNext(listIter *iter);
void listReleaseIterator(listIter *iter);
list *listDup(list *orig);
listNode *listSearchKey(list *list, void *key);
listNode *listIndex(list *list, long index);
void listRewind(list *list, listIter *li);
void listRewindTail(list *list, listIter *li);
void listRotate(list *list);
|
adlist.c
头文件
1 2 3
| #include <stdlib.h> #include "adlist.h" #include "zmalloc.h"
|
创建链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
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; }
|
释放链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
void listRelease(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; }
zfree(list); }
|
添加新节点到双端链表的表头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
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 = node; node->prev = node->next = NULL; } else { node->prev = NULL; node->next = list->head; list->head->prev = node; list->head = node; }
list->len++;
return list; }
|
添加新节点到双端链表的表尾
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
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 = node->next = NULL; } else { node->prev = list->tail; node->next = NULL; list->tail->next = node; list->tail = node; }
list->len++;
return list; }
|
插入新节点到某一个节点之前或之后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
list *listInsertNode(list *list, listNode *old_node, void *value, int after) { listNode *node; if ((node = zmalloc(sizeof(*node))) == NULL) return NULL;
node->value = value; if (after) { node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { list->tail = node; } } else { node->next = old_node; node->prev = old_node->prev; 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; }
|
删除给定的某节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
void listDelNode(list *list, listNode *node) { if (node->prev) node->prev->next = node->next; else list->head = node->next;
if (node->next) node->next->prev = node->prev; else list->tail = node->prev;
if (list->free) list->free(node->value);
zfree(node);
list->len--; }
|
为链表添加迭代器 并决定迭代方向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
listIter *listGetIterator(list *list, int direction) { listIter *iter; if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
if (direction == AL_START_HEAD) iter->next = list->head; else iter->next = list->tail;
iter->direction = direction;
return iter; }
|
释放迭代器
1 2 3 4 5 6 7 8
|
void listReleaseIterator(listIter *iter) { zfree(iter); }
|
将迭代器的方向设置为从表头开始
1 2 3 4 5 6 7 8 9 10
|
void listRewind(list *list, listIter *li) { li->next = list->head; li->direction = AL_START_HEAD; }
|
将迭代器的方向设置为从表尾开始
1 2 3 4 5 6 7 8 9 10
|
void listRewindTail(list *list, listIter *li) { li->next = list->tail; li->direction = AL_START_TAIL; }
|
返回迭代器当前所指向的节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
listNode *listNext(listIter *iter) { listNode *current = iter->next;
if (current != NULL) { if (iter->direction == AL_START_HEAD) iter->next = current->next; else iter->next = current->prev; }
return current; }
|
复制链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
list *listDup(list *orig) { list *copy; listIter *iter; listNode *node;
if ((copy = listCreate()) == NULL) return NULL;
copy->dup = orig->dup; copy->free = orig->free; copy->match = orig->match;
iter = listGetIterator(orig, AL_START_HEAD); while((node = listNext(iter)) != NULL) { void *value;
if (copy->dup) { value = copy->dup(node->value); if (value == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } } else value = node->value;
if (listAddNodeTail(copy, value) == NULL) { listRelease(copy); listReleaseIterator(iter); return NULL; } }
listReleaseIterator(iter);
return copy; }
|
查找链表内某一个节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
listNode *listSearchKey(list *list, void *key) { listIter *iter; listNode *node;
iter = listGetIterator(list, AL_START_HEAD); while((node = listNext(iter)) != NULL) { if (list->match) { if (list->match(node->value, key)) { listReleaseIterator(iter); return node; } } else { if (key == node->value) { listReleaseIterator(iter); return node; } } } listReleaseIterator(iter);
return NULL; }
|
输入索引 返回链表中该索引对应的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
listNode *listIndex(list *list, long index) { listNode *n;
if (index < 0) { index = (-index)-1; n = list->tail; while(index-- && n) n = n->prev; } else { n = list->head; while(index-- && n) n = n->next; }
return n; }
|
将表尾节点移动到表头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void listRotate(list *list) { listNode *tail = list->tail;
if (listLength(list) <= 1) return;
list->tail = tail->prev; list->tail->next = NULL;
list->head->prev = tail; tail->prev = NULL; tail->next = list->head; list->head = tail; }
|
End of reading! -- Thanks for your supporting
微信支付
支付宝