Macro learning in kernel

Source: Internet
Author: User
The macro learning in kernel-general Linux technology-Linux programming and kernel information. The following is a detailed description. Check the macro of the variable type obtained from the kernel.

First implementation:
/*
* Check at compile time that something is of a particle type.
* Always evaluates to 1 so you may use it easily in comparisons.
*/
# Define typecheck (type, x )\
({Type _ dummy ;\
Typeof (x) _ dummy2 ;\
(Void) (& __ dummy ==&__ dummy2 );\
1 ;\
})


Second Implementation:

# Define TYPE_EQUAL (n, type )\
_ Builtin_types_compatible_p (typeof (n), type *)

------------

See the example in the kernel:
About montavista-kernel-2.6.18_pro500

# Define PICK_OP (op, lock )\
Do {\
If (TYPE_EQUAL (lock), raw_spinlock_t ))\
_ Spin # op (raw_spinlock_t *) (lock ));\
Else if (TYPE_EQUAL (lock, spinlock_t ))\
_ Spin # op (spinlock_t *) (lock ));\
Else _ bad_spinlock_type ();\
} While (0)

# Define spin_lock_irq (lock) PICK_OP (_ lock_irq, lock)
# Define spin_lock_bh (lock) PICK_OP (_ lock_bh, lock)

It is not well verified in the kernel:

I wrote a simple program to play:
GNU's extension syntax is really strong. It must be more powerful than ansi c.

Function Requirement: implement a general printing function to print all the variables.


# Include
# Include

# Define TYPE_EQUAL (n, type )\
_ Builtin_types_compatible_p (typeof (n), type *)


# Define PICK_OP_PRINT (op, n )\
Do {\
If (TYPE_EQUAL (n), int ))\
Printf ("int number = % d \ n", * n );\
Else if (TYPE_EQUAL (n, long ))\
Printf ("long number = % ld \ n", * n );\
Else \
Printf ("error \ n ");\
} While (0)

# Define printtype (n) PICK_OP_PRINT (op, n)


Int main (void)
{
Int a = 20;
Printtype (& );

Long B = 100000000;
Printtype (& B );

Float c = 3.6L;
Printf ("float c = % f \ n", c );
Printtype (& c );

Return 0;
}

The output result is as follows:
[Root @ bobzhanglinux debug] # gcc spinlock. c
[Root @ bobzhanglinux debug] #./a. out
Int number = 20
Long number = 100000000
Flowat c = 3.600000
Error
[Root @ bobzhanglinux debug] #

We often see the definition macro in the kernel code. The format is as follows:

# Define A ({a}) is generally used to define class function macros. In this way, you can add these statement bodies and then return some values,
Finally, do not return, never see the words "return. We do not need to go into details. Just write some commonly used class function macros.

For example:

# Define PICK_OP_PRINT (op, n )\
({\
If (TYPE_EQUAL (n), int ))\
Printf ("int number = % d \ n", * n );\
Else if (TYPE_EQUAL (n, long ))\
Printf ("long number = % ld \ n", * n );\
Else \
Printf ("error \ n ");\
})


Like the above class FUNCTION macro, you can also write it like this:

# Define PICK_OP_PRINT (op, n )\
Do {\
If (TYPE_EQUAL (n), int ))\
Printf ("int number = % d \ n", * n );\
Else if (TYPE_EQUAL (n, long ))\
Printf ("long number = % ld \ n", * n );\
Else \
Printf ("error \ n ");\
} While (0)

The above situations are suitable for situations where no return value is required.
--------------------

If you want to return a value, I can only use ({})
Do {} while (0) cannot be used, which is more suitable for writing class function macros.

----

For example, in the following example, only ({}) can be used.
To return a value:
# Define PICK_OP2_RET (op, lock, flags )\
({\
Unsigned long _ ret ;\
\
If (TYPE_EQUAL (lock), raw_spinlock_t ))\
_ Ret = _ spin # op (raw_spinlock_t *) (lock), flags );\
Else if (TYPE_EQUAL (lock, spinlock_t ))\
_ Ret = _ spin # op (spinlock_t *) (lock), flags );\
Else _ bad_spinlock_type ();\
\
_ Ret ;\
})
Related Article

Contact Us

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.