Linux内核列表使用例子

参考http://www.ibm.com/developerworks/library/l-timers-list/

#include <stdlib.h>
#include <stdio.h>

struct list_head { 
    struct list_head *next, *prev; 
}; 

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

#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

/**
 * list_entry - get the struct for this entry
 * @ptr:    the &struct list_head pointer.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

/**
 * list_for_each    -   iterate over a list
 * @pos:    the &struct list_head to use as a loop cursor.
 * @head:   the head for your list.
 */
#define list_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * list_for_each_entry  -   iterate over list of given type 
 * @pos:    the type * to use as a loop cursor. 
 * @head:   the head for your list. 
 * @member: the name of the list_struct within the struct. 
 */
#define list_for_each_entry(pos, head, member)              \
    for (pos = list_entry((head)->next, typeof(*pos), member);  \
         &pos->member != (head);    \
         pos = list_entry(pos->member.next, typeof(*pos), member)) 


/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:      another &struct list_head to use as temporary storage
 * @head:   the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)


/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}
#define LIST_POISON1  ((void *) 0x00100100 ) 
#define LIST_POISON2  ((void *) 0x00200200 ) 

static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}

#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );}) 

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

static inline void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next)
{
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}

static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

struct my_data_struct {
  int value;
  struct list_head full_list;
  struct list_head odd_list;
};

LIST_HEAD( my_full_list );
LIST_HEAD( my_odd_list );


int main( void ) {
//init
  int count;
  struct my_data_struct *obj1, *obj2,*obj3;

  printf("my_full_list addres:%p, prev:%p, next:%p\n", &my_full_list, my_full_list.prev, my_full_list.next);
  printf("\n"); 
 obj1= (struct my_data_struct *)
       malloc( sizeof(struct my_data_struct));
 obj1->value = 1;
 list_add( &obj1->full_list, &my_full_list );
  printf("my_full_list addres:%p, prev:%p, next:%p\n", &my_full_list, my_full_list.prev, my_full_list.next);
 printf("obj.vale:%d,obj address:%p, prev:%p, next:%p\n", obj1->value, &(obj1->full_list), obj1->full_list.prev, obj1->full_list.next);
  printf("\n"); 

 obj2 = (struct my_data_struct *)
       malloc( sizeof(struct my_data_struct));
 obj2->value = 2;
 list_add( &obj2->full_list, &my_full_list );
  printf("my_full_list addres:%p, prev:%p, next:%p\n", &my_full_list, my_full_list.prev, my_full_list.next);
 printf("obj.vale:%d,obj address:%p, prev:%p, next:%p\n", obj1->value, &(obj1->full_list), obj1->full_list.prev, obj1->full_list.next);
 printf("obj2.vale:%d,obj2 address:%p, prev:%p, next:%p\n", obj2->value, &(obj2->full_list), obj2->full_list.prev, obj2->full_list.next);
  printf("\n"); 

 obj3 = (struct my_data_struct *)
       malloc( sizeof(struct my_data_struct));
 obj3->value = 3;
 list_add( &obj3->full_list, &my_full_list );
  printf("my_full_list addres:%p, prev:%p, next:%p\n", &my_full_list, my_full_list.prev, my_full_list.next);
 printf("obj.vale:%d,obj address:%p, prev:%p, next:%p\n", obj1->value, &(obj1->full_list), obj1->full_list.prev, obj1->full_list.next);
 printf("obj2.vale:%d,obj2 address:%p, prev:%p, next:%p\n", obj2->value, &(obj2->full_list), obj2->full_list.prev, obj2->full_list.next);
 printf("obj3.vale:%d,obj3 address:%p, prev:%p, next:%p\n", obj3->value, &(obj3->full_list), obj3->full_list.prev, obj3->full_list.next);
  printf("\n"); 

//leanup
  struct list_head *pos, *q;
  struct my_data_struct *my_obj;

  printf("Emit full list\n");
  list_for_each( pos, &my_full_list ) {
    my_obj = list_entry( pos, struct my_data_struct, full_list );
    printf( "%d\n", my_obj->value );
  }

  printf("Emit odd list\n");
  list_for_each_entry( my_obj, &my_odd_list, odd_list ) {
    printf( "%d\n", my_obj->value );
  }

  printf("Cleaning up\n");
  list_for_each_safe( pos, q, &my_full_list ) {
    struct my_data_struct *tmp;
    tmp = list_entry( pos, struct my_data_struct, full_list );
    list_del( pos );
    free( tmp );
  }

  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值