Linux API 揭秘

【Linux API 揭秘】module_init与module_exit

Nov 22, 2023
本文阅读量
Tech
Linux API 揭秘

【Linux API 揭秘】module_init与module_exit # Linux Version:6.6 Author:Donge Github:linux-api-insides 1、函数作用 # module_init和module_exit是驱动中最常用的两个接口,主要用来注册、注销设备驱动程序。 并且这两个接口的实现机制是一样的,我们先以module_init为切入点分析。 2、module_init函数解析 # 2.1 module_init # #ifndef MODULE /** * module_init() - driver initialization entry point * @x: function to be run at kernel boot time or module insertion * * module_init() will either be called during do_initcalls() (if * builtin) or at module insertion time (if a module). There can only * be one per module. */ #define module_init(x) __initcall(x); . ...

【Linux API 揭秘】container_of函数详解

Dec 13, 2023
本文阅读量
Tech
Linux API 揭秘

【Linux API 揭秘】container_of函数详解 # Linux Version:6.6 Author:Donge Github:linux-api-insides 1、container_of函数介绍 # container_of可以说是内核中使用最为频繁的一个函数了,简单来说,它的主要作用就是根据我们结构体中的已知的成员变量的地址,来寻求该结构体的首地址,直接看图,更容易理解。 下面我们看看linux是如何实现的吧 2、container_of函数实现 # /** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. * * WARNING: any const qualifier of @ptr is lost. */ #define container_of(ptr, type, member) ({ \ void *__mptr = (void *)(ptr); \ static_assert(__same_type(*(ptr), ((type *)0)->member) || \ __same_type(*(ptr), void), \ "pointer type mismatch in container_of()"); \ ((type *)(__mptr - offsetof(type, member))); }) 函数名称:container_of ...