Mastering the Essence of C Language through Redis Source Code Analysis

I want to reveal how Redis utilizes the features of the C language to achieve step-by-step design in this series. This article will first look at the global variable shared in Redis..The structure type of the shared variable, sharedObjectsStruct, is declared in the server.h header file, and from its name, it is clear that it is used for shared objects. This structure defines the following members in version 3.2 (later versions have added more), which are:Mastering the Essence of C Language through Redis Source Code AnalysisFour numeric macros are also defined in the server.h header file:Mastering the Essence of C Language through Redis Source Code AnalysisFrom this structure, we can learn the following points:1. Except for the four robj pointer arrays: select, integers, mbulkhdr, and bulkhdr, all other members are of robj pointer type. It can be observed that as long as the beginning has the robj type, the middle can be omitted, and only commas are needed to separate them. However, members with different type specifiers must be separated by semicolons.For example:Mastering the Essence of C Language through Redis Source Code AnalysisHere, i is of int type, and j is of int pointer type.2. The array lengths of the four robj pointer arrays: select, integers, mbulkhdr, and bulkhdr are defined using macros, rather than directly writing constant numbers. This approach improves the maintainability and flexibility of the code. Here is a summary of the core advantages of macro definitions as described by AI:1) Easy to modify uniformly: When the array size needs to be adjusted, you only need to modify the macro definition in one place, and all related code in the program will automatically update.2) Enhances code readability: Using meaningful macro names (like MAX_USERS) instead of numbers makes the code’s intent clearer.3) Reduces duplicate code: Avoid hardcoding the same array length value in multiple places.Now let’s talk about the global variable shared, which is defined at the top of the server.c source file:Mastering the Essence of C Language through Redis Source Code AnalysisIt can be seen that the sharedObjectsStruct has the struct keyword in front of it. If this keyword is removed, a compilation error will occur. However, if sharedObjectsStruct is declared using typedef, the struct keyword can be omitted.Why is shared considered a global variable? I quote the description of global variables from Chapter 12 on storage classes in C Primer Plus:The variable is defined outside of any function and has file scope. A variable with file scope is visible from its definition to the end of the file where it is defined. Consider the following example:Mastering the Essence of C Language through Redis Source Code AnalysisHere, the variable units has file scope, and both the main() and critic() functions can use it (more accurately, units has external linkage file scope). Since such variables can be used across multiple functions, file scope variables are also referred to as global variables.

Leave a Comment