Linux内核链表 [英] Linux Kernel Linked List

查看:151
本文介绍了Linux内核链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Linux内核链表实现,但我无法编译。我下面的这些来源正是没有结果( http://www.roman10.net / Linux的内核programminglinked列表/ http://kernelnewbies.org/FAQ/LinkedLists

I'm trying to use the Linux Kernel Linked List implementation but I am unable to compile. I'm following these sources exactly with no results (http://www.roman10.net/linux-kernel-programminglinked-list/ and http://kernelnewbies.org/FAQ/LinkedLists)

该list.h内核宏LIST_HEAD_INIT如下:

The list.h Kernel Macro for LIST_HEAD_INIT is as follows:

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


struct Node {
int data;
struct list_head list;
};

struct Node mylinkedlist;
LIST_HEAD_INIT(&mylinkedlist.list);    

void add(){
struct Node first;
first.data = 1;
first.list = LIST_HEAD_INIT(first.list);
list_add_tail(&first->list, &mylinkedlist.list);
return 0;
}

我不断收到:
    错误:预期的标识符或'('前'{'

I keep getting: "error: expected identifier or '(' before '{'"

推荐答案

您获得的是错的。结果
首先,你应该 LIST_HEAD(mylinkedlist),不是 LIST_HEAD_INIT 也没有结构节点mylinkedlist。结果
mylinkedlist应该是内核链表结构的一个独立的头,它用来链接所有LIST_HEAD。

You are getting that wrong.
First, your should LIST_HEAD(mylinkedlist), not LIST_HEAD_INIT nor struct Node mylinkedlist.
mylinkedlist should be a standalone head of kernel linked list struct, it's used to link all list_head.

其次,你应该 INIT_LIST_HEAD(安培; first.list),这是一种动态分配; LIST_HEAD_INIT 当结构是在编译时静态创建使用。

Second, you should INIT_LIST_HEAD(&first.list), this is the way to dynamically assignment; LIST_HEAD_INIT is used when structure is statically created at compile time.

最后,你应该 list_add_tail(安培; first.list,&安培; mylinkedlist)

所以整个code应该是:

so the complete code should be:

LIST_HEAD(mylinkedlist);

void add(){
  struct Node first;
  first.data = 1;
  INIT_LIST_HEAD(&first.list);
  list_add_tail(&first.list, &mylinkedlist);
}

这code做工精细我。结果
我建议你​​阅读Linux内核开发第6章,它解释这很好。

this code work fine for me.
I suggest you read Linux Kernel Development chapter 6, it explain this very well.

这篇关于Linux内核链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆