#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct list_head ulist;
struct task {
struct list_head list;
int id;
char name[10];
};
int main()
{
int i;
struct task *task_item;
struct list_head *ptr, *ptr_del;;
LIST_HEAD(ulist);
for (i=0; i<10; i++)
{
task_item = (struct task *)malloc(sizeof(struct task));
task_item->id=i;
sprintf (task_item->name, "# %-2d #", i);
list_add(&task_item->list, &ulist);
printf ("add item=%p,id=%d,name=%s \n", &task_item->list, task_item->id, task_item->name);
}
for (ptr=ulist.prev; ptr != &ulist;)
{
task_item = list_entry(ptr, struct task, list);
printf ("foreach item=%p,id=%d,name=%s \n", ptr, task_item->id, task_item->name);
ptr_del=ptr;
ptr=ptr->prev;
list_del(ptr_del);
printf ("del item=%p,id=%d,name=%s \n", ptr_del, task_item->id, task_item->name);
free(task_item);
}
/*在这里不要用for_each_entry那个宏,会出警告,具体警告忘记了。都是因为prefetch(pos->member.prev)宏的问题,查了下,好像是说告诉cpu预先取出这个数,是优化用的,在用户空间没有必要吧*/
for (ptr=ulist.prev; ptr != &ulist; ptr=ptr->prev)
{
task_item = list_entry(ptr, struct task, list);
printf ("foreach item=%p,id=%d,name=%s \n", &task_item->list, task_item->id, task_item->name);
}
return 0;
}
|