# Min and Max functions in Linux Kernel

Source: Internet
Author: User
These days, in order to sort out the driver knowledge that ldd3 learned some time ago, I went to look at the USB driver. I don't know, but I was shocked. There are a lot of syntaxes in it, which I found to be great at using. It's not like writing code as we usually do. The code in it is really good. However, it is not easy to understand what is inside. When I saw the usb_stor_access_xfer_buf function today, I wanted to see if the min function was written according to what we wrote. But it's silly. I haven't used some other things yet, so I need to Google it. What is the code below? You can go to, include/Linux/kernel. h to find it. Don't look for something, so I'll paste it here. /*

* Min ()/MAX () macros that also do

* Strict type-checking .. see

* "Unnecessary" pointer comparison.

*/

# Define min (x, y )({\

Typeof (x) _ x = (x );\

Typeof (y) _ y = (y );\

(Void) (& _ x = & _ y );\

_ X <_ y? _ X: _ y ;}) # define max (x, y )({\

Typeof (x) _ x = (x );\

Typeof (y) _ y = (y );\

(Void) (& _ x = & _ y );\

_ X> _ y? _ X: _ y;}) When I Google this function, many people have read it. It seems that I am out. This is all due to University accounts. The so-called "getting out and mixing will be paid back sooner or later." Fortunately, I started learning today. Let's take a look at a brother's understanding. He spoke very well. Http://blog.chinaunix.net/u/24474/showart_1101969.html min macro definition Exploration

 We recommend that you use the following min macro definition in the GCC document:Reference: # define min (x, y )\(_ Extension __\({\Typeof (x) _ x = (x), _ y = (y );\(_ X <__ y )? _ X :__ y ;\})\)This article discusses the significance of this practice.1. Side effects of traditional min2. ({Statement List}) Extension in GCC3. typeof (expression)4. _ extension _5. Use typeof and ({}) to implement min, avoiding side effectsAppendix 1. Solutions in the earlier version of GCCAppendix 2. How to Use template in C ++1. Side effects of traditional minMin is usually defined as a macro like this:# Define min (x, y) (x) <(y )? (X): (y ))This definition has some side effects. For more information, see the following example:Int x = 1, y = 2;Int main (){Printf ("min = % d \ n", min (x ++, y ++ ));Printf ("x = % d, y = % d \ n", x, y );}After the execution of min (x ++, y ++), we expect the value of X to be 2, and the value of Y to be 3.However, the actual result is that after the mini (x ++, y ++) is executed, the value of X is 3, and the value of Y is 3, the reason is that X ++ was executed twice after the macro scale was launched:Reference:Int x = 1, y = 2;Int main (){Printf ("min = % d \ n", X ++ ?, ? Returns a larger value of the two operands. The min defined by the two operators is as follows:# Define min (x, y) (x) ? (Y ))However, the new GCC document states that these two operators are outdated and we recommend that you do not use them.Appendix 2. How to Use template in C ++Template Type min (type A, type B){Return a _ y? _ X: _ y ;})

Min and Max macros:

/*

* Min ()/MAX () macros that also do

* Strict type-checking .. see

* "Unnecessary" pointer comparison.

*/

# Define min (x, y) ({typeof (x) _ x = (x); typeof (y) _ y = (y); (void) (& _ x = & _ y); _ x <_ y? _ X: _ y ;})

# Define max (x, y) ({typeof (x) _ x = (x); typeof (y) _ y = (y); (void) (& _ x = & _ y); _ x> _ y? _ X: _ y ;})

/*

*. And if you can't take the strict

* Types, you can specify one yourself.

*

* Or not use min/max at all, of course.

*/

# Define min_t (type, x, y) ({type _ x = (x); Type _ y = (y); _ x <_ y? _ X: _ y ;})

# Define max_t (type, x, y) ({type _ x = (x); Type _ y = (y); _ x> _ y? _ X: _ y ;})

It doesn't feel a little different from what we use:

(Void) (& _ x = & _ y );

(Void) (& _ x = & _ y) The statement itself is a nonsense in terms of program execution. Its role is, in itself, we cannot perform this operation typeof (_ x) = typeof (_ y). Therefore, it is obviously impossible to determine whether the two address pointers are equal, however, if the _ x and _ y types are different, the pointer types will be different. If two different pointer types are compared, a compilation warning will be thrown. That is to say char * P; int * q; then p = Q;, this judgment will generate a warning during compilation because one is char * and the other is int. Clever here.

Because the kernel is developed together by many developers, there are some other implementations, just like what we usually use:

# Define min (A, B) (a) <(B ))? (A): (B ))

Imagine:

Min (++ A, ++ B) ==> (++ A) <(++ B ))? (++ A) :( ++ B)

Is there a problem? The input parameter is added twice.
Well, I just collected some other people's things. Do you understand the ({Statement List}) extensions in GCC now? It should be basically understandable. Then work hard, the road is long and its repair distance, the original address: http://blog.chinaunix.net/uid-23247212-id-99921.html
Related Keywords:

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

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.