<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML ><HEAD ><TITLE >Hello World (part 3): The __init and __exit Macros</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="The Linux Kernel Module Programming Guide" HREF="index.html"><LINK REL="UP" TITLE="Hello World" HREF="c147.html"><LINK REL="PREVIOUS" TITLE="Hello World (part 2)" HREF="hello2.html"><LINK REL="NEXT" TITLE="Hello World (part 4): Licensing and Module Documentation" HREF="x298.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >The Linux Kernel Module Programming Guide</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="hello2.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 2. Hello World</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x298.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN264" ></A >2.4. Hello World (part 3): The <TT CLASS="LITERAL" >__init</TT > and <TT CLASS="LITERAL" >__exit</TT > Macros</H1 ><P >This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The <TT CLASS="FUNCTION" >__init</TT > macro causes the init function to be discarded and its memory freed once the init function finishes for built-in drivers, but not loadable modules. If you think about when the init function is invoked, this makes perfect sense.</P ><P >There is also an <TT CLASS="FUNCTION" >__initdata</TT > which works similarly to <TT CLASS="FUNCTION" >__init</TT > but for init variables rather than functions.</P ><P >The <TT CLASS="FUNCTION" >__exit</TT > macro causes the omission of the function when the module is built into the kernel, and like <TT CLASS="FUNCTION" >__exit</TT >, has no effect for loadable modules. Again, if you consider when the cleanup function runs, this makes complete sense; built-in drivers don't need a cleanup function, while loadable modules do.</P ><P >These macros are defined in <TT CLASS="FILENAME" >linux/init.h</TT > and serve to free up kernel memory. When you boot your kernel and see something like <TT CLASS="LITERAL" >Freeing unused kernel memory: 236k freed</TT >, this is precisely what the kernel is freeing.</P ><DIV CLASS="EXAMPLE" ><A NAME="AEN294" ></A ><P ><B >Example 2-5. hello-3.c</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >/* * hello-3.c - Illustrating the __init, __initdata and __exit macros. */ #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);</PRE ></FONT ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="hello2.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x298.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Hello World (part 2)</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c147.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Hello World (part 4): Licensing and Module Documentation</TD ></TR ></TABLE ></DIV ></BODY ></HTML >