1.
Block "unused Parameters" Warning
This macro is used in ob_define.h in oceanbase to forcibly convert incoming data to void type.
#ifndef UNUSED #define UNUSED(v) ((void)(v)) #endif
This macro is mainly used to block the warning of "unused Parameters". For example, if the following function uses two parameters but none of them are used, some compilers will report a warning: P and mod_id are not used. All we useUNUSEMacro. In this way, both parameters are used, but no effective action is actually performed to block the "unused Parameters" Warning.
virtual void mod_free(void* p, const int32_t mod_id = 0) { UNUSED(p); UNUSED(mod_id); }
2.
Specify byte alignment
By default, the compiler allocates space for each variable or data unit based on its natural limitations. Generally, you can use the following method to change the default peer condition:
1) Use pseudoinstructions#pragma pack (n), The compiler will be aligned according to n Bytes.
2) use pseudoinstructions#pragma pack ()To cancel the custom byte alignment.
In addition, the following method is provided:
3)__attribute__((aligned (n)))To align the structure members to the natural boundary of n Bytes. If the length of a member in the structure is greater than N, the maximum member length is used for alignment.
4)__attribute__ ((packed))Cancels the optimization alignment of the structure during compilation and alignment according to the actual number of bytes occupied.
The first method above n = 1, 2, 4, 8, 16... is more common.
#define CACHE_ALIGNED __attribute__((aligned(CACHE_ALIGN_SIZE)))
3.
Variable Parameter macro
We specify that some function parameters are variable. For example, after the printf function is specified as the first formatted string, it can be followed by parameters with an indefinite number. Similarly, macros can also specify multiple parameters. There are two ways to do this.
First, useargs...And##argsSecond, use...And__VA_ARGS__. The following two macros are defined in two ways. Both macros can output a string before the printf string to achieve the same effect.
#define P_Debug(format,args...) { printf("[DeBug]"); printf(format,##args);}while(false)
#define P_Release(format,...) \do{ printf("[Release]"); printf(format,__VA_ARGS__);}while(false)
When used:
P_Debug("%s:%d\n","good",100);P_Release("%s:%d\n","good",100);
4.
#,
##,
#@Difference
Assume that three macros are defined,
#define A(x) T_##x#define B(x) #@x#define C(x) #x
Then
A (1) ------> T_1
B (1) ------> '1'
C (1) ------> "1"
Welcome to my website-Butterfly's blog garden-a column with no name.
If you have any questions during reading this article, please contact the author. For more information, see the source!