[关闭]
@LIUHUAN 2019-03-11T22:32:49.000000Z 字数 8068 阅读 942

redis链表源码解析

redis


数据结构

  1. // 链表节点
  2. typedef struct listNode {
  3. struct listNode *prev; // 前驱
  4. struct listNode *next; // 后继
  5. void *value; // 值
  6. } listNode;
  7. // 链表迭代器
  8. typedef struct listIter {
  9. listNode *next; // 下一个节点
  10. int direction; // 迭代方向
  11. } listIter;
  12. // 迭代方向
  13. #define AL_START_HEAD 0
  14. #define AL_START_TAIL 1
  15. // 链表定义
  16. typedef struct list {
  17. listNode *head; // 链表头指针
  18. listNode *tail; // 链表尾指针
  19. void *(*dup)(void *ptr); // 节点值的复制函数
  20. void (*free)(void *ptr); // 节点值的释放函数
  21. int (*match)(void *ptr, void *key); // 节点值的匹配函数
  22. unsigned long len; // 链表长度
  23. } list;

相关宏定义

  1. /* Functions implemented as macros */
  2. #define listLength(l) ((l)->len) // 获取链表长度
  3. #define listFirst(l) ((l)->head) // 获取链表头部节点
  4. #define listLast(l) ((l)->tail) // 获取链表尾部节点
  5. #define listPrevNode(n) ((n)->prev) // 获取某个节点的前驱节点
  6. #define listNextNode(n) ((n)->next) // 获取某个节点的后继节点
  7. #define listNodeValue(n) ((n)->value) // 获取某个节点的值
  8. #define listSetDupMethod(l,m) ((l)->dup = (m)) // 设置节点复制函数
  9. #define listSetFreeMethod(l,m) ((l)->free = (m)) // 设置节点释放函数
  10. #define listSetMatchMethod(l,m) ((l)->match = (m))// 设置节点匹配函数
  11. #define listGetDupMethod(l) ((l)->dup) // 获取节点复制函数
  12. #define listGetFree(l) ((l)->free) // 获取节点释放函数
  13. #define listGetMatchMethod(l) ((l)->match) // 获取节点匹配函数

功能函数实现

  1. /* Prototypes */
  2. list *listCreate(void);
  3. void listRelease(list *list);
  4. list *listAddNodeHead(list *list, void *value);
  5. list *listAddNodeTail(list *list, void *value);
  6. list *listInsertNode(list *list, listNode *old_node, void *value, int after);
  7. void listDelNode(list *list, listNode *node);
  8. listIter *listGetIterator(list *list, int direction);
  9. listNode *listNext(listIter *iter);
  10. void listReleaseIterator(listIter *iter);
  11. list *listDup(list *orig);
  12. listNode *listSearchKey(list *list, void *key);
  13. listNode *listIndex(list *list, long index);
  14. void listRewind(list *list, listIter *li);
  15. void listRewindTail(list *list, listIter *li);
  16. void listRotate(list *list);

- 具体实现

  1. /* Create a new list. The created list can be freed with
  2. * AlFreeList(), but private value of every node need to be freed
  3. * by the user before to call AlFreeList().
  4. *
  5. * On error, NULL is returned. Otherwise the pointer to the new list. */
  6. // 创建链表
  7. list *listCreate(void)
  8. {
  9. struct list *list;
  10. // 分配内存
  11. if ((list = zmalloc(sizeof(*list))) == NULL)
  12. return NULL;
  13. // 初始化
  14. list->head = list->tail = NULL;
  15. list->len = 0;
  16. list->dup = NULL;
  17. list->free = NULL;
  18. list->match = NULL;
  19. return list;
  20. }
  21. /* Free the whole list.
  22. *
  23. * This function can't fail. */
  24. // 释放链表
  25. void listRelease(list *list)
  26. {
  27. unsigned long len;
  28. listNode *current, *next;
  29. current = list->head;
  30. len = list->len; // 链表长度
  31. while(len--) {
  32. next = current->next;
  33. if (list->free) list->free(current->value); // 如果有值的释放函数调用
  34. zfree(current); // 释放内存
  35. current = next;
  36. }
  37. zfree(list); // 释放整个链表管理节点
  38. }
  39. /* Add a new node to the list, to head, containing the specified 'value'
  40. * pointer as value.
  41. *
  42. * On error, NULL is returned and no operation is performed (i.e. the
  43. * list remains unaltered).
  44. * On success the 'list' pointer you pass to the function is returned. */
  45. // 头部增加节点
  46. list *listAddNodeHead(list *list, void *value)
  47. {
  48. listNode *node;
  49. // 为节点分配内存
  50. if ((node = zmalloc(sizeof(*node))) == NULL)
  51. return NULL;
  52. node->value = value;
  53. if (list->len == 0) { // 添加前链表为空
  54. list->head = list->tail = node;
  55. node->prev = node->next = NULL;
  56. } else {// 已经存在头节点
  57. node->prev = NULL;
  58. node->next = list->head;
  59. list->head->prev = node;
  60. list->head = node;
  61. }
  62. list->len++; // 增加长度
  63. return list;
  64. }
  65. /* Add a new node to the list, to tail, containing the specified 'value'
  66. * pointer as value.
  67. *
  68. * On error, NULL is returned and no operation is performed (i.e. the
  69. * list remains unaltered).
  70. * On success the 'list' pointer you pass to the function is returned. */
  71. // 在链表尾部添加节点
  72. list *listAddNodeTail(list *list, void *value)
  73. {
  74. listNode *node;
  75. if ((node = zmalloc(sizeof(*node))) == NULL)
  76. return NULL;
  77. node->value = value;
  78. if (list->len == 0) { // 链表为空
  79. list->head = list->tail = node;
  80. node->prev = node->next = NULL;
  81. } else { // 链表非空
  82. node->prev = list->tail;
  83. node->next = NULL;
  84. list->tail->next = node;
  85. list->tail = node;
  86. }
  87. list->len++; // 增加长度
  88. return list;
  89. }
  90. // 在某个节点前(后)插入节点
  91. list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
  92. listNode *node;
  93. // 创建插入节点
  94. if ((node = zmalloc(sizeof(*node))) == NULL)
  95. return NULL;
  96. node->value = value;
  97. if (after) {// 节点之后插入
  98. node->prev = old_node;
  99. node->next = old_node->next;
  100. if (list->tail == old_node) { // 在尾部节点之后插入节点
  101. list->tail = node;
  102. }
  103. } else {// 节点之前插入
  104. node->next = old_node;
  105. node->prev = old_node->prev;
  106. if (list->head == old_node) {// 在头部部节点之前插入节点
  107. list->head = node;
  108. }
  109. }
  110. if (node->prev != NULL) { // 插入节点后,非头节点
  111. node->prev->next = node;
  112. }
  113. if (node->next != NULL) { // 插入节点后,非尾部节点
  114. node->next->prev = node;
  115. }
  116. list->len++;
  117. return list;
  118. }
  119. /* Remove the specified node from the specified list.
  120. * It's up to the caller to free the private value of the node.
  121. *
  122. * This function can't fail. */
  123. // 删除某个节点
  124. void listDelNode(list *list, listNode *node)
  125. {
  126. if (node->prev) // 待删除节点有前驱节点
  127. node->prev->next = node->next;
  128. else // 删除头节点
  129. list->head = node->next;
  130. if (node->next)// 待删除节点有后继节点
  131. node->next->prev = node->prev;
  132. else // 删除尾节点
  133. list->tail = node->prev;
  134. if (list->free) list->free(node->value); // 释放值函数
  135. zfree(node);
  136. list->len--; // 链表长度减1
  137. }
  138. /* Returns a list iterator 'iter'. After the initialization every
  139. * call to listNext() will return the next element of the list.
  140. *
  141. * This function can't fail. */
  142. // 获取链表某个方向上的迭代器
  143. listIter *listGetIterator(list *list, int direction)
  144. {
  145. listIter *iter;
  146. if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
  147. if (direction == AL_START_HEAD)// 头部开始的迭代器
  148. iter->next = list->head;
  149. else// 尾部开始的迭代器
  150. iter->next = list->tail;
  151. iter->direction = direction; // 迭代器方向
  152. return iter;
  153. }
  154. /* Release the iterator memory */
  155. // 释放迭代器内存
  156. void listReleaseIterator(listIter *iter) {
  157. zfree(iter);
  158. }
  159. /* Create an iterator in the list private iterator structure */
  160. // 关联迭代器和链表,从头部开始迭代
  161. void listRewind(list *list, listIter *li) {
  162. li->next = list->head;
  163. li->direction = AL_START_HEAD;
  164. }
  165. // 关联迭代器和链表,从尾部开始迭代
  166. void listRewindTail(list *list, listIter *li) {
  167. li->next = list->tail;
  168. li->direction = AL_START_TAIL;
  169. }
  170. /* Return the next element of an iterator.
  171. * It's valid to remove the currently returned element using
  172. * listDelNode(), but not to remove other elements.
  173. *
  174. * The function returns a pointer to the next element of the list,
  175. * or NULL if there are no more elements, so the classical usage patter
  176. * is:
  177. *
  178. * iter = listGetIterator(list,<direction>);
  179. * while ((node = listNext(iter)) != NULL) {
  180. * doSomethingWith(listNodeValue(node));
  181. * }
  182. *
  183. * */
  184. // 获取迭代器的下一个元素
  185. listNode *listNext(listIter *iter)
  186. {
  187. listNode *current = iter->next;
  188. if (current != NULL) {
  189. if (iter->direction == AL_START_HEAD) // 后向
  190. iter->next = current->next;
  191. else// 前向
  192. iter->next = current->prev;
  193. }
  194. return current;
  195. }
  196. /* Duplicate the whole list. On out of memory NULL is returned.
  197. * On success a copy of the original list is returned.
  198. *
  199. * The 'Dup' method set with listSetDupMethod() function is used
  200. * to copy the node value. Otherwise the same pointer value of
  201. * the original node is used as value of the copied node.
  202. *
  203. * The original list both on success or error is never modified. */
  204. // 复制链表
  205. list *listDup(list *orig)
  206. {
  207. list *copy;
  208. listIter *iter;
  209. listNode *node;
  210. if ((copy = listCreate()) == NULL)
  211. return NULL;
  212. // 函数复制
  213. copy->dup = orig->dup;
  214. copy->free = orig->free;
  215. copy->match = orig->match;
  216. iter = listGetIterator(orig, AL_START_HEAD);
  217. while((node = listNext(iter)) != NULL) {
  218. void *value;
  219. if (copy->dup) {
  220. value = copy->dup(node->value);
  221. if (value == NULL) { // 复制节点值失败
  222. listRelease(copy);
  223. listReleaseIterator(iter);
  224. return NULL;
  225. }
  226. } else // 没有复制函数,那么复制后的链表指向复制前的链表
  227. value = node->value;
  228. if (listAddNodeTail(copy, value) == NULL) { // 添加节点到尾部
  229. listRelease(copy);
  230. listReleaseIterator(iter);
  231. return NULL;
  232. }
  233. }
  234. listReleaseIterator(iter);
  235. return copy;
  236. }
  237. /* Search the list for a node matching a given key.
  238. * The match is performed using the 'match' method
  239. * set with listSetMatchMethod(). If no 'match' method
  240. * is set, the 'value' pointer of every node is directly
  241. * compared with the 'key' pointer.
  242. *
  243. * On success the first matching node pointer is returned
  244. * (search starts from head). If no matching node exists
  245. * NULL is returned. */
  246. // 查找链表
  247. listNode *listSearchKey(list *list, void *key)
  248. {
  249. listIter *iter;
  250. listNode *node;
  251. iter = listGetIterator(list, AL_START_HEAD);
  252. while((node = listNext(iter)) != NULL) {
  253. if (list->match) {// 值匹配函数
  254. if (list->match(node->value, key)) {
  255. listReleaseIterator(iter);
  256. return node;
  257. }
  258. } else { // 直接使用==
  259. if (key == node->value) {
  260. listReleaseIterator(iter);
  261. return node;
  262. }
  263. }
  264. }
  265. listReleaseIterator(iter);
  266. return NULL;
  267. }
  268. /* Return the element at the specified zero-based index
  269. * where 0 is the head, 1 is the element next to head
  270. * and so on. Negative integers are used in order to count
  271. * from the tail, -1 is the last element, -2 the penultimate
  272. * and so on. If the index is out of range NULL is returned. */
  273. // 获取索引上的链表节点
  274. listNode *listIndex(list *list, long index) {
  275. listNode *n;
  276. if (index < 0) { // 索引为负数,从尾部开始查找
  277. index = (-index)-1;
  278. n = list->tail;
  279. while(index-- && n) n = n->prev;
  280. } else { // 索引非负,从头部开始查找
  281. n = list->head;
  282. while(index-- && n) n = n->next;
  283. }
  284. return n;
  285. }
  286. /* Rotate the list removing the tail node and inserting it to the head. */
  287. // 旋转链表,把尾部节点删除,插入到头部
  288. void listRotate(list *list) {
  289. listNode *tail = list->tail;
  290. if (listLength(list) <= 1) return;
  291. /* Detach current tail */
  292. list->tail = tail->prev;
  293. list->tail->next = NULL;
  294. /* Move it as head */
  295. list->head->prev = tail;
  296. tail->prev = NULL;
  297. tail->next = list->head;
  298. list->head = tail;
  299. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注