双链表

来源:互联网 发布:网络电缆测试仪上电池 编辑:程序博客网 时间:2024/06/04 01:07

实现了一个简单方便使用的双链表结构。是修改linux 2.6 kernel 中 list.h 得来的。

 

  1. /**
  2.  * macro.h - common macro define.
  3.  *
  4.  * Author:jta , 2008.6
  5.  *
  6.  */
  7. #ifndef _MACRO_H_ 
  8. #define _MACRO_H_ 
  9. /*
  10.  * common primary data type operator.
  11.  */
  12. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 
  13. #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1)) 
  14. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 
  15. #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 
  16. #ifndef offsetof 
  17. /**
  18. * return a offset of member in struct.
  19. */
  20. #define offsetof(TYPE,MEMBER) ( (int)(&((TYPE*)0)->MEMBER) ) 
  21. #endif 
  22. /**
  23.  * container_of - cast a member of a structure out to the containing structure
  24.  * @ptr:    the pointer to the member.
  25.  * @type:   the type of the container struct this is embedded in.
  26.  * @member: the name of the member within the struct.
  27.  *
  28.  */
  29. #define container_of(ptr, type, member) ( / 
  30.         (type *)( (char *)ptr - offsetof(type,member) ) )
  31. #define bzero( p , len) memset( p, 0, len ) 
  32. #endif /*_MACRO_H_*/

这里只用到了offsetof 和 container_of 两个宏。

#define offsetof(TYPE,MEMBER) ( (int)(&((TYPE*)0)->MEMBER) )

宏offsetof计算结构体成员在其结构体中的地址偏移量。

#define container_of(ptr, type, member) ( /
        (type *)( (char *)ptr - offsetof(type,member) ) )

宏 container_of由结构体中某个成员地址计算结构体的起始地址。

例如有以下结构体:

  1. struct my_data
  2. {
  3.     int this;
  4.     char other;
  5.     ...
  6.     struct list_head list;
  7. }data;

&data 等于 container_of(&(data.other), struct my_data, other) ;

  1. #ifndef _LIST_H_
  2. #define _LIST_H_
  3. #include "macrodef.h"
  4. /*
  5.  * Simple doubly linked list implementation.
  6.  *
  7.  * from linux kernel 2.6, modified by jta 2008.6
  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. struct list_head {
  16.     struct list_head *next, *prev;
  17. };
  18. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  19. #define LIST_HEAD(name) /
  20.     struct list_head name = LIST_HEAD_INIT(name)
  21. static inline void INIT_LIST_HEAD(struct list_head *list)
  22. {
  23.     list->next = list;
  24.     list->prev = list;
  25. }
  26. /*
  27.  * Insert a new entry between two known consecutive entries.
  28.  *
  29.  * This is only for internal list manipulation where we know
  30.  * the prev/next entries already!
  31.  */
  32. static inline void __list_add(struct list_head *pnew,
  33.                   struct list_head *prev,
  34.                   struct list_head *next)
  35. {
  36.     next->prev = pnew;
  37.     pnew->next = next;
  38.     pnew->prev = prev;
  39.     prev->next = pnew;
  40. }
  41. /**
  42.  * list_add - add a new entry
  43.  * @head: list head to add it after
  44.  * @new: new entry to be added
  45.  *
  46.  * Insert a new entry after the specified head.
  47.  * This is good for implementing stacks.
  48.  */
  49. static inline void list_add(struct list_head *pnew, struct list_head *head)
  50. {
  51.     __list_add(pnew, head, head->next);
  52. }
  53. /**
  54.  * list_add_tail - add a new entry
  55.  * @new: new entry to be added
  56.  * @head: list head to add it before
  57.  *
  58.  * Insert a new entry before the specified head.
  59.  * This is useful for implementing queues.
  60.  */
  61. static inline void list_add_tail(struct list_head *pnew, struct list_head *head)
  62. {
  63.     __list_add(pnew, head->prev, head);
  64. }
  65. /*
  66.  * Delete a list entry by making the prev/next entries
  67.  * point to each other.
  68.  *
  69.  * This is only for internal list manipulation where we know
  70.  * the prev/next entries already!
  71.  */
  72. static inline void __list_del(struct list_head * prev, struct list_head * next)
  73. {
  74.     next->prev = prev;
  75.     prev->next = next;
  76. }
  77. /**
  78.  * list_del - deletes entry from list.
  79.  * @entry: the element to delete from the list.
  80.  * Note: list_empty() on entry does not return true after this, the entry is
  81.  * in an undefined state.
  82.  */
  83. static inline void list_del(struct list_head *entry)
  84. {
  85.     __list_del(entry->prev, entry->next);
  86.     entry->next = NULL;
  87.     entry->prev = NULL;
  88. }
  89. /**
  90.  * list_del_init - deletes entry from list and reinitialize it.
  91.  * @entry: the element to delete from the list.
  92.  */
  93. static inline void list_del_init(struct list_head *entry)
  94. {
  95.     __list_del(entry->prev, entry->next);
  96.     INIT_LIST_HEAD(entry);
  97. }
  98. /**
  99.  * list_replace - replace old entry by new one
  100.  * @old : the element to be replaced
  101.  * @new : the new element to insert
  102.  *
  103.  * If @old was empty, it will be overwritten.
  104.  */
  105. static inline void list_replace(struct list_head *old,
  106.                 struct list_head *pnew)
  107. {
  108.     pnew->next = old->next;
  109.     pnew->next->prev = pnew;
  110.     pnew->prev = old->prev;
  111.     pnew->prev->next = pnew;
  112. }
  113. static inline void list_replace_init(struct list_head *old,
  114.                     struct list_head *pnew)
  115. {
  116.     list_replace(old, pnew);
  117.     INIT_LIST_HEAD(old);
  118. }
  119. /**
  120.  * list_move - delete from one list and add as another's head
  121.  * @list: the entry to move
  122.  * @head: the head that will precede our entry
  123.  */
  124. static inline void list_move(struct list_head *list, struct list_head *head)
  125. {
  126.     __list_del(list->prev, list->next);
  127.     list_add(list, head);
  128. }
  129. /**
  130.  * list_move_tail - delete from one list and add as another's tail
  131.  * @list: the entry to move
  132.  * @head: the head that will follow our entry
  133.  */
  134. static inline void list_move_tail(struct list_head *list,
  135.                   struct list_head *head)
  136. {
  137.     __list_del(list->prev, list->next);
  138.     list_add_tail(list, head);
  139. }
  140. /**
  141.  * list_is_last - tests whether @list is the last entry in list @head
  142.  * @list: the entry to test
  143.  * @head: the head of the list
  144.  */
  145. static inline int list_is_last(const struct list_head *list,
  146.                 const struct list_head *head)
  147. {
  148.     return list->next == head;
  149. }
  150. /**
  151.  * list_empty - tests whether a list is empty
  152.  * @head: the list to test.
  153.  */
  154. static inline int list_empty(const struct list_head *head)
  155. {
  156.     return head->next == head;
  157. }
  158. /**
  159.  * list_empty_careful - tests whether a list is empty and not being modified
  160.  * @head: the list to test
  161.  *
  162.  * Description:
  163.  * tests whether a list is empty _and_ checks that no other CPU might be
  164.  * in the process of modifying either member (next or prev)
  165.  *
  166.  * NOTE: using list_empty_careful() without synchronization
  167.  * can only be safe if the only activity that can happen
  168.  * to the list entry is list_del_init(). Eg. it cannot be used
  169.  * if another CPU could re-list_add() it.
  170.  */
  171. static inline int list_empty_careful(const struct list_head *head)
  172. {
  173.     struct list_head *next = head->next;
  174.     return (next == head) && (next == head->prev);
  175. }
  176. static inline void __list_splice(struct list_head *list,
  177.                  struct list_head *head)
  178. {
  179.     struct list_head *first = list->next;
  180.     struct list_head *last = list->prev;
  181.     struct list_head *at = head->next;
  182.     first->prev = head;
  183.     head->next = first;
  184.     last->next = at;
  185.     at->prev = last;
  186. }
  187. /**
  188.  * list_splice - join two lists
  189.  * @list: the new list to add.
  190.  * @head: the place to add it in the first list.
  191.  */
  192. static inline void list_splice(struct list_head *list, struct list_head *head)
  193. {
  194.     if (!list_empty(list))
  195.         __list_splice(list, head);
  196. }
  197. /**
  198.  * list_splice_init - join two lists and reinitialise the emptied list.
  199.  * @list: the new list to add.
  200.  * @head: the place to add it in the first list.
  201.  *
  202.  * The list at @list is reinitialised
  203.  */
  204. static inline void list_splice_init(struct list_head *list,
  205.                     struct list_head *head)
  206. {
  207.     if (!list_empty(list)) {
  208.         __list_splice(list, head);
  209.         INIT_LIST_HEAD(list);
  210.     }
  211. }
  212. /**
  213.  * list_entry - get the struct for this entry
  214.  * @ptr:    the &struct list_head pointer.
  215.  * @type:   the type of the struct this is embedded in.
  216.  * @member: the name of the list_struct within the struct.
  217.  */
  218. #define list_entry(ptr, type, member) /
  219.     container_of(ptr, type, member)
  220. /**
  221.  * list_first_entry - get the first element from a list
  222.  * @ptr:    the list head to take the element from.
  223.  * @type:   the type of the struct this is embedded in.
  224.  * @member: the name of the list_struct within the struct.
  225.  *
  226.  * Note, that list is expected to be not empty.
  227.  */
  228. #define list_first_entry(ptr, type, member) /
  229.     list_entry((ptr)->next, type, member)
  230. /**
  231.  * list_for_each    -   iterate over a list
  232.  * @pos:    the &struct list_head to use as a loop cursor.
  233.  * @head:   the head for your list.
  234.  */
  235. #define list_for_each(pos, head) /
  236.     for (pos = (head)->next; pos != (head); pos = pos->next)
  237. /**
  238.  * list_for_each_prev   -   iterate over a list backwards
  239.  * @pos:    the &struct list_head to use as a loop cursor.
  240.  * @head:   the head for your list.
  241.  */
  242. #define list_for_each_prev(pos, head) /
  243.     for (pos = (head)->prev; pos != (head); pos = pos->prev)
  244. /**
  245.  * list_for_each_safe - iterate over a list safe against removal of list entry
  246.  * @pos:    the &struct list_head to use as a loop cursor.
  247.  * @n:      another &struct list_head to use as temporary storage
  248.  * @head:   the head for your list.
  249.  */
  250. #define list_for_each_safe(pos, n, head) /
  251.     for (pos = (head)->next, n = pos->next; pos != (head); /
  252.         pos = n, n = pos->next)
  253. /**
  254.  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  255.  * @pos:    the &struct list_head to use as a loop cursor.
  256.  * @n:      another &struct list_head to use as temporary storage
  257.  * @head:   the head for your list.
  258.  */
  259. #define list_for_each_prev_safe(pos, n, head) /
  260.     for (pos = (head)->prev, n = pos->prev; /
  261.          pos != (head); pos = n, n = pos->prev)
  262. /**
  263.  * list_for_each_entry  -   iterate over list of given type
  264.  * @pos:    the type * to use as a loop cursor.
  265.  * @head:   the head for your list.
  266.  * @member: the name of the list_struct within the struct.
  267.  */
  268. #define list_for_each_entry(pos, type, head, member)                /
  269.     for ( pos = list_entry((head)->next, type, member); /
  270.          &pos->member != (head);    /
  271.          pos = list_entry((pos->member).next, type, member))
  272. /**
  273.  * list_for_each_entry_reverse - iterate backwards over list of given type.
  274.  * @pos:    the type * to use as a loop cursor.
  275.  * @head:   the head for your list.
  276.  * @member: the name of the list_struct within the struct.
  277.  */
  278. #define list_for_each_entry_reverse(pos, type, head, member)            /
  279.     for (pos = list_entry((head)->prev, type, member);  /
  280.          &pos->member != (head);    /
  281.          pos = list_entry(pos->member.prev, type, member))
  282. /**
  283.  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  284.  * @pos:    the type * to use as a start point
  285.  * @head:   the head of the list
  286.  * @member: the name of the list_struct within the struct.
  287.  *
  288.  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  289.  */
  290. #define list_prepare_entry(pos, type, head, member) /
  291.     ((pos) ? : list_entry(head, type, member))
  292. /**
  293.  * list_for_each_entry_continue - continue iteration over list of given type
  294.  * @pos:    the type * to use as a loop cursor.
  295.  * @head:   the head for your list.
  296.  * @member: the name of the list_struct within the struct.
  297.  *
  298.  * Continue to iterate over list of given type, continuing after
  299.  * the current position.
  300.  */
  301. #define list_for_each_entry_continue(pos, type, head, member)       /
  302.     for (pos = list_entry(pos->member.next, type, member);  /
  303.          &pos->member != (head);    /
  304.          pos = list_entry(pos->member.next, type, member))
  305. /**
  306.  * list_for_each_entry_continue_reverse - iterate backwards from the given point
  307.  * @pos:    the type * to use as a loop cursor.
  308.  * @head:   the head for your list.
  309.  * @member: the name of the list_struct within the struct.
  310.  *
  311.  * Start to iterate over list of given type backwards, continuing after
  312.  * the current position.
  313.  */
  314. #define list_for_each_entry_continue_reverse(pos, type, head, member)       /
  315.     for (pos = list_entry(pos->member.prev, type, member);  /
  316.          &pos->member != (head);    /
  317.          pos = list_entry(pos->member.prev, type, member))
  318. /**
  319.  * list_for_each_entry_from - iterate over list of given type from the current point
  320.  * @pos:    the type * to use as a loop cursor.
  321.  * @head:   the head for your list.
  322.  * @member: the name of the list_struct within the struct.
  323.  *
  324.  * Iterate over list of given type, continuing from current position.
  325.  */
  326. #define list_for_each_entry_from(pos, type, head, member)           /
  327.     for (; &pos->member != (head);  /
  328.          pos = list_entry(pos->member.next, type, member))
  329. /**
  330.  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  331.  * @pos:    the type * to use as a loop cursor.
  332.  * @n:      another type * to use as temporary storage
  333.  * @head:   the head for your list.
  334.  * @member: the name of the list_struct within the struct.
  335.  */
  336. #define list_for_each_entry_safe(pos, n, type, head, member)            /
  337.     for (pos = list_entry((head)->next, type, member),  /
  338.         n = list_entry(pos->member.next, type, member); /
  339.          &pos->member != (head);                    /
  340.          pos = n, n = list_entry(n->member.next, type, member))
  341. /**
  342.  * list_for_each_entry_safe_continue
  343.  * @pos:    the type * to use as a loop cursor.
  344.  * @n:      another type * to use as temporary storage
  345.  * @head:   the head for your list.
  346.  * @member: the name of the list_struct within the struct.
  347.  *
  348.  * Iterate over list of given type, continuing after current point,
  349.  * safe against removal of list entry.
  350.  */
  351. #define list_for_each_entry_safe_continue(pos, n, type, head, member)       /
  352.     for (pos = list_entry(pos->member.next, type, member),      /
  353.         n = list_entry(pos->member.next, type, member);     /
  354.          &pos->member != (head);                        /
  355.          pos = n, n = list_entry(n->member.next, type, member))
  356. /**
  357.  * list_for_each_entry_safe_from
  358.  * @pos:    the type * to use as a loop cursor.
  359.  * @n:      another type * to use as temporary storage
  360.  * @head:   the head for your list.
  361.  * @member: the name of the list_struct within the struct.
  362.  *
  363.  * Iterate over list of given type from current point, safe against
  364.  * removal of list entry.
  365.  */
  366. #define list_for_each_entry_safe_from(pos, n, type, head, member)           /
  367.     for (n = list_entry(pos->member.next, type, member);        /
  368.          &pos->member != (head);                        /
  369.          pos = n, n = list_entry(n->member.next, type, member))
  370. /**
  371.  * list_for_each_entry_safe_reverse
  372.  * @pos:    the type * to use as a loop cursor.
  373.  * @n:      another type * to use as temporary storage
  374.  * @head:   the head for your list.
  375.  * @member: the name of the list_struct within the struct.
  376.  *
  377.  * Iterate backwards over list of given type, safe against removal
  378.  * of list entry.
  379.  */
  380. #define list_for_each_entry_safe_reverse(pos, n, type, head, member)        /
  381.     for (pos = list_entry((head)->prev, type, member),  /
  382.         n = list_entry(pos->member.prev, type, member); /
  383.          &pos->member != (head);                    /
  384.          pos = n, n = list_entry(n->member.prev, type, member))
  385. #endif /* _LIST_H_ */
0 0
原创粉丝点击