当前位置: 首页 > news >正文

学习/linux/list.h_双链表实现

 linux-3.5/include/linux/list.h

使用只含指针域的双向循环链表进行链表的操作。

下面是我选取部分list.h中代码:

  1 #ifndef _LINUX_LIST_H
  2 #define _LINUX_LIST_H
  3 
  4 struct list_head {
  5     struct list_head *next, *prev;
  6 };
  7 /*
  8  * Simple doubly linked list implementation.
  9  *
 10  * Some of the internal functions ("__xxx") are useful when
 11  * manipulating whole lists rather than single entries, as
 12  * sometimes we already know the next/prev entries and we can
 13  * generate better code by using them directly rather than
 14  * using the generic single-entry routines.
 15  */
 16 
 17 #define LIST_HEAD_INIT(name) { &(name), &(name) }
 18 
 19 #define LIST_HEAD(name) \
 20     struct list_head name = LIST_HEAD_INIT(name)
 21 
 22 static inline void INIT_LIST_HEAD(struct list_head *list)
 23 {
 24     list->next = list;
 25     list->prev = list;
 26 }
 27 
 28 /*
 29  * Insert a new entry between two known consecutive entries.
 30  *
 31  * This is only for internal list manipulation where we know
 32  * the prev/next entries already!
 33  */
 34 #ifndef CONFIG_DEBUG_LIST
 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 #else
 45 extern void __list_add(struct list_head *new,
 46                   struct list_head *prev,
 47                   struct list_head *next);
 48 #endif
 49 
 50 /**
 51  * list_add - add a new entry
 52  * @new: new entry to be added
 53  * @head: list head to add it after
 54  *
 55  * Insert a new entry after the specified head.
 56  * This is good for implementing stacks.
 57  */
 58 static inline void list_add(struct list_head *new, struct list_head *head)
 59 {
 60     __list_add(new, head, head->next);
 61 }
 62 
 63 
 64 /**
 65  * list_add_tail - add a new entry
 66  * @new: new entry to be added
 67  * @head: list head to add it before
 68  *
 69  * Insert a new entry before the specified head.
 70  * This is useful for implementing queues.
 71  */
 72 static inline void list_add_tail(struct list_head *new, struct list_head *head)
 73 {
 74     __list_add(new, head->prev, head);
 75 }
 76 
 77 /*
 78  * Delete a list entry by making the prev/next entries
 79  * point to each other.
 80  *
 81  * This is only for internal list manipulation where we know
 82  * the prev/next entries already!
 83  */
 84 static inline void __list_del(struct list_head * prev, struct list_head * next)
 85 {
 86     next->prev = prev;
 87     prev->next = next;
 88 }
 89 
 90 /**
 91  * list_del - deletes entry from list.
 92  * @entry: the element to delete from the list.
 93  * Note: list_empty() on entry does not return true after this, the entry is
 94  * in an undefined state.
 95  */
 96 #ifndef CONFIG_DEBUG_LIST
 97 static inline void __list_del_entry(struct list_head *entry)
 98 {
 99     __list_del(entry->prev, entry->next);
100 }
101 #endif
102 
103 /**
104  * list_replace - replace old entry by new one
105  * @old : the element to be replaced
106  * @new : the new element to insert
107  *
108  * If @old was empty, it will be overwritten.
109  */
110 static inline void list_replace(struct list_head *old,
111                 struct list_head *new)
112 {
113     new->next = old->next;
114     new->next->prev = new;
115     new->prev = old->prev;
116     new->prev->next = new;
117 }
118 
119 static inline void list_replace_init(struct list_head *old,
120                     struct list_head *new)
121 {
122     list_replace(old, new);
123     INIT_LIST_HEAD(old);
124 }
125 
126 /**
127  * list_del_init - deletes entry from list and reinitialize it.
128  * @entry: the element to delete from the list.
129  */
130 static inline void list_del_init(struct list_head *entry)
131 {
132     __list_del_entry(entry);
133     INIT_LIST_HEAD(entry);
134 }
135 
136 /**
137  * list_move - delete from one list and add as another's head
138  * @list: the entry to move
139  * @head: the head that will precede our entry
140  */
141 static inline void list_move(struct list_head *list, struct list_head *head)
142 {
143     __list_del_entry(list);
144     list_add(list, head);
145 }
146 
147 /**
148  * list_move_tail - delete from one list and add as another's tail
149  * @list: the entry to move
150  * @head: the head that will follow our entry
151  */
152 static inline void list_move_tail(struct list_head *list,
153                   struct list_head *head)
154 {
155     __list_del_entry(list);
156     list_add_tail(list, head);
157 }
158 
159 /**
160  * list_is_last - tests whether @list is the last entry in list @head
161  * @list: the entry to test
162  * @head: the head of the list
163  */
164 static inline int list_is_last(const struct list_head *list,
165                 const struct list_head *head)
166 {
167     return list->next == head;
168 }
169 
170 /**
171  * list_empty - tests whether a list is empty
172  * @head: the list to test.
173  */
174 static inline int list_empty(const struct list_head *head)
175 {
176     return head->next == head;
177 }
178 
179 /**
180  * list_empty_careful - tests whether a list is empty and not being modified
181  * @head: the list to test
182  *
183  * Description:
184  * tests whether a list is empty _and_ checks that no other CPU might be
185  * in the process of modifying either member (next or prev)
186  *
187  * NOTE: using list_empty_careful() without synchronization
188  * can only be safe if the only activity that can happen
189  * to the list entry is list_del_init(). Eg. it cannot be used
190  * if another CPU could re-list_add() it.
191  */
192 static inline int list_empty_careful(const struct list_head *head)
193 {
194     struct list_head *next = head->next;
195     return (next == head) && (next == head->prev);
196 }
197 
198 /**
199  * list_rotate_left - rotate the list to the left
200  * @head: the head of the list
201  */
202 static inline void list_rotate_left(struct list_head *head)
203 {
204     struct list_head *first;
205 
206     if (!list_empty(head)) {
207         first = head->next;
208         list_move_tail(first, head);
209     }
210 }
211 
212 /**
213  * list_is_singular - tests whether a list has just one entry.
214  * @head: the list to test.
215  */
216 static inline int list_is_singular(const struct list_head *head)
217 {
218     return !list_empty(head) && (head->next == head->prev);
219 }
220 
221 static inline void __list_cut_position(struct list_head *list,
222         struct list_head *head, struct list_head *entry)
223 {
224     struct list_head *new_first = entry->next;
225     list->next = head->next;
226     list->next->prev = list;
227     list->prev = entry;
228     entry->next = list;
229     head->next = new_first;
230     new_first->prev = head;
231 }
232 
233 /**
234  * list_cut_position - cut a list into two
235  * @list: a new list to add all removed entries
236  * @head: a list with entries
237  * @entry: an entry within head, could be the head itself
238  *    and if so we won't cut the list
239  *
240  * This helper moves the initial part of @head, up to and
241  * including @entry, from @head to @list. You should
242  * pass on @entry an element you know is on @head. @list
243  * should be an empty list or a list you do not care about
244  * losing its data.
245  *
246  */
247 static inline void list_cut_position(struct list_head *list,
248         struct list_head *head, struct list_head *entry)
249 {
250     if (list_empty(head))
251         return;
252     if (list_is_singular(head) &&
253         (head->next != entry && head != entry))
254         return;
255     if (entry == head)
256         INIT_LIST_HEAD(list);
257     else
258         __list_cut_position(list, head, entry);
259 }
260 
261 static inline void __list_splice(const struct list_head *list,
262                  struct list_head *prev,
263                  struct list_head *next)
264 {
265     struct list_head *first = list->next;
266     struct list_head *last = list->prev;
267 
268     first->prev = prev;
269     prev->next = first;
270 
271     last->next = next;
272     next->prev = last;
273 }
274 
275 /**
276  * list_splice - join two lists, this is designed for stacks
277  * @list: the new list to add.
278  * @head: the place to add it in the first list.
279  */
280 static inline void list_splice(const struct list_head *list,
281                 struct list_head *head)
282 {
283     if (!list_empty(list))
284         __list_splice(list, head, head->next);
285 }
286 
287 /**
288  * list_splice_tail - join two lists, each list being a queue
289  * @list: the new list to add.
290  * @head: the place to add it in the first list.
291  */
292 static inline void list_splice_tail(struct list_head *list,
293                 struct list_head *head)
294 {
295     if (!list_empty(list))
296         __list_splice(list, head->prev, head);
297 }
298 
299 /**
300  * list_splice_init - join two lists and reinitialise the emptied list.
301  * @list: the new list to add.
302  * @head: the place to add it in the first list.
303  *
304  * The list at @list is reinitialised
305  */
306 static inline void list_splice_init(struct list_head *list,
307                     struct list_head *head)
308 {
309     if (!list_empty(list)) {
310         __list_splice(list, head, head->next);
311         INIT_LIST_HEAD(list);
312     }
313 }
314 
315 /**
316  * list_splice_tail_init - join two lists and reinitialise the emptied list
317  * @list: the new list to add.
318  * @head: the place to add it in the first list.
319  *
320  * Each of the lists is a queue.
321  * The list at @list is reinitialised
322  */
323 static inline void list_splice_tail_init(struct list_head *list,
324                      struct list_head *head)
325 {
326     if (!list_empty(list)) {
327         __list_splice(list, head->prev, head);
328         INIT_LIST_HEAD(list);
329     }
330 }
331 
332 /**
333  * list_entry - get the struct for this entry
334  * @ptr:    the &struct list_head pointer.
335  * @type:    the type of the struct this is embedded in.
336  * @member:    the name of the list_struct within the struct.
337  */
338 #define list_entry(ptr, type, member) \
339     container_of(ptr, type, member)
340 
341 /**
342  * list_first_entry - get the first element from a list
343  * @ptr:    the list head to take the element from.
344  * @type:    the type of the struct this is embedded in.
345  * @member:    the name of the list_struct within the struct.
346  *
347  * Note, that list is expected to be not empty.
348  */
349 #define list_first_entry(ptr, type, member) \
350     list_entry((ptr)->next, type, member)
351 
352 /**
353  * list_for_each    -    iterate over a list
354  * @pos:    the &struct list_head to use as a loop cursor.
355  * @head:    the head for your list.
356  */
357 #define list_for_each(pos, head) \
358     for (pos = (head)->next; pos != (head); pos = pos->next)
359 
360 /**
361  * __list_for_each    -    iterate over a list
362  * @pos:    the &struct list_head to use as a loop cursor.
363  * @head:    the head for your list.
364  *
365  * This variant doesn't differ from list_for_each() any more.
366  * We don't do prefetching in either case.
367  */
368 #define __list_for_each(pos, head) \
369     for (pos = (head)->next; pos != (head); pos = pos->next)
370 
371 /**
372  * list_for_each_prev    -    iterate over a list backwards
373  * @pos:    the &struct list_head to use as a loop cursor.
374  * @head:    the head for your list.
375  */
376 #define list_for_each_prev(pos, head) \
377     for (pos = (head)->prev; pos != (head); pos = pos->prev)
378 
379 /**
380  * list_for_each_safe - iterate over a list safe against removal of list entry
381  * @pos:    the &struct list_head to use as a loop cursor.
382  * @n:        another &struct list_head to use as temporary storage
383  * @head:    the head for your list.
384  */
385 #define list_for_each_safe(pos, n, head) \
386     for (pos = (head)->next, n = pos->next; pos != (head); \
387         pos = n, n = pos->next)
388 
389 /**
390  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
391  * @pos:    the &struct list_head to use as a loop cursor.
392  * @n:        another &struct list_head to use as temporary storage
393  * @head:    the head for your list.
394  */
395 #define list_for_each_prev_safe(pos, n, head) \
396     for (pos = (head)->prev, n = pos->prev; \
397          pos != (head); \
398          pos = n, n = pos->prev)
399 
400 /**
401  * list_for_each_entry    -    iterate over list of given type
402  * @pos:    the type * to use as a loop cursor.
403  * @head:    the head for your list.
404  * @member:    the name of the list_struct within the struct.
405  */
406 #define list_for_each_entry(pos, head, member)                \
407     for (pos = list_entry((head)->next, typeof(*pos), member);    \
408          &pos->member != (head);     \
409          pos = list_entry(pos->member.next, typeof(*pos), member))
410 
411 /**
412  * list_for_each_entry_reverse - iterate backwards over list of given type.
413  * @pos:    the type * to use as a loop cursor.
414  * @head:    the head for your list.
415  * @member:    the name of the list_struct within the struct.
416  */
417 #define list_for_each_entry_reverse(pos, head, member)            \
418     for (pos = list_entry((head)->prev, typeof(*pos), member);    \
419          &pos->member != (head);     \
420          pos = list_entry(pos->member.prev, typeof(*pos), member))
421 
422 /**
423  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
424  * @pos:    the type * to use as a start point
425  * @head:    the head of the list
426  * @member:    the name of the list_struct within the struct.
427  *
428  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
429  */
430 #define list_prepare_entry(pos, head, member) \
431     ((pos) ? : list_entry(head, typeof(*pos), member))
432 
433 /**
434  * list_for_each_entry_continue - continue iteration over list of given type
435  * @pos:    the type * to use as a loop cursor.
436  * @head:    the head for your list.
437  * @member:    the name of the list_struct within the struct.
438  *
439  * Continue to iterate over list of given type, continuing after
440  * the current position.
441  */
442 #define list_for_each_entry_continue(pos, head, member)         \
443     for (pos = list_entry(pos->member.next, typeof(*pos), member);    \
444          &pos->member != (head);    \
445          pos = list_entry(pos->member.next, typeof(*pos), member))
446 
447 /**
448  * list_for_each_entry_continue_reverse - iterate backwards from the given point
449  * @pos:    the type * to use as a loop cursor.
450  * @head:    the head for your list.
451  * @member:    the name of the list_struct within the struct.
452  *
453  * Start to iterate over list of given type backwards, continuing after
454  * the current position.
455  */
456 #define list_for_each_entry_continue_reverse(pos, head, member)        \
457     for (pos = list_entry(pos->member.prev, typeof(*pos), member);    \
458          &pos->member != (head);    \
459          pos = list_entry(pos->member.prev, typeof(*pos), member))
460 
461 /**
462  * list_for_each_entry_from - iterate over list of given type from the current point
463  * @pos:    the type * to use as a loop cursor.
464  * @head:    the head for your list.
465  * @member:    the name of the list_struct within the struct.
466  *
467  * Iterate over list of given type, continuing from current position.
468  */
469 #define list_for_each_entry_from(pos, head, member)             \
470     for (; &pos->member != (head);    \
471          pos = list_entry(pos->member.next, typeof(*pos), member))
472 
473 /**
474  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
475  * @pos:    the type * to use as a loop cursor.
476  * @n:        another type * to use as temporary storage
477  * @head:    the head for your list.
478  * @member:    the name of the list_struct within the struct.
479  */
480 #define list_for_each_entry_safe(pos, n, head, member)            \
481     for (pos = list_entry((head)->next, typeof(*pos), member),    \
482         n = list_entry(pos->member.next, typeof(*pos), member);    \
483          &pos->member != (head);                     \
484          pos = n, n = list_entry(n->member.next, typeof(*n), member))
485 
486 /**
487  * list_for_each_entry_safe_continue - continue list iteration safe against removal
488  * @pos:    the type * to use as a loop cursor.
489  * @n:        another type * to use as temporary storage
490  * @head:    the head for your list.
491  * @member:    the name of the list_struct within the struct.
492  *
493  * Iterate over list of given type, continuing after current point,
494  * safe against removal of list entry.
495  */
496 #define list_for_each_entry_safe_continue(pos, n, head, member)         \
497     for (pos = list_entry(pos->member.next, typeof(*pos), member),         \
498         n = list_entry(pos->member.next, typeof(*pos), member);        \
499          &pos->member != (head);                        \
500          pos = n, n = list_entry(n->member.next, typeof(*n), member))
501 
502 /**
503  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
504  * @pos:    the type * to use as a loop cursor.
505  * @n:        another type * to use as temporary storage
506  * @head:    the head for your list.
507  * @member:    the name of the list_struct within the struct.
508  *
509  * Iterate over list of given type from current point, safe against
510  * removal of list entry.
511  */
512 #define list_for_each_entry_safe_from(pos, n, head, member)             \
513     for (n = list_entry(pos->member.next, typeof(*pos), member);        \
514          &pos->member != (head);                        \
515          pos = n, n = list_entry(n->member.next, typeof(*n), member))
516 
517 /**
518  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
519  * @pos:    the type * to use as a loop cursor.
520  * @n:        another type * to use as temporary storage
521  * @head:    the head for your list.
522  * @member:    the name of the list_struct within the struct.
523  *
524  * Iterate backwards over list of given type, safe against removal
525  * of list entry.
526  */
527 #define list_for_each_entry_safe_reverse(pos, n, head, member)        \
528     for (pos = list_entry((head)->prev, typeof(*pos), member),    \
529         n = list_entry(pos->member.prev, typeof(*pos), member);    \
530          &pos->member != (head);                     \
531          pos = n, n = list_entry(n->member.prev, typeof(*n), member))
532 
533 /**
534  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
535  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
536  * @n:        temporary storage used in list_for_each_entry_safe
537  * @member:    the name of the list_struct within the struct.
538  *
539  * list_safe_reset_next is not safe to use in general if the list may be
540  * modified concurrently (eg. the lock is dropped in the loop body). An
541  * exception to this is if the cursor element (pos) is pinned in the list,
542  * and list_safe_reset_next is called after re-taking the lock and before
543  * completing the current iteration of the loop body.
544  */
545 #define list_safe_reset_next(pos, n, member)                \
546     n = list_entry(pos->member.next, typeof(*pos), member)
547 
548 /*
549  * Double linked lists with a single pointer list head.
550  * Mostly useful for hash tables where the two pointer list head is
551  * too wasteful.
552  * You lose the ability to access the tail in O(1).
553  */
554 
555 #endif
list.h

测试用例:

 1 #include "list.h"
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 
 6 #define SZ 32
 7 
 8 #define CNT 66
 9 
10 struct ourstu {
11     char name[SZ];
12     int num;
13     struct list_head list;
14 };
15 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
16 
17 #define container_of(ptr, type, member) ({            \
18     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
19     (type *)( (char *)__mptr - offsetof(type,member) );})
20 
21 
22 
23 /*创建双向循环链表*/
24 LIST_HEAD(head);
25 
26 static int demo_init(void)
27 {
28     int i;
29     struct ourstu *stu;
30 
31     struct list_head *pos, *n;
32 
33     for (i = 0; i < CNT; i++) {
34         stu = malloc(sizeof(struct ourstu));
35         if (NULL == stu) {
36             goto error0;
37         }
38         stu->num = 9527 + i;
39         sprintf(stu->name, "spring%d", i);
40 
41         list_add_tail(&stu->list, &head);
42     }
43 
44     list_for_each (pos, &head) {
45         stu = container_of(pos, struct ourstu, list);            
46         printf("%s's number %d\n", stu->name, stu->num);
47     }
48 
49 
50     return 0;
51     
52 error0:
53     list_for_each_safe(pos, n, &head) {
54         stu = container_of(pos, struct ourstu, list);    
55         printf("%s see bye...\n", stu->name);
56         free(stu);
57     }
58 
59     return -1;
60 }
61 
62 
63 
64 static void demo_exit(void)
65 {
66     struct list_head *n, *pos;
67     struct ourstu *stu;
68 
69     list_for_each_safe(pos, n, &head) {
70         stu = container_of(pos, struct ourstu, list);    
71         printf("%s say bye...\n", stu->name);
72         free(stu);
73     }
74 
75     printf("see you, kernel...\n");
76 }
77 
78 int main()
79 {
80     demo_init();
81     demo_exit();
82 }

 先学习到这,以后用到 list.h 中更多的再来补充。

转载于:https://www.cnblogs.com/jason-linux/p/10465827.html

相关文章:

  • unknown
  • 3.Jmeter参数化
  • 抓取远程图片到本地,你会用什么函数?
  • ELK 企业内部日志分析系统
  • Handler中的IdleHandler
  • 企业级 SpringBoot 教程 (十七)上传文件
  • bboss v5.5.3 发布,Elasticsearch Rest Client
  • 4.Git文件系统
  • ios监听键盘删除事件
  • 秒懂正则表达式
  • 怎么把GPU上训练的模型转到TPU或者CPU上去?DeepMind发布新工具支招
  • 互联网项目中mysql应该选什么事务隔离级别
  • 转载【阿里员工排查问题的工具清单,总有一款适合你】
  • 一、图书管理系统
  • 来自Google资深工程师的API设计最佳实践
  • ES6核心特性
  • HTTP中的ETag在移动客户端的应用
  • Java IO学习笔记一
  • V4L2视频输入框架概述
  • Vue.js 移动端适配之 vw 解决方案
  • vuex 笔记整理
  • Vue实战(四)登录/注册页的实现
  • WebSocket使用
  • 分布式熔断降级平台aegis
  • 计算机常识 - 收藏集 - 掘金
  • 什么是Javascript函数节流?
  • 双管齐下,VMware的容器新战略
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • 昨天1024程序员节,我故意写了个死循环~
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • #Linux(Source Insight安装及工程建立)
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (12)目标检测_SSD基于pytorch搭建代码
  • (3)STL算法之搜索
  • (SpringBoot)第二章:Spring创建和使用
  • (二)Linux——Linux常用指令
  • (分布式缓存)Redis持久化
  • (附源码)ssm高校实验室 毕业设计 800008
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (一)Neo4j下载安装以及初次使用
  • (转)树状数组
  • .bat批处理出现中文乱码的情况
  • .net core 6 集成和使用 mongodb
  • .Net Core 中间件验签
  • .NET Core/Framework 创建委托以大幅度提高反射调用的性能
  • .NET开源快速、强大、免费的电子表格组件
  • .NET企业级应用架构设计系列之技术选型
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • [].shift.call( arguments ) 和 [].slice.call( arguments )
  • [3300万人的聊天室] 作为产品的上游公司该如何?
  • [Android] 240204批量生成联系人,短信,通话记录的APK