InSource codeDirectory contains the source file. cpp and header file. H. We also found files with the extension. I and. inL. In fact, files with the. I and. INL extensions are stored as inline functions in ACE source code.
Before explaining why ace uses this method to store inline functions, let's look at the consciousness of inline keywords. We know that when a function is called, some operations such as the return address and parameter pressure Stack are involved. These operations are the overhead of the function call.
In the original CCodeGenerally, macro-defined functions are used to simulate functions to eliminate the overhead of function calls. Therefore, we know that macros are processed during pre-compilation. However, macro definitions also have many defects, which can easily lead to incorrect use. This is why the inline keyword was born.
A function defined with the inline keyword does not generate a real function during compilation, but directly expands the code at the function call, eliminating the overhead of the function call. Note that the inline keyword is just a hint to the compiler. It is determined by the compiler if inline processing is performed.
So why does ace use this special method to store inline letters? We provide the answer based on the instance.
Let's take a look at the processing at the end of reactor. h file:
# If defined (_ ace_inline __)
# Include "ACE/reactor. I"
# Endif/* _ ace_inline __*/
Take a look at the macro processing at the beginning of the reactor. cpp file:
# If! Defined (_ ace_inline __)
# Include "ACE/reactor. I"
# Endif/* _ ace_inline __*/
The above reactor. H, reactor. cpp and reactor. I files are related code of the ACE reactor framework. We can understand the macro definition above,If macro _ ace_inline __is defined in the header file, we will include the reactor. I file into the header file. If the macro _ ace_inline __is not specified, the reactor. I file will be included in the source file.In fact, with the introduction of the meaning of inline above, it is not difficult to understand why this method is used for processing.
Here we assume that the macro _ ace_inline __is defined, and the reactor. I file is included in the source file rather than in the include header file. What are the consequences? We know that the inline function will not generate a real function after compilation by the compiler. Therefore, if there are other source files, such as zhx. CPP, the reactor is called. the Inline Function in the I file will throw the error that the symbol cannot be parsed during connection, and if the reactor. the I file is included in the header file, and in zhx. in CPP, reactor is called. in the I file. in CPP, you only need to include reactor. the H header file is enough, then the reactor. the Inline Function of I is in zhx. CPP also performs code expansion. If macro _ ace_inline __is not specified, the reactor. I is included in the source file. There is no problem because reactor. the functions in I will generate real functions after compilation, rather than being processed by inline. This is why ace adopts this method.
The _ ace_inline _ switch is used to set whether the function in the INL that uses this switch is processed in the inline mode.
In the above introduction, we also found the disadvantage of inline, that is, it will cause code expansion. Therefore, not all functions can be defined using inline. Only short functions are suitable for inline processing.
Note: Why is ace used. I and. INL files with two extensions are used to store inline functions. I am not very clear, but I feel like. files stored in the INL form are the methods in the early ace code, and the later ace code is used. I Method to store inline functions, that is to say, this should be a historical problem ^ _ ^, the disadvantage of open-source projects.