概述:在linux 2.4内核中,于平台无关头文件/include/linux主要用于实现linux内核中的双线链表,对于很多内核中的功能都起到了很好的支撑作用,而且只有100多行,本文主要为你讲解这些函数的实现。
先浏览代码:
1 #ifndef _LINUX_LIST_H 2 #define _LINUX_LIST_H 3 4 #ifdef __KERNEL__ 5 6 /* 7 * Simple doubly linked list implementation. 8 * 9 * Some of the internal functions ("__xxx") are useful when 10 * manipulating whole lists rather than single entries, as 11 * sometimes we already know the next/prev entries and we can 12 * generate better code by using them directly rather than 13 * using the generic single-entry routines. 14 */ 15 16 struct list_head { 17 struct list_head *next, *prev; 18 };//双向链表结构体 19 20 #define LIST_HEAD_INIT(name) { &(name), &(name) } 21 22 #define LIST_HEAD(name) \ 23 struct list_head name = LIST_HEAD_INIT(name) 24 25 #define INIT_LIST_HEAD(ptr) do { \ 26 (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 27 } while (0)//初始化,让它指向自己,这里使用dowhile(0)来避免分号带来的问题 28 29 /* 30 * Insert a new entry between two known consecutive entries. 31 * 32 * This is only for internal list manipulation where we know 33 * the prev/next entries already! 34 */ 35 static __inline__ void __list_add(struct list_head * new, 36 struct list_head * prev, 37 struct list_head * next) 38 { 39 next->prev = new; 40 new->next = next; 41 new->prev = prev; 42 prev->next = new; 43 } 44 45 /** 46 * list_add - add a new entry 47 * @new: new entry to be added 48 * @head: list head to add it after 49 * 50 * Insert a new entry after the specified head. 51 * This is good for implementing stacks. 52 */ 53 static __inline__ void list_add(struct list_head *new, struct list_head *head) 54 { 55 __list_add(new, head, head->next); 56 } 57 58 /** 59 * list_add_tail - add a new entry 60 * @new: new entry to be added 61 * @head: list head to add it before 62 * 63 * Insert a new entry before the specified head. 64 * This is useful for implementing queues. 65 */ 66 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head) 67 { 68 __list_add(new, head->prev, head); 69 } 70 71 /* 72 * Delete a list entry by making the prev/next entries 73 * point to each other. 74 * 75 * This is only for internal list manipulation where we know 76 * the prev/next entries already! 77 */ 78 static __inline__ void __list_del(struct list_head * prev, 79 struct list_head * next) 80 { 81 next->prev = prev; 82 prev->next = next;83 } 84 85 /** 86 * list_del - deletes entry from list. 87 * @entry: the element to delete from the list. 88 * Note: list_empty on entry does not return true after this, the entry is in an undefined state. 89 */ 90 static __inline__ void list_del(struct list_head *entry) 91 { 92 __list_del(entry->prev, entry->next); 93 } 94 95 /** 96 * list_del_init - deletes entry from list and reinitialize it. 97 * @entry: the element to delete from the list. 98 */ 99 static __inline__ void list_del_init(struct list_head *entry)100 {101 __list_del(entry->prev, entry->next);102 INIT_LIST_HEAD(entry);103 }104 105 /**106 * list_empty - tests whether a list is empty107 * @head: the list to test.108 */109 static __inline__ int list_empty(struct list_head *head)110 {111 return head->next == head;112 }113 114 /**115 * list_splice - join two lists116 * @list: the new list to add.117 * @head: the place to add it in the first list.118 */119 static __inline__ void list_splice(struct list_head *list, struct list_head *head)120 {121 struct list_head *first = list->next;122 123 if (first != list) {124 struct list_head *last = list->prev;125 struct list_head *at = head->next;126 127 first->prev = head;128 head->next = first;129 130 last->next = at;131 at->prev = last;132 }133 }134 135 /**136 * list_entry - get the struct for this entry137 * @ptr: the &struct list_head pointer.138 * @type: the type of the struct this is embedded in.139 * @member: the name of the list_struct within the struct.140 */141 #define list_entry(ptr, type, member) \142 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))143 144 /**145 * list_for_each - iterate over a list146 * @pos: the &struct list_head to use as a loop counter.147 * @head: the head for your list.148 */149 #define list_for_each(pos, head) \150 for (pos = (head)->next; pos != (head); pos = pos->next)151 152 #endif /* __KERNEL__ */153 154 #endif
代码太简单,没有什么要分析的。
本文来源:谁不小心的CSDN博客linux 2.4内核中双向链表的实现/include/linux
痛苦留给的一切,请细加回味!苦难一经过去,苦难就变为甘美。