[Learning] Kernel Basics --01 some standard macros and files include relations

Start with the most simple macro definitions learn; familiarize yourself with some basic kernel type definitions, and code style

Today we start with the stddef.h file:

1、include/linux/stddef.h

 1 #ifndef _LINUX_STDDEF_H
 2 #define _LINUX_STDDEF_H
 3 
 4 #include <uapi/linux/stddef.h>
 5 
 6 
 7 #undef NULL
 8 #define NULL ((void *)0)
 9 
10 enum {
11     false    = 0,
12     true    = 1
13 };
14 
15 #undef offsetof
16 #ifdef __compiler_offsetof
17 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
18 #else
19 #define offsetof (the TYPE, MEMBER) ((size_t) & ((the TYPE *) 0) -> MEMBER)
 20  #endif 
21  #endif 
// we will notice that some of the basic features, such as
// 1, before the first macro definition there #undef, this may prevent some redefined Represents warnings;
// 2, we know the values of two variables corresponding bool, false is 0, true is 1;
// structure members 3 and 19 relative to the line is defined classic, first cast
((the tYPE *) 0) , when to take the address of a structure member relative position has been offset; is a value obtained by converting the last type size_t;
// 4, using conditional compilation offsetof macro to define the situation, if there is a built-in __builtin_offsetof gcc defined macro __compiler_offsetof, so long the latter to define this offsetof;
 
  

 

2, the above document clearly calls include / uapi / linux / stddef.h this document, the contents of the file:

#include <linux/compiler.h>

3, in include / linux / compiler.h file in accordance with the conditions will eventually call other gcc compiler related definitions:

__GNUC__ #ifdef 
#include <Linux / Compiler-gcc.h>
 #endif 
// compiler-related definitions follow us to analyze more, and let us sort out what today calls related to the relationship;

4, calls the appropriate version of the linux / compiler-gcc.h according to a macro definitions:

__GNUC__ below the corresponding version of the call, such as: linux / compiler-gcc4.h

#Define 97 __gcc_header (X) #x is
 98 #define _gcc_header (X) __gcc_header (Linux / Compiler ## XH-GCC)
 99 #define gcc_header (X) _gcc_header (X) 
100 #include gcc_header (__ GNUC__) 
//. 1, 97 definition line conversion parameters corresponding to a string! For example:
__gcc_header (Linux) is obtained "Linux"
// 2, line ## represents the parameter 98 is connected, if x is 4 to give Linux / Compiler-gcc4.h
//. 3, line 100 using the value of __GNUC__, #include came in the appropriate file, such as: #include "linux / compiler-gcc4.h "

5, linux / compiler-gcc4.h has some specific definitions, macro definitions such as the above-mentioned __compiler_offsetof:

#define __compiler_offsetof(a,b) __builtin_offsetof(a,b)

First I write to you today, we continue to analyze in detail the definition of the relevant header file compiler in the back :)

 

Reproduced in: https: //www.cnblogs.com/QuLory/archive/2013/04/10/3011564.html

Guess you like

Origin blog.csdn.net/weixin_33915554/article/details/93154360