这演示了内核 2.2 及更高版本的一个特性。请注意 init 和 cleanup 函数定义中的变化。__init__init宏会导致 init 函数在内置驱动程序的 init 函数完成后被丢弃并释放其内存,但对于可加载模块则不然。如果您考虑 init 函数何时被调用,这完全合理。
还有一个__initdata宏,其工作方式类似于__init__init,但用于 init 变量而不是函数。
__exit__exit宏会导致在将模块构建到内核中时省略该函数,并且像__exit__init 一样,对可加载模块没有影响。同样,如果您考虑 cleanup 函数何时运行,这完全合理;内置驱动程序不需要 cleanup 函数,而可加载模块需要。
这些宏在linux/init.h中定义,用于释放内核内存。当您启动内核并看到类似Freeing unused kernel memory: 236k freed (释放未使用的内核内存:已释放 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");
顺便说一句,您可能会在为 Linux 2.2 内核编写的驱动程序中看到指令“__initfunction()”。
__initfunction(int init_module(void)) { printk(KERN_ALERT "Hi there.\n"); return 0; }
这个宏与__init__init 的目的相同,但现在已被强烈弃用,转而使用__init__init。我提到它只是因为您可能会在现代内核中看到它。截至 2.4.18 版本,有 38 个引用__initfunction()__initfunction() 的地方,而在 2.4.20 版本中,有 37 个引用。但是,请不要在您自己的代码中使用它。