做内核开发都会用到链表,那么在用户态怎样使用链表呢,其实内核链表只是个头文件,将其移植到用户态就行,下面就是本 人修改后的头文件:
list.h:
-
#ifndef __LIST_H_
-
#define __LIST_H_
-
#include <stdio.h>
-
struct xm_list_head{
-
struct xm_list_head *next,*prev;
-
};
-
#define xmoffsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-
#define xm_container_of(ptr, type, member) ({ \
-
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
-
(type *)( (char *)__mptr - xmoffsetof(type,member) );})
-
#define xm_list_entry(ptr, type, member) \
-
xm_container_of(ptr, type, member)
-
#define xm_list_for_each_entry_safe(pos,n,head,member) \
-
for(pos=xm_list_entry((head)->next,typeof(*pos),member), \
-
n=xm_list_entry(pos->member.next,typeof(*pos),member); \
-
&pos->member!=(head); \
-
pos=n,n=xm_list_entry(n->member.next,typeof(*n),member))
-
-
static inline void xm_list_init_head(struct xm_list_head *list)
-
{
-
list->next=list;
-
list->prev=list;
-
}
-
static inline int xm_list_empty(const struct xm_list_head *head)
-
{
-
return head->next == head;
-
}
-
static inline void __xm_list_add(struct xm_list_head *_new,
-
struct xm_list_head *prev,
-
struct xm_list_head *next)
-
{
-
next->prev = _new;
-
_new->next = next;
-
_new->prev = prev;
-
prev->next = _new;
-
}
-
static inline void __xm_list_insert_before(struct xm_list_head *_new,struct xm_list_head *prev,struct xm_list_head *next)
-
{
-
_new->prev=prev;
-
_new->next=next;
-
next->prev=_new;
-
prev->next=_new;
-
}
-
static inline void xm_list_add_tail(struct xm_list_head *_new, struct xm_list_head *head)
-
{
-
__xm_list_add(_new, head->prev, head);
-
}
-
static inline void xm_list_add_before(struct xm_list_head *_new, struct xm_list_head *head)
-
{
-
__xm_list_insert_before(_new, head->prev, head);
-
}
-
static inline void __xm_list_del(struct xm_list_head *prev, struct xm_list_head *next)
-
{
-
next->prev = prev;
-
prev->next = next;
-
}
-
static inline void xm_list_del(struct xm_list_head *entry)
-
{
-
__xm_list_del(entry->prev, entry->next);
-
entry->next = NULL;
-
entry->prev = NULL;
-
}
-
#endif
阅读(3165) | 评论(0) | 转发(0) |