Reputation: 1100
Why does the Linux kernel sometimes implement multiple versions of a function with very similar names that just wrap another function? For example, here:
static void clocksource_select(void)
{
__clocksource_select(false);
}
static void clocksource_select_fallback(void)
{
__clocksource_select(true);
}
Upvotes: 1
Views: 141
Reputation: 8573
The example you gave is not a very good example, because it has nothing to do with the Linux kernel. This is just basic software engineering.
When you have two functions that need to have very close functionality, there are several paths you can take.
You can implement the function twice. We don't like to do that, as it creates code duplication. It also means that if you need to change something in the common area of the code, you need to remember to change it at two places.
You can split the common code into its own function, and call that function from each of the functions. That is the best solution if it is possible. The problem is that it is not always possible. It might not be possible because the common code needs too much context, or because it needs to be spread out across the function. Which brings us right to:
Create an internal "common" function, with an argument telling which functionality to provide. Just write the code, and put an if
where the two functions need to do something different. That is the path the kernel took in your example.
With that said, there is another case, specific to the Linux kernel, where two functions really do seem to be almost identical. On the i386 platform, the stat
system call is implemented not twice, but three times:
oldstat
syscall number 18stat
syscall number 106stat64
syscall number 195The reason for that is that the Linux kernel promises complete backwards compatibility over its user space kernel interface. When a function has to be superseded for some reason, as happened to stat
not once, but twice (three times if you count fstatat
), the old system call entry needs to be kept around and remain operational.
If you look at the actual implementation, however, you will notice that there is very little difference between them, and they all end up calling, pretty much, the same function.
Upvotes: 2