Linux kernel source code study 1-Linux general technology-Linux programming and kernel information. The following is a detailed description. The Linux kernel is mainly written in gnu c language. GNU provides the gcc compilation tool for this purpose.
I. Extensive use of inline functions:
Gcc absorbs "inline" and "const" from the C ++ language ". In fact, gnu c and C ++ are integrated. gcc is both C compilation and C ++ compilation, therefore, it is natural to absorb something from C ++ into C. In terms of function, the inline function is similar to the # define macro definition, but is more independent and secure. Using the inline function is also conducive to program debugging. If optimization is not added during compilation, these inline functions are common and independent functions, making debugging easier. After debugging, Use Optimization and re-compile. These inline functions are incorporated into the reference code just like macro operations, which improves the running efficiency. Thanks to the extensive use of inline functions, a considerable amount of code is moved from the. c file to the. h file.
Ii. Strange macro operation definition:
Linux Kernel code uses a large number of inline functions, but this does not eliminate the use of macro operations, there are still many macro operation definitions in the kernel. I am often confused about the definition of some macro operations in the kernel code. Let's first look at an instance, from fs/proc/kcore. c:
163 # define DUMP_WRITE (add, nr) do {memcpy (bufp, addr, nr); buf + = nr;} while (0)
This loop body is executed only once. Why is this defined through a do-while loop? First, can it be defined as follows:
No. If a program references this macro operation in an if statement, a problem occurs:
If (add)
DUMP_WRITE (addr, nr );
Else
Do_something_else ();
After preprocessing, this code will become like this:
If (add)
Memcpy (bufp, addr, nr); buf + = nr;
Else
Do_something_else ();
Compiling this code gcc will fail and a syntax error is reported. Because gcc considers that the if statement ends after memcpy (), but then encounters an else. If you change DUMP_WRITE (addr, nr) and Do_something_else () to another location, the compilation is successful, but the problem is more serious, because no matter whether the conditions are met or not, bufp + = nr will be executed. I would like to add curly braces to the definition as follows:
However, the above program is not compiled, because after preprocessing, it becomes like this:
If (add)
{Memcpy (bufp, addr, nr); buf + = nr ;};
Else
Do_something_else ();
Similarly, when gcc encounters the ";" before else, it considers that the if statement has ended, so the else following it is not in the if statement. In contrast, the use of the do-while definition is fine under any circumstances.
Iii. Queue usage:
Queue and queue operations are widely used in the kernel.
If we have a Data Structure foo and need to maintain a dual-chain queue of this data structure, the simplest and most common method is to add two pointers to the Type Definition of the data structure. For example:
Typedef struct foo
{
Struct foo * prev;
Struct foo * next;
......
} Foo_t;
Then, write a set of subprograms for various queue operations for this data structure. Since the two pointers used to maintain the queue are fixed (all pointing to the foo data structure), these subroutines cannot be used for queue operations in other data structures. In other words, the number of queues in which data structures need to be maintained requires a set of queue operation subprograms. Applications that use fewer queues may not be a big problem, but the kernel that uses a large number of queues becomes a problem. Therefore, the Linux kernel uses a set of usable, General, and can use a variety of queue operations with different data structures. To this end, the authors of the Code abstract the pointers prev and next from the specific "host" data structure into a data structure list_head, this data structure can be "hosted" inside a specific host structure and become a "connector" for the data structure. It can also exist independently and become the header of a queue. This data structure is defined in include/linux/list. h.
16 struct list_head {
17 struct list_head * next, * prev;
18 };
If you need a queue with a certain data structure, put a list_head data structure in this structure. Take the page data structure for Memory page Management as an example. Its definition is as follows: (See include/linux/mm. h)
134 typedef struct page {
135 struct list_head list;
......
138 struct page * next_hash;
......
141 struct list_head lru;
......
148} mem_map_t;
It can be seen that two list_head structures are hosted in the page data structure, or there are connectors for two queue operations, so the page structure can exist in two double-stranded queues at the same time. In addition, there is a single-chain pointer next_hash in the structure to maintain a single-chain collection queue.
Each list_head data structure in the host data structure must be initialized. You can use a macro to operate INIT_LIST_HEAD, you can use list_add () to link a page structure to a queue through its "queue Header (). Use list_del () to chain from the queue ().
But there is a problem here: queue operations are all performed through list_head, but it is just a connector. If we have a host structure in our hands, of course we will know that one of its list_head is there, so we can call list_add () or list_del () as a parameter. But, in turn, how can we find its host structure when we get one of the list_head structures along a queue? There is no pointer to the host structure in the list_head structure. After all, what we really care about is the host structure, not the connector.
The following example shows how this problem is solved. The following code is taken from mm/page_alloc.c:
[Rmqueue ()]
188 page = memlist_entry (curr, struct page, list );
Here memlist_entry () converts a list_head pointer curr into the starting address of its host structure, that is, the pointer to its host structure is obtained. How is memlist_entry () implemented? Because the called parameter page is a type, rather than the specific data. If you look at the entire code of the rmqueue () function, you can find that the list is undefined.
In fact, memlist_entry is defined as list_entry in the same file, so the actual reference is list_entry ():
48 # define memlist_entry list_entry
The list_entry is defined in include/linux/list. h:
135 /**
136 * list_entry: get the struct for this entry
137 * @ ptr: the & struct list_head pointer
138 * @ type: the type of the struct this is embedded in
139 * @ member: the name of the list_struct within the struct
In contrast to the preceding 188 lines, we can see the mysteries of this line: After the C-preprocessing text is replaced, the content of this line becomes:
The curr here is the address of the list of components inside the page structure, but what we need is the address of the page structure itself, so we need to subtract a displacement from the address curr, that is, the displacement of the component list within the page can meet the requirements. So what is the displacement? & (Struct page *) 0)-> list indicates the address of its component list when the structure page is on address 0, which is the displacement. Similarly, if it is in the lru queue of the page structure, the transmitted member is lru, and the address of the host structure can be calculated.
It can be seen that this set of operations is both widely applied and highly efficient. However, there is a drawback for those who read the code, that is, it is not easy to see from the code what the host structure of a list_head is, in the past, you only had to look at the type of next.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service