内核链表初始化

时间:2013-08-04 21:21:25

标签: linked-list kernel

在下面的代码中我试图在内核模块中创建一个链表,为什么我会得到以下错误?

error(in LIST_HEAD(root->list)): expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token


typedef struct
{
     int a;
     char* b;
     struct list_head list;

}mystruct;

void add(mystruct* r)
{
   mystruct* tmp = (mystruct*)vmalloc(sizeof(mystruct));
   tmp->a = 110;
   tmp->b = (char*)vmalloc(sizeof(char)*10);
   list_add_tail(&(tmp->list),&(r->list));
}
int init_module(void)
{
  mystruct *root;
  LIST_HEAD(root->list);
  add(root);
}

1 个答案:

答案 0 :(得分:2)

@Sweet,我修改了上面的代码,如下所示,

int list_init(void)
{
  mystruct *root = (mystruct*)kmalloc(sizeof(mystruct), GFP_KERNEL); //or mystruct root;

  INIT_LIST_HEAD(&root->list); //or INIT_LIST_HEAD(&root.list);
  add(root);

  return 0;
}

它对我来说很好,请在你的结尾尝试: - )。

相关问题