I have a doubt about a syntax used in linux kernel code. I have an intuition of what it does but I want to know it more formally. I am using kernel v3.5.4
In file /include/linux/sched.h the following is defined
struct task_struct {
volatile long state;
//some more data members
};
and in file /include/linux/init_task.h file the following is defined:
#define INIT_TASK(tsk) {
.state = 0, \
//some more initializations
}
I am confused about two things:
a) I feel it is used for initialization but can anyone suggest some good read for this type of initialization for structures.
b) I do not understand how the following initialization works. Like how this #define and the corresponding task_struct structure are related.
[EDIT]
I noticed the following things also:
c) Is \ at the end of every line necessary.
d) There are many parts of kernel doe wrapped in #ifdef #endif. If you want to initialize a data member wrapped in #ifdef #endif can we use this form of initialization. I mean can we use #ifdef #endif inside INIT_TASK() like this
#define INIT_TASK(tsk) {
.state = 0, \
//some more initializations
#ifdef CX
.tickets = 5, \
#endif
}
This results in the following code:
which is valid C syntax to initialize fields in a struct via their name instead of their position. Doing so makes the code safe against struct members that are not added at the last position.
Regarding the backslashes: Yes, they are necessary so the preprocessor knows that the macro continues on the next line.
No, you cannot use
#ifdefinside a macro.