redis源码解析(二)—— adlist

这是对adlist结构体的定义,与其他代码关联不大。

#ifndef __ADLIST_H__
#define __ADLIST_H__

/* Node, List, and Iterator are the only data structures used currently. */

/*节点*/
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);//根据key和值,匹配一个节点
    unsigned long len;//列表长度
} list;

/* Functions implemented as macros */
/* 常用操作的宏定义 */
#define listLength(l) ((l)->len)//得到list长度
#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)//获取匹配节点函数

/* Prototypes */
list *listCreate(void);//生成list
void listRelease(list *list);//释放整个list
void listEmpty(list *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);//根据key得到相应节点
listNode *listIndex(list *list, long index);//得到给定下标的节点
void listRewind(list *list, listIter *li);//重置迭代器,从头开始
void listRewindTail(list *list, listIter *li);//重置迭代器,从尾开始
void listRotate(list *list);//反转列表,把尾节点插入到头部
void listJoin(list *l, list *o);//插入一个列表

/* Directions for iterators */
/*迭代器的方向*/
#define AL_START_HEAD 0//从头向尾
#define AL_START_TAIL 1//从尾向头

#endif /* __ADLIST_H__ */

#include <stdlib.h>
#include "adlist.h"
#include "zmalloc.h"

/* Create a new list. The created list can be freed with
 * AlFreeList(), but private value of every node need to be freed
 * by the user before to call AlFreeList().
 *
 * On error, NULL is returned. Otherwise the pointer to the new list. */

 /* 创建新列表。
 可以使用 AlFreeList () 释放创建的列表, 但在调用 AlFreeList () 之前, 用户需要释放每个节点的私有值。
 出现错误时, 将返回 NULL。否则, 指向新列表的指针。
 */
list *listCreate(void)
{
    struct list *list;

    //zmalloc 可以理解为malloc,用于申请空间,
    //redis对malloc做了封装,产生了zmalloc,具体信息在zmalloc.c和zmalloc.h中。
    if ((list = zmalloc(sizeof(*list))) == NULL)
        return NULL;//申请空间失败,则返回null

    //初始化list
    list->head = list->tail = NULL;
    list->len = 0;
    list->dup = NULL;
    list->free = NULL;
    list->match = NULL;
    return list;
}

/* Remove all the elements from the list without destroying the list itself. */
/*删除所有元素,但不销毁list*/
void 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;//设置头结点,尾节点为null
    list->len = 0;//设置长度为0
}

/* Free the whole list.
 * This function can't fail. */

 /*释放整个列表,此操作不会失败*/
void listRelease(list *list)
{
    listEmpty(list);//清空list
    zfree(list);//释放list
}

/* 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的头部插入一个新节点,参数value作为其中的值
 成功返回list指针,失败返回null,且不执行任何操作*/
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;
}

/* Add a new node to the list, to tail, 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 *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;
}

/*在指定位置插入
list:指定list;
value:新节点的值
old_node:插入在节点附近
after:为1时,插入到旧节点的后面,否则插入到旧节点的前面*/
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;
}

/* Remove the specified node from the specified list.
 * It's up to the caller to free the private value of the node.
 *
 * This function can't fail. */
/*删除指定列表中的指定节点,并释放节点空间,此操作不会失败*/
void listDelNode(list *list, listNode *node)
{
    if (node->prev)//前一个节点不是null
        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--;
}

/* Returns a list iterator 'iter'. After the initialization every
 * call to listNext() will return the next element of the list.
 *
 * This function can't fail. */

 /*
 返回列表迭代器 "iter"。初始化后, 对 listNext () 的每次调用都将返回列表中的下一个元素。
 此操作不会失败。*/
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;
}

/* Release the iterator memory */
/*释放迭代器*/
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}

/* Create an iterator in the list private iterator structure */
/*重置迭代器:从头开始*/
void listRewind(list *list, listIter *li) {
    li->next = list->head;
    li->direction = AL_START_HEAD;
}

/*重置迭代器:从尾开始*/
void listRewindTail(list *list, listIter *li) {
    li->next = list->tail;
    li->direction = AL_START_TAIL;
}

/* Return the next element of an iterator.
 * It's valid to remove the currently returned element using
 * listDelNode(), but not to remove other elements.
 *
 * The function returns a pointer to the next element of the list,
 * or NULL if there are no more elements, so the classical usage patter
 * is:
 *
 * iter = listGetIterator(list,<direction>);
 * while ((node = listNext(iter)) != NULL) {
 *     doSomethingWith(listNodeValue(node));
 * }
 *
 * */

 /*
 返回迭代器的下一个元素。
 使用 listDelNode () 删除当前返回的元素是有效的, 但不能删除其他元素。
 该函数返回指向列表的下一个元素的指针, 如果没有其他元素, 则返回 NULL。
 经典的应用场景有:*/
listNode *listNext(listIter *iter)
{
    listNode *current = iter->next;
    //current是迭代器中的下一个元素,则返回的是迭代器的下一个元素的下一个元素

    if (current != NULL) {
        //根据迭代方向得到下一个元素
        if (iter->direction == AL_START_HEAD)
            iter->next = current->next;
        else
            iter->next = current->prev;
    }
    return current;
}

/* Duplicate the whole list. On out of memory NULL is returned.
 * On success a copy of the original list is returned.
 *
 * The 'Dup' method set with listSetDupMethod() function is used
 * to copy the node value. Otherwise the same pointer value of
 * the original node is used as value of the copied node.
 *
 * The original list both on success or error is never modified. */

 /*
 复制整个列表。在内存不足时返回 null。
 成功时, 将返回原始列表的副本。
 使用 Listsetdupfal () 函数设置的 "Dup" 方法复制节点值。
 否则, 原始节点的相同的指针值将被用作复制节点的值。原始列表永远不会被修改无论成功还是失败。*/
list *listDup(list *orig)
{
    list *copy;
    listIter iter;
    listNode *node;

    //生成一个list
    if ((copy = listCreate()) == NULL)
        return NULL;

    //设置list信息
    copy->dup = orig->dup;
    copy->free = orig->free;
    copy->match = orig->match;
    listRewind(orig, &iter);

    //遍历节点,复制
    while((node = listNext(&iter)) != NULL) {
        void *value;

        //如果有dup函数
        if (copy->dup) {
            value = copy->dup(node->value);
            //值为null,释放copyList
            if (value == NULL) {
                listRelease(copy);
                return NULL;
            }
        } else
            value = node->value;
        //添加失败,释放copyList
        if (listAddNodeTail(copy, value) == NULL) {
            listRelease(copy);
            return NULL;
        }
    }
    //全部复制成功
    return copy;
}

/* Search the list for a node matching a given key.
 * The match is performed using the 'match' method
 * set with listSetMatchMethod(). If no 'match' method
 * is set, the 'value' pointer of every node is directly
 * compared with the 'key' pointer.
 *
 * On success the first matching node pointer is returned
 * (search starts from head). If no matching node exists
 * NULL is returned. */

 /*
 在列表中搜索与给定键匹配的节点。
 匹配是使用 Listsetmatchmetf () 设置的 "匹配" 方法。
 如果未设置 "匹配" 方法, 则拿每个节点的value和key比较(搜索从头部开始)。
 成功时返回第一个匹配的节点指针。如果不存在匹配节点,返回 null。*/
listNode *listSearchKey(list *list, void *key)
{
    listIter iter;
    listNode *node;

    //遍历全部节点
    listRewind(list, &iter);
    while((node = listNext(&iter)) != NULL) {
        //有匹配函数
        if (list->match) {
            if (list->match(node->value, key)) {
                return node;
            }
        } else {
            //比较key和value
            if (key == node->value) {
                return node;
            }
        }
    }
    return NULL;
}

/* Return the element at the specified zero-based index
 * where 0 is the head, 1 is the element next to head
 * and so on. Negative integers are used in order to count
 * from the tail, -1 is the last element, -2 the penultimate
 * and so on. If the index is out of range NULL is returned. */

 /*
 返回指定的下标的元素:0是头节点, 1 是下一个节点, 依此类推。
 负整数用于从尾部开始计数,-1 是尾节点,-2 倒数第二个节点, 依此类推。
 如果索引超出范围, 则返回 NULL。*/
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;
}

/* Rotate the list removing the tail node and inserting it to the head. */
/*旋转列表, 把尾节点移到头部。*/
void listRotate(list *list) {
    //得到尾节点
    listNode *tail = list->tail;

    if (listLength(list) <= 1) return;

    /* Detach current tail */
    //设置倒数第二个节点为尾节点
    list->tail = tail->prev;
    list->tail->next = NULL;
    /* Move it as head */
    //把尾节点设为头结点
    list->head->prev = tail;
    tail->prev = NULL;
    tail->next = list->head;
    list->head = tail;
}

/* Add all the elements of the list 'o' at the end of the
 * list 'l'. The list 'other' remains empty but otherwise valid. */
 /*把列表o的所有元素插入到列表l的尾部。列表o设为空, 但在其他方面有效。 */
void listJoin(list *l, list *o) {
    if (o->head)
        o->head->prev = l->tail;

    if (l->tail)
        l->tail->next = o->head;
    else//l->tail为null,l列表为空
        l->head = o->head;

    if (o->tail) l->tail = o->tail;
    l->len += o->len;

    /* Setup other as an empty list. */
    //把o设为空,其他的都不动
    o->head = o->tail = NULL;
    o->len = 0;
}

体会:这一部分比较简单,定义了adlist结构体和一些基本操作。跟链表差不多,不难,基本上看了就都能懂。
1.到处都是指针,很方便;
2.代码很干净,看了很舒服;
3.有很多注释;

猜你喜欢

转载自blog.csdn.net/A_BCDEF_/article/details/89468736
今日推荐