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

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

问题描述

我正在尝试使用 Linux 内核链表实现,但我无法编译.我完全按照这些来源没有结果(http://www.roman10.net/linux-kernel-programminglinked-list/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_HEAD_INIT 的 list.h 内核宏如下:

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_INITstruct Node 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).

所以完整的代码应该是:

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);
}

这段代码对我来说很好用.
我建议你阅读 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天全站免登陆