感性狂野理智不灭 发表于 2014-04-10 18:27

list.h 用户态下 list_entry错误

本帖最后由 感性狂野理智不灭 于 2014-04-10 18:31 编辑

一个简单的小实验程序

linux课程的一个实验
调用内核源码里的list.h的宏, 简单使用实现链表的插入删除求和并将其插入尾部

gcc 添加 -D__KERNEL__ 和 自己添加offset和container_of定义都不行求解不胜感谢


系统环境:win8.1 64位virtualbox虚拟机ubuntu64位

错误信息:windery@windery-VirtualBox:~/linux$ gcc ex1.c -D __KERNEL__ -I/usr/src/linux-headers-3.11.0-12/include/ -o ex1
In file included from ex1.c:1:0:
ex1.c: In function ‘my_delete’:
ex1.c:33:30: error: expected expression before ‘struct’
         tmp = list_entry(pos,struct node, list);
                              ^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
                  ^
ex1.c: In function ‘my_sum’:
ex1.c:56:31: error: expected expression before ‘struct’
         tmp = list_entry(pos, struct node, list);
                               ^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
                  ^
ex1.c: In function ‘my_show’:
ex1.c:70:31: error: expected expression before ‘struct’
         tmp = list_entry(pos, struct node, list);
                               ^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
                  ^
ex1.c: In function ‘my_delete_all’:
ex1.c:83:31: error: expected expression before ‘struct’
         tmp = list_entry(pos, struct node, list);
                               ^
/usr/src/linux-headers-3.11.0-12/include/linux/list.h:351:20: note: in definition of macro ‘list_entry’
container_of(ptr, type, member)
                  ^
源代码:#include <linux/list.h>
#include <stdio.h>
#include <malloc.h>


struct node
{
    int num;
    struct list_head list;
};




int my_insert(struct node *my_list, int num)
{
    struct node *tmpNode;

    tmpNode = (struct node *)malloc(sizeof(struct node));
    tmpNode->num = num;
    INIT_LIST_HEAD(&tmpNode->list);
    list_add_tail(&tmpNode->list, &my_list->list);
}

int my_delete(struct node *my_list, int num)
{
    struct node *tmp;
    struct list_head *pos, *n;
   

    list_for_each_safe(pos, n, &my_list->list)
    {
      tmp = list_entry(pos,struct node, list);
      if (tmp->num == num)
            list_del(pos);
      free(tmp);
    }
}

int my_init(struct node **my_listp)
{
    *my_listp = (struct node *)malloc(sizeof(struct node));
    INIT_LIST_HEAD(&(*my_listp)->list);

    return 1;
}

int my_sum(struct node *my_list)
{
    struct list_head *pos, *n;
    struct node *tmp;
    int sum = 0;

    list_for_each_safe(pos, n, &my_list->list)
    {
       tmp = list_entry(pos, struct node, list);
      sum += tmp->num;
    }

    return sum;
}

void my_show(struct node *my_list)
{
    struct node *tmp;
    struct list_head *pos, *n;

    list_for_each_safe(pos, n, &my_list->list)
    {
       tmp = list_entry(pos, struct node, list);
      printf("num: %d\n", tmp->num);
    }
}

void my_delete_all(struct node *my_list)
{
    struct node *tmp;
    struct list_head *pos, *n;

    list_for_each_safe(pos, n, &my_list->list)
    {
      list_del(pos);
      tmp = list_entry(pos, struct node, list);
      free(tmp);
    }
}

int main()
{
    int i;

    struct node *my_list;
    my_init(&my_list);

    for (i = 0; i < 10; i++)
      my_insert(my_list, i+1);

    my_show(my_list);

    my_delete(my_list, 3);
    my_show(my_list);

    int sum = my_sum(my_list);
    printf("sum: %d", sum);

    my_insert(my_list, sum);
    my_show(my_list);

    my_delete_all(my_list);

    return 0;
}

鬼鬼一哈 发表于 2014-04-10 19:28

list_entry就是container_of,是用下面的编译试试看,以前测试过是可以用的.
-----------------------------------------------------------------------
#define container_of(ptr, type, member) ( { \
      const typeof( ((type *)0)->member ) *__mptr = (ptr); \
      (type *)( (char *)__mptr - offsetof(type,member) ); } )

#define list_entry(ptr, type, member) container_of(ptr, type, member)

感性狂野理智不灭 发表于 2014-04-14 17:26

回复 2# 鬼鬼一哈


   代码加上这一段已通过编译,,   谢啦~~   代码还有错误, 继续改去

感性狂野理智不灭 发表于 2014-04-14 21:47

回复 3# 感性狂野理智不灭


    ok   完工,

my_delete函数free函数位置有问题

运行无误

#include <linux/list.h>

#include <stdio.h>

#include <malloc.h>





#define container_of(ptr, type, member) ( { \

      const typeof( ((type *)0)->member ) *__mptr = (ptr); \

      (type *)( (char *)__mptr - offsetof(type,member) ); } )



#define list_entry(ptr, type, member) container_of(ptr, type, member)





struct node

{

    int num;

    struct list_head list;

};









int my_insert(struct node *my_list, int num)

{

    struct node *tmpNode;



    tmpNode = (struct node *)malloc(sizeof(struct node));

    tmpNode->num = num;

    INIT_LIST_HEAD(&tmpNode->list);

    list_add_tail(&tmpNode->list, &my_list->list);

}



int my_delete(struct node *my_list, int num)

{

    struct node *tmp;

    struct list_head *pos, *n;

   



    list_for_each_safe(pos, n, &my_list->list)

    {

      tmp = list_entry(pos,struct node, list);

      if (tmp->num == num)

      {

            list_del(pos);

            free(tmp);

            //break;

      }

    }

}



int my_init(struct node **my_listp)

{

    *my_listp = (struct node *)malloc(sizeof(struct node));

    INIT_LIST_HEAD(&(*my_listp)->list);



    return 1;

}



int my_sum(struct node *my_list)

{

    struct list_head *pos, *n;

    struct node *tmp;

    int sum = 0;



    list_for_each_safe(pos, n, &my_list->list)

    {

      tmp = list_entry(pos, struct node, list);

      sum += tmp->num;

    }



    return sum;

}



void my_show(struct node *my_list)

{

    struct node *tmp;

    struct list_head *pos, *n;



    list_for_each_safe(pos, n, &my_list->list)

    {

      tmp = list_entry(pos, struct node, list);

      printf("num: %d\n", tmp->num);

    }

    printf("\n");

}



void my_delete_all(struct node *my_list)

{

    struct node *tmp;

    struct list_head *pos, *n;



    list_for_each_safe(pos, n, &my_list->list)

    {

      list_del(pos);

      tmp = list_entry(pos, struct node, list);

      free(tmp);

    }

}



int main()

{

    int i;



    struct node *my_list;

    my_init(&my_list);



    for (i = 0; i < 10; i++)

      my_insert(my_list, i+1);



    my_show(my_list);



    my_delete(my_list, 3);

    my_show(my_list);



    int sum = my_sum(my_list);

    printf("sum: %d\n\n", sum);



    my_insert(my_list, sum);

    my_show(my_list);



    my_delete_all(my_list);



    return 0;

}

von81 发表于 2014-12-27 12:22

为什么加上2楼的代码就可以了呢?
2楼的代码在内核的list.h文件是有的啊
页: [1]
查看完整版本: list.h 用户态下 list_entry错误