x86/mm: declare static variable inside a function instead of global
PATCH 1/1 x86/mm: declare static variable inside a function instead of global
Global variables are global capacity variables, unless they are shadowed, they are available to the entire program.
To reduce the scope of a variable as much as possible is always good practice.
--- arch/x86/mm/init_64.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c +2 -1
@@ -1515,9 +1515,10 @@ static unsigned long probe_memory_block_size(void)return bz; } -static unsigned long memory_block_size_probed;unsigned long memory_block_size_bytes (void ) { +static unsigned long memory_block_size_probed; +if (!memory_block_size_probed) memory_block_size_probed =probe_memory_block_size ();
Signature · 1 line
2.36.0
Andy Lutomirski
maintainerQuoted 2 lines · Fanjun Kong
Global variables are global capacity variables, unless they are shadowed, they are available to the entire program.
This is true in general, but:
Quoted 1 line
-static unsigned long
memory_block_size_probed;
^^^^^^
So this isn't actually the case.
arch/x86/mm/init_64.c static unsigned long probe_memory_block_size(void) · line 1523
Quoted 7 lines
unsigned long memory_block_size_bytes(void) {arch/x86/mm/init_64.c +2
-0+static unsigned long memory_block_size_probed; +if (!memory_block_size_probed) memory_block_size_probed =probe_memory_block_size ();
I'm sort of okay with the patch, but it's worth noting that, in C++, initialized function-scope static variables have quite surprising semantics, so this type of change isn't a pure win in my book. (Yes, the kernel is C, not C++. But C++ programmers may be nervous anyway.)