Friday, January 04, 2008

First notes about Minix Source code.

I hope not to loose the path with this post, it is about Minix Source code which i am studying for curiosity, but would also be applicable to Linux kernel source code. It is about some conventions used while coding header files in C.

In some headers i find this:
PUBLIC char *t_stack[TOT_STACK_SPACE / sizeof(char *)];
PRIVATE int mini_send() {...};
EXTERN struct proc proc[NR_TASKS + NR_PROCS];


These words are not from C, they are macros declared in a header src/include/minix/const.h file, like this:
#define EXTERN extern /* used in *.h files */
#define PRIVATE static /* PRIVATE x limits the scope of x */
#define PUBLIC /* PUBLIC is the opposite of PRIVATE */


The extern keyword
C permits to link together various modules already compiled, for better management possibilities and time reduction in big projects. The global variables have to be declared just one time. So, a solution is to declare all the global variables in one of the files, and in the other files where this variable is required, use the "extern" declaration. The "extern" work gives the compiler the information to know the types and names of the global variables, without creating a new storage.

The static keyword
The static variables have different effects when they are local or global.
Static local variables:
When the static modifier is applied to a local variable, the compiler creates a permanent storage for it. The difference between a local static variable and a global one, is that the local static variable is only known in the block in which it is declared. It is a variable that whoes value remains between the function calls.
Example:
adding (void ) {
static int age;
age = age + 1;
return age;
}

The variable age keeps existing between calls to the function, which returns always a new number.
Static global variables
This variables are only known in the file where they where declared, this means that though the variable is global, the other files are not going to see it, nor modify its content directly.

0 comentarios: