这演示了 2.2 及更高版本内核的一个特性。请注意 init 和 cleanup 函数定义的更改。这个__init宏导致 init 函数在内置驱动程序的 init 函数完成后被丢弃并释放其内存,但对于可加载模块则不然。 如果你考虑一下 init 函数何时被调用,这完全说得通。
还有一个__initdata其工作方式类似于__init但用于 init 变量而不是函数。
这个__exit宏导致当模块被构建到内核中时,该函数被省略,并且像__exit, 对可加载模块没有影响。 同样,如果你考虑一下 cleanup 函数何时运行,这完全说得通; 内置驱动程序不需要 cleanup 函数,而可加载模块需要。
这些宏定义在linux/init.h并用于释放内核内存。 当你启动内核并看到类似如下内容时正在释放未使用的内核内存:236k 已释放,这正是内核正在释放的内容。
示例 2-5. hello-3.c
/* hello-3.c - Illustrating the __init, __initdata and __exit macros. * * Copyright (C) 2001 by Peter Jay Salzman * * 08/02/2006 - Updated by Rodrigo Rubira Branco <rodrigo@kernelhacking.com> */ /* Kernel Programming */ #define MODULE #define LINUX #define __KERNEL__ #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ #include <linux/init.h> /* Needed for the macros */ static int hello3_data __initdata = 3; static int __init hello_3_init(void) { printk(KERN_ALERT "Hello, world %d\n", hello3_data); return 0; } static void __exit hello_3_exit(void) { printk(KERN_ALERT "Goodbye, world 3\n"); } module_init(hello_3_init); module_exit(hello_3_exit); MODULE_LICENSE("GPL"); |
顺便说一句,你可能会看到指令“__initfunction()”在为 Linux 2.2 内核编写的驱动程序中
__initfunction(int init_module(void)) { printk(KERN_ALERT "Hi there.\n"); return 0; } |
这个宏的作用与__init相同,但现在已被强烈弃用,转而使用__init。 我提到它只是因为你可能会在现代内核中看到它。 截至 2.4.18,有 38 处引用__initfunction(),截至 2.4.20,有 37 处引用。 但是,不要在自己的代码中使用它。