Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Question about macro __init
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Geminias




PostPosted: Wed Aug 08, 2007 6:44 am   Post subject: Question about macro __init

Please clear up what __init exactly is and how it works. It is described as: The __init 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. Which I perfectly understand but do not understand how it works.
I looked it up in init.h only to become more confused. I found this:
code:
#define __init  __attribute__ ((__section__ (".init.text")))




I'm mainly interested in __init because I've never seen syntax like:

storage_class return_type macro function()

I also I don't really know what static does. I think it is different from the C++ static keyword... right?

code:

#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int __init hello_init(void) {
  printk(KERN_ALERT "Hello\n");
  return 0;
}

static void  __exit hello_exit(void) {
  printk(KERN_ALERT "Good bye.\n");
}

module_init(hello_init);
module_exit(hello_exit);
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Thu Aug 09, 2007 9:39 am   Post subject: Re: Question about macro __init

Geminias @ Wed Aug 08, 2007 6:44 am wrote:
I'm mainly interested in __init because I've never seen syntax like:

storage_class return_type macro function()
Nothing special... the macro could have been an obfuscated part of the return type like "unsigned". What you probably want to know if what the GNU extension for function attributes is. __attribute__((__section__(".init.text") )) causes the function to go into a special section of the compiled object. The linker puts it into some (I guess special) segment, and the loader does something special with it so that you get the behaviour you described. So go hunt down what .init.text is.

Quote:
I also I don't really know what static does. I think it is different from the C++ static keyword... right?
Well, C++ lets you use "static" this way too. It makes it so that the function can only be referenced by name in this source file. Other source files can have functions of the same name, but they won't be this function. It affects the linkage of the function.
Geminias




PostPosted: Fri Aug 10, 2007 5:34 am   Post subject: Re: Question about macro __init

But C++ adds a new definition to static for when you use it as a member variable of a class. I believe in this case it means the variable can be used without creating an object of that class. Since C doesn't support classes I am guessing this has no relevance in C.

Aside from that, thanks for clarifiying __attribute__, I just realized that I actually knew what it was from a previous encounter that just unrooted itself in my brain as I read your comment.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: