Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404456
  • 博文数量: 35
  • 博客积分: 943
  • 博客等级: 准尉
  • 技术积分: 742
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-15 13:17
个人简介

积累知识,方便自己

文章分类

全部博文(35)

文章存档

2014年(2)

2013年(5)

2012年(13)

2011年(15)

分类: C/C++

2014-05-29 14:17:13

做内核开发都会用到链表,那么在用户态怎样使用链表呢,其实内核链表只是个头文件,将其移植到用户态就行,下面就是本 人修改后的头文件:
list.h:

点击(此处)折叠或打开

  1. #ifndef __LIST_H_
  2. #define __LIST_H_
  3. #include <stdio.h>
  4. struct xm_list_head{
  5. struct xm_list_head *next,*prev;
  6. };
  7. #define xmoffsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  8. #define xm_container_of(ptr, type, member) ({ \
  9. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  10.         (type *)( (char *)__mptr - xmoffsetof(type,member) );})
  11. #define xm_list_entry(ptr, type, member) \
  12.         xm_container_of(ptr, type, member)
  13. #define xm_list_for_each_entry_safe(pos,n,head,member) \
  14. for(pos=xm_list_entry((head)->next,typeof(*pos),member), \
  15.         n=xm_list_entry(pos->member.next,typeof(*pos),member); \
  16.         &pos->member!=(head); \
  17.         pos=n,n=xm_list_entry(n->member.next,typeof(*n),member))

  18. static inline void xm_list_init_head(struct xm_list_head *list)
  19. {
  20.     list->next=list;
  21.     list->prev=list;
  22. }
  23. static inline int xm_list_empty(const struct xm_list_head *head)
  24. {
  25.     return head->next == head;
  26. }
  27. static inline void __xm_list_add(struct xm_list_head *_new,
  28.                                  struct xm_list_head *prev,
  29.                                  struct xm_list_head *next)
  30. {
  31.     next->prev = _new;
  32.     _new->next = next;
  33.     _new->prev = prev;
  34.     prev->next = _new;
  35. }
  36. static inline void __xm_list_insert_before(struct xm_list_head *_new,struct xm_list_head *prev,struct xm_list_head *next)
  37. {
  38.     _new->prev=prev;
  39.     _new->next=next;
  40.     next->prev=_new;
  41.     prev->next=_new;
  42. }
  43. static inline void xm_list_add_tail(struct xm_list_head *_new, struct xm_list_head *head)
  44. {
  45.     __xm_list_add(_new, head->prev, head);
  46. }
  47. static inline void xm_list_add_before(struct xm_list_head *_new, struct xm_list_head *head)
  48. {
  49.     __xm_list_insert_before(_new, head->prev, head);
  50. }
  51. static inline void __xm_list_del(struct xm_list_head *prev, struct xm_list_head *next)
  52. {
  53.     next->prev = prev;
  54.     prev->next = next;
  55. }
  56. static inline void xm_list_del(struct xm_list_head *entry)
  57. {
  58.     __xm_list_del(entry->prev, entry->next);
  59.     entry->next = NULL;
  60.     entry->prev = NULL;
  61. }
  62. #endif


阅读(3081) | 评论(0) | 转发(0) |
0

上一篇:使用CRC20算法对ip信息五元组hash键值计算

下一篇:没有了

给主人留下些什么吧!~~