模块A首先加载并导出许多符号,供模块B或C使用.然后,模块B或C随后被加载并且符号存在供其使用.
但是,我现在发现模块A需要来自模块B或C的符号,但仅在运行时期间,而不需要初始化模块.所以当A加载时,它发现该符号尚不存在.我甚至在模块A中将符号标记为extern,但这也没有用.
是否可以在加载模块A后延迟加载符号,但在加载B或C之前它仍然不存在?
解决方法 这种情况通常使用回调来解决.假设模块A导出函数以注册/取消注册回调. B和/或C使用这些函数并向A提供适当的回调.当需要时,A检查回调是否已设置并调用它们.
像这样的东西(为简单起见,没有错误处理和锁定):
/* Module A */struct a_ops /* Better to define struct a_ops in a header file */{ voID (*needed_func)(voID); voID (*another_needed_func)(voID);}; ...struct a_ops ops = { .needed_func = NulL; .another_needed_func = NulL;};...int a_register_needed_funcs(struct a_ops *a_ops){ ops.needed_func = a_ops->needed_func; ops.another_needed_func = a_ops->another_needed_func;}EXPORT_SYMBol(a_register_needed_funcs);voID a_unregister_needed_funcs(){ ops.needed_func = NulL; ops.another_needed_func = NulL;}EXPORT_SYMBol(a_unregister_needed_funcs);.../* Call the specifIEd callbacks when needed: */voID do_something(voID){ if (ops.needed_func != NulL) { ops.needed_func(); } else { /* the callback is not set,handle this: report error,ignore it or * do something else */ ... }}.../* Modules B and C *//* Their code #includes the file where struct a_ops is defined. * The module registers the callbacks,for example,in its init function * and unregister in exit function. */...static voID func(voID){ ...}static voID another_func(voID){ ...}struct a_ops my_funcs = { .needed_func = func; .another_needed_func = another_func;};int __init my_module_init(voID){ ... result = a_register_needed_funcs(&my_funcs); ... }voID __exit my_module_exit(voID){ ... a_unregister_needed_funcs(); ...}
这类似于文件 *** 作和内核中的许多其他回调 *** 作.假设用户想要读取由自定义驱动程序维护的字符设备.内核本身(确切地说是VFS)接收请求但不能自己处理它.它将请求转发给已为该设备注册其文件 *** 作回调的自定义驱动程序.反过来,驱动程序使用内核正确导出的函数,如cdev_add()等.
总结以上是内存溢出为你收集整理的linux-kernel – 可以延迟导出符号的加载吗?全部内容,希望文章能够帮你解决linux-kernel – 可以延迟导出符号的加载吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)