2012-10-25 23 views
1

list_empty()功能在./include/linux/list.h定義,它的定義是list_empty功能內核鏈表

static inline int list_empty(const struct list_head *head) 
{ 
    return head->next == head; 
} 

list_head數據結構定義爲

struct list_head { 
    struct list_head *next, *prev; 
}; 

我不明白的是在內核檢查爲什麼這個實施對於head->next == head而不是對於head->next == NULL & & head->prev == NULL

回答

7

該列表是循環的,頭部本身充當虛擬節點。循環列表在插入和刪除過程中所需的比較次數方面略有優勢。由於沒有空指針,所以沒有那麼多特殊情況需要查找。我沒有源代碼在這裏,但如果你選擇了它,你會發現,初始化列表與做事情,如:

head->next = head->prev = head; 

插入將是:

void insert_after(struct list_head *node, struct list_head *after) 
{ 
    node->next = after->next; 
    node->prev = after; 
    after->next->prev = node; 
    after->next = node; 
} 

看!沒有,如果聲明!

0

因爲head->next不是NULL當列表爲空時,它指向head而不是。