我正在研究一个linux内核模块。
struct tcpsp_conn在头文件中定义如下:
struct tcpsp_conn {
...
struct timer_list timer; /* exp. timer*/
...
};
然后我声明一个指向结构的指针并尝试分配函数:
struct tcpsp_conn *cp;
cp->timer.function = tcpsp_conn_expire;
tcpsp_conn_expire函数的定义方式与内核的struct timer_list相同:
static void tcpsp_conn_expire(unsigned long data)
我不明白为什么我会收到此错误:错误:从不兼容的指针类型分配[-Werror = incompatible-pointer-types] cp-> timer.function = tcpsp_conn_expire;
它看起来没有类型问题。
你的tcpsp_conn_expire
函数的类型不同于.function
结构的timer_list
字段的类型。
在最新的内核中(从4.15开始),这个函数字段用struct timer_list *
参数而不是unsigned long
声明,如下所示:
struct timer_list {
...
void (*function)(struct timer_list *);
...
};
有了这样的参数,你可以使用宏struct tcpsp_conn
获得指向嵌入计时器的container_of
结构的指针。