Linux 內(nèi)核數(shù)據(jù)結(jié)構(gòu)——鏈表

系統(tǒng)內(nèi)核鏈表linux/list.h

#ifndef __LIST_H
#define __LIST_H

/* This file is from Linux Kernel (include/linux/list.h)
 * * and modified by simply removing hardware prefetching of list items.
 * * Here by copyright, credits attributed to wherever they belong.
 * * Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
 * */

/*
 * * Simple doubly linked list implementation.
 * *
 * * Some of the internal functions (“__xxx”) are useful when
 * * manipulating whole lists rather than single entries, as
 * * sometimes we already know the next/prev entries and we can
 * * generate better code by using them directly rather than
 * * using the generic single-entry routines.
 * */

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

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

static inline void INIT_LIST_HEAD(struct list_head *list)
{
        list->next = list;
            list->prev = list;
}
/*
 * * Insert a new entry between two known consecutive entries.
 * *
 * * This is only for internal list manipulation where we know
 * * the prev/next entries already!
 * */
static inline void __list_add(struct list_head *new,
                                      struct list_head *prev,
                                                                    struct list_head *next)
{
        next->prev = new;
            new->next = next;
                new->prev = prev;
                    prev->next = new;
}

/**
 * * list_add – add a new entry
 * * @new: new entry to be added
 * * @head: list head to add it after
 * *
 * * Insert a new entry after the specified head.
 * * This is good for implementing stacks.
 * */
static inline void list_add(struct list_head *new, struct list_head *head)
{
        __list_add(new, head, head->next);
}

/**
 * * list_add_tail – add a new entry
 * * @new: new entry to be added
 * * @head: list head to add it before
 * *
 * * Insert a new entry before the specified head.
 * * This is useful for implementing queues.
 * */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
        __list_add(new, head->prev, head);
}

/*
 * * Delete a list entry by making the prev/next entries
 * * point to each other.
 * *
 * * This is only for internal list manipulation where we know
 * * the prev/next entries already!
 * */
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
        next->prev = prev;
            prev->next = next;
}

/**
 * * list_del – deletes entry from list.
 * * @entry: the element to delete from the list.
 * * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
 * */
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
            entry->next = (void *) 0;
                entry->prev = (void *) 0;
}

/**
 * * list_del_init – deletes entry from list and reinitialize it.
 * * @entry: the element to delete from the list.
 * */
static inline void list_del_init(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
            INIT_LIST_HEAD(entry);
}

/**
 * * list_move – delete from one list and add as another’s head
 * * @list: the entry to move
 * * @head: the head that will precede our entry
 * */
static inline void list_move(struct list_head *list, struct list_head *head)
{
        __list_del(list->prev, list->next);
            list_add(list, head);
}

/**
 * * list_move_tail – delete from one list and add as another’s tail
 * * @list: the entry to move
 * * @head: the head that will follow our entry
 * */
static inline void list_move_tail(struct list_head *list,
        struct list_head *head)
{
        __list_del(list->prev, list->next);
            list_add_tail(list, head);
}

/**
 * * list_empty – tests whether a list is empty
 * * @head: the list to test.
 * */
static inline int list_empty(struct list_head *head)
{
        return head->next == head;
}

static inline void __list_splice(struct list_head *list,
        struct list_head *head)
{
        struct list_head *first = list->next;
            struct list_head *last = list->prev;
                struct list_head *at = head->next;

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

                            last->next = at;
                                at->prev = last;
}

/**
 * * list_splice – join two lists
 * * @list: the new list to add.
 * * @head: the place to add it in the first list.
 * */
static inline void list_splice(struct list_head *list, struct list_head *head)
{
        if (!list_empty(list))
                    __list_splice(list, head);
}

/**
 * * list_splice_init – join two lists and reinitialise the emptied list.
 * * @list: the new list to add.
 * * @head: the place to add it in the first list.
 * *
 * * The list at @list is reinitialised
 * */
static inline void list_splice_init(struct list_head *list,
                                            struct list_head *head)
{
        if (!list_empty(list)) {
                    __list_splice(list, head);
                            INIT_LIST_HEAD(list);
                                }
}

/**
 * * list_entry – get the struct for this entry
 * * @ptr:    the &struct list_head pointer.
 * * @type:    the type of the struct this is embedded in.
 * * @member:    the name of the list_struct within the struct.
 * */
#define list_entry(ptr, type, member) \
        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

/**
 * * list_for_each    -    iterate over a list
 * * @pos:    the &struct list_head to use as a loop counter.
 * * @head:    the head for your list.
 * */
#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); \
                        pos = pos->next)
/**
 * * list_for_each_prev    -    iterate over a list backwards
 * * @pos:    the &struct list_head to use as a loop counter.
 * * @head:    the head for your list.
 * */
#define list_for_each_prev(pos, head) \
        for (pos = (head)->prev; pos != (head); \
                        pos = pos->prev)

/**
 * * list_for_each_safe    -    iterate over a list safe against removal of list entry
 * * @pos:    the &struct list_head to use as a loop counter.
 * * @n:        another &struct list_head to use as temporary storage
 * * @head:    the head for your list.
 * */
#define list_for_each_safe(pos, n, head) \
        for (pos = (head)->next, n = pos->next; pos != (head);\
                        pos = n, n = pos->next)


/**
 * * list_for_each_entry    -    iterate over list of given type
 * * @pos:    the type * to use as a loop counter.
 * * @head:    the head for your list.
 * * @member:    the name of the list_struct within the struct.
 * */
#define list_for_each_entry(pos, head, member)                \
        for (pos = list_entry((head)->next,typeof(*pos), member); \
                        &pos->member != (head);                                  \
                            pos = list_entry(pos->member.next, typeof(*pos), member))

/**
 * * list_for_each_entry_safe – iterate over list of given type safe against removal of list entry
 * * @pos:    the type * to use as a loop counter.
 * * @n:        another type * to use as temporary storage
 * * @head:    the head for your list.
 * * @member:    the name of the list_struct within the struct.
 * */
#define list_for_each_entry_safe(pos, n, head, member)            \
    for (pos = list_entry((head)->next, typeof(*pos), member),    \
                n = list_entry(pos->member.next, typeof(*pos), member);    \
                    &pos->member != (head);                     \
                        pos = n, n = list_entry(n->member.next, typeof(*n), member))

#endif

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末部蛇,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子凶朗,更是在濱河造成了極大的恐慌传透,老刑警劉巖沟绪,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡癣朗,警方通過查閱死者的電腦和手機蜗元,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門或渤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人奕扣,你說我怎么就攤上這事薪鹦。” “怎么了惯豆?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵池磁,是天一觀的道長。 經(jīng)常有香客問我楷兽,道長地熄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任芯杀,我火速辦了婚禮端考,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘揭厚。我一直安慰自己却特,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布筛圆。 她就那樣靜靜地躺著核偿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪顽染。 梳的紋絲不亂的頭發(fā)上漾岳,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天轰绵,我揣著相機與錄音,去河邊找鬼尼荆。 笑死左腔,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捅儒。 我是一名探鬼主播液样,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼巧还!你這毒婦竟也來了鞭莽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤麸祷,失蹤者是張志新(化名)和其女友劉穎澎怒,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阶牍,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡喷面,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了走孽。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惧辈。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖磕瓷,靈堂內(nèi)的尸體忽然破棺而出盒齿,到底是詐尸還是另有隱情,我是刑警寧澤困食,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布县昂,位于F島的核電站,受9級特大地震影響陷舅,放射性物質(zhì)發(fā)生泄漏倒彰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一莱睁、第九天 我趴在偏房一處隱蔽的房頂上張望待讳。 院中可真熱鬧,春花似錦仰剿、人聲如沸创淡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽琳彩。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間露乏,已是汗流浹背碧浊。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瘟仿,地道東北人箱锐。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像劳较,于是被迫代替她去往敵國和親驹止。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容