免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2112 | 回复: 2
打印 上一主题 下一主题

[分享] list.h 把内核中的代码抽出来,用来实现 链表 ,有源码,希望大家斧正 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-27 10:45 |只看该作者 |倒序浏览
本帖最后由 liuchang8877 于 2012-07-27 10:46 编辑

  1. //内核中有关链表的实现的 头文件 list.h

  2. #ifndef _LINUX_LIST_H
  3. #define _LINUX_LIST_H

  4. //#if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)


  5. /*
  6. * Simple doubly linked list implementation.
  7. *
  8. * Some of the internal functions ("__xxx") are useful when
  9. * manipulating whole lists rather than single entries, as
  10. * sometimes we already know the next/prev entries and we can
  11. * generate better code by using them directly rather than
  12. * using the generic single-entry routines.
  13. */

  14. struct list_head {
  15.         struct list_head *next, *prev;
  16. };

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

  18. #define LIST_HEAD(name) \
  19.         struct list_head name = LIST_HEAD_INIT(name)

  20. #define INIT_LIST_HEAD(ptr) do { \
  21.         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  22. } while (0)

  23. /*
  24. * Insert a new entry between two known consecutive entries.
  25. *
  26. * This is only for internal list manipulation where we know
  27. * the prev/next entries already!
  28. */
  29. static __inline__ void __list_add(struct list_head * new,
  30.         struct list_head * prev,
  31.         struct list_head * next)
  32. {
  33.         next->prev = new;
  34.         new->next = next;
  35.         new->prev = prev;
  36.         prev->next = new;
  37. }

  38. /**
  39. * list_add - add a new entry
  40. * @new: new entry to be added
  41. * @head: list head to add it after
  42. *
  43. * Insert a new entry after the specified head.
  44. * This is good for implementing stacks.
  45. */
  46. static __inline__ void list_add(struct list_head *new, struct list_head *head)
  47. {
  48.         __list_add(new, head, head->next);
  49. }

  50. /**
  51. * list_add_tail - add a new entry
  52. * @new: new entry to be added
  53. * @head: list head to add it before
  54. *
  55. * Insert a new entry before the specified head.
  56. * This is useful for implementing queues.
  57. */
  58. static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
  59. {
  60.         __list_add(new, head->prev, head);
  61. }

  62. /**
  63. * list_get_tail - get end
  64. * @head: list head to add it before
  65. *
  66. * Return the end
  67. */
  68. static __inline__ struct list_head* list_get_end(struct list_head *head)
  69. {
  70.         return head->prev;
  71. }

  72. static __inline__ struct list_head* list_get_head(struct list_head *head)
  73. {
  74.         return head->next;
  75. }

  76. /*
  77. * Delete a list entry by making the prev/next entries
  78. * point to each other.
  79. *
  80. * This is only for internal list manipulation where we know
  81. * the prev/next entries already!
  82. */
  83. static __inline__ void __list_del(struct list_head * prev,
  84.                                   struct list_head * next)
  85. {
  86.         next->prev = prev;
  87.         prev->next = next;
  88. }

  89. /**
  90. * list_del - deletes entry from list.
  91. * @entry: the element to delete from the list.
  92. * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
  93. */
  94. static __inline__ void list_del(struct list_head *entry)
  95. {
  96.         __list_del(entry->prev, entry->next);
  97.         entry->next = entry->prev = 0;
  98. }

  99. /**
  100. * list_del_init - deletes entry from list and reinitialize it.
  101. * @entry: the element to delete from the list.
  102. */
  103. static __inline__ void list_del_init(struct list_head *entry)
  104. {
  105.         __list_del(entry->prev, entry->next);
  106.         INIT_LIST_HEAD(entry);
  107. }

  108. /**
  109. * list_empty - tests whether a list is empty
  110. * @head: the list to test.
  111. */
  112. static __inline__ int list_empty(struct list_head *head)
  113. {
  114.         return head->next == head;
  115. }

  116. /**
  117. * list_splice - join two lists
  118. * @list: the new list to add.
  119. * @head: the place to add it in the first list.
  120. */
  121. static __inline__ void list_splice(struct list_head *list, struct list_head *head)
  122. {
  123.         struct list_head *first = list->next;

  124.         if (first != list) {
  125.                 struct list_head *last = list->prev;
  126.                 struct list_head *at = head->next;

  127.                 first->prev = head;
  128.                 head->next = first;

  129.                 last->next = at;
  130.                 at->prev = last;
  131.         }
  132. }

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

  141. /**
  142. * list_for_each        -        iterate over a list
  143. * @pos:        the &struct list_head to use as a loop counter.
  144. * @head:        the head for your list.
  145. */
  146. #define list_for_each(pos, head) \
  147.         for (pos = (head)->next; pos != (head); \
  148.                 pos = pos->next)
  149.                
  150. /**
  151. * list_for_each_safe        -        iterate over a list safe against removal of list entry
  152. * @pos:        the &struct list_head to use as a loop counter.
  153. * @n:                another &struct list_head to use as temporary storage
  154. * @head:        the head for your list.
  155. */
  156. #define list_for_each_safe(pos, n, head) \
  157.         for (pos = (head)->next, n = pos->next; pos != (head); \
  158.                 pos = n, n = pos->next)

  159. //#endif /* __KERNEL__ || _LVM_H_INCLUDE */

  160. #endif
复制代码

  1. 这是带有 结构体说明的 简单使用的例子,希望大家多多指教
  2. /* STRUCT */
  3. typedef struct xml_value{
  4.         struct list_head list;
  5.         char * key_value;
  6. }xml_value;

  7. typedef struct XMLConfInfo {
  8.         xml_value *xml_value;
  9.         int  key_count;
  10.         xmlDocPtr doc;
  11.         xmlDocPtr xsd;
  12.         xmlChar *xsd_string;
  13.         char errmsg[XML_ERROMSG_LEN];

  14.         char *dict;
  15.         char *dict_bak;

  16. } XMLConfInfo;

  17. xml_value* init_xmL_value(char *value)
  18. {
  19.         //get a new node
  20.     xml_value* key = (xml_value*)malloc(sizeof(xml_value));
  21.     memcpy(key->key_value,value,strlen(value));
  22.     key->key_value[strlen(value)] = '\0';
  23.        
  24.         if(NULL == key->key_value){
  25.                 printf("[ERROR] xmlconf:malloc value out of memory\n");
  26.          free(key);
  27.         }
  28.         return key;
  29. }

  30. void  add_xml_value(XMLConfInfo *info,xml_value *key)
  31. {
  32.         list_add((struct list_head*)info->xml_value,(struct list_head*)&key);
  33.         info->key_count++;
  34. }
  35. /* 原有版本
  36. void del_xml_value(XMLConfInfo *info)
  37. {
  38.         int i ;
  39.         int count = info->key_count;
  40.         struct list_head * list = NULL;

  41.         for(i=0; i<count; i++){
  42.                 list = list_get_head((struct list_head *)&info->xml_value);
  43.                 list_del(list);
  44.                 info->key_count--;
  45.         }

  46. }*/
  47. //2012.7.27 修改后
  48. 354 void del_xml_value(XMLConfInfo *info)
  49. 355 {
  50. 356     int i ;
  51. 357     int count = info->key_count;
  52. 358     struct list_head * list = NULL;
  53. 359
  54. 360     for(i=0; i<count; i++){
  55. 361         list = list_get_head((struct list_head *)&info->xml_value);
  56. 362         list_del(list);
  57. 363         free(((xml_value *)list)->key_value);
  58. 364         free(list);
  59. 365         info->key_count--;
  60. 366     }
  61. 367
  62. 368 }

复制代码

论坛徽章:
14
水瓶座
日期:2014-06-10 09:51:0215-16赛季CBA联赛之江苏
日期:2017-11-27 11:42:3515-16赛季CBA联赛之八一
日期:2017-04-12 14:26:2815-16赛季CBA联赛之吉林
日期:2016-08-20 10:43:1215-16赛季CBA联赛之广夏
日期:2016-06-23 09:53:58程序设计版块每日发帖之星
日期:2016-02-11 06:20:00程序设计版块每日发帖之星
日期:2016-02-09 06:20:0015-16赛季CBA联赛之上海
日期:2015-12-25 16:40:3515-16赛季CBA联赛之广夏
日期:2015-12-22 09:39:36程序设计版块每日发帖之星
日期:2015-08-24 06:20:002015亚冠之德黑兰石油
日期:2015-08-07 09:57:302015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2012-07-27 22:23 |只看该作者
自己调试 ,功能正常,还需要斧正什么?

论坛徽章:
0
3 [报告]
发表于 2012-07-30 13:32 |只看该作者
回复 2# lxyscls 新手嘛,看有需要修改和改进的地方,或是更好的方法没。


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP