Skip to contentSkip to conversation

Eliminating nested functions

DISCUSSION Pavel Roskin 6 participants 12 messages Apr 17, 2009 – Apr 19, 2009

lore
Share & tools
lore
#0 lore raw

Hello!

While I have just applied a patch adding NESTED_FUNC_ATTR to several functions as an emergency fix for a major breakage in ata, ohci, uhci and lspci modules, I would prefer a more radical solution.

I suggest that we eliminate all nested functions. The reasons are:

1) They make the code less readable, as they make the parent functions longer.

2) They have problems with some popular compilers, as recent as gcc-4.0 when regparm(3) is used.

3) We failed to implement a reliable test for such problems. We are using regparm(1) for all compilers.

4) The existing test is one of the obstacles making it impossible to compile without having libc for the target (x86_64->i386 would be really nice), as we need to run the compiled test executable.

5) Non-i386 architectures define NESTED_FUNC_ATTR as an empty symbol, so developers on such architectures don't see if they use it correctly.

6) NESTED_FUNC_ATTR tends to proliferate to the file scope functions, as it happened with grub_pci_iterate(). It only takes one caller using a nested function to force NESTED_FUNC_ATTR on all functions used as an argument to the same function.

We can give all formerly nested functions better, more descriptive names starting like other functions in the file and ending with "_iter".

Signature · 2 lines
Regards,
Pavel Roskin
#1 lore raw

Hi,

One of the advantage of nested function is to use local variables. Without it, we would need to pass them as global variable, or add custom data pointer in many of the iterate function, which would make the code a lot uglier IMO.

Quoted 42 lines · Pavel Roskin

Hello!

While I have just applied a patch adding NESTED_FUNC_ATTR to several functions as an emergency fix for a major breakage in ata, ohci, uhci and lspci modules, I would prefer a more radical solution.

I suggest that we eliminate all nested functions.  The reasons are:

1) They make the code less readable, as they make the parent functions longer.

2) They have problems with some popular compilers, as recent as gcc-4.0 when regparm(3) is used.

3) We failed to implement a reliable test for such problems.  We are using regparm(1) for all compilers.

4) The existing test is one of the obstacles making it impossible to compile without having libc for the target (x86_64->i386 would be really nice), as we need to run the compiled test executable.

5) Non-i386 architectures define NESTED_FUNC_ATTR as an empty symbol, so developers on such architectures don't see if they use it correctly.

6) NESTED_FUNC_ATTR tends to proliferate to the file scope functions, as it happened with grub_pci_iterate().  It only takes one caller using a nested function to force NESTED_FUNC_ATTR on all functions used as an argument to the same function.

We can give all formerly nested functions better, more descriptive names starting like other functions in the file and ending with "_iter".

--
Regards,
Pavel Roskin

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 1 line
Bean
#2 lore raw
Quoted 7 lines · Bean

Hi,

One of the advantage of nested function is to use local variables. Without it, we would need to pass them as global variable, or add custom data pointer in many of the iterate function, which would make the code a lot uglier IMO.

Do you have information in which direction it will influence the size of kernel? (trampoline vs global vs custom pointer)

Quoted 56 lines · Pavel Roskin
Quoted 37 lines

Hello!

While I have just applied a patch adding NESTED_FUNC_ATTR to several functions as an emergency fix for a major breakage in ata, ohci, uhci and lspci modules, I would prefer a more radical solution.

I suggest that we eliminate all nested functions. The reasons are:

1) They make the code less readable, as they make the parent functions longer.

2) They have problems with some popular compilers, as recent as gcc-4.0 when regparm(3) is used.

3) We failed to implement a reliable test for such problems. We are using regparm(1) for all compilers.

4) The existing test is one of the obstacles making it impossible to compile without having libc for the target (x86_64->i386 would be really nice), as we need to run the compiled test executable.

5) Non-i386 architectures define NESTED_FUNC_ATTR as an empty symbol, so developers on such architectures don't see if they use it correctly.

6) NESTED_FUNC_ATTR tends to proliferate to the file scope functions, as it happened with grub_pci_iterate(). It only takes one caller using a nested function to force NESTED_FUNC_ATTR on all functions used as an argument to the same function.

We can give all formerly nested functions better, more descriptive names starting like other functions in the file and ending with "_iter".

--
Regards,
Pavel Roskin

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

--
Bean

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
In reply to Bean
Quoted 6 lines · Bean

Hi,

One of the advantage of nested function is to use local variables. Without it, we would need to pass them as global variable, or add custom data pointer in many of the iterate function, which would make the code a lot uglier IMO.

Indeed, I tried to get rid on nested functions in fs/ext2.c, and it requires more changes than I expected.

However, my impression is that everything can be written nicely with more effort. Some arguments could be put into the structures we need to pass, so the number of arguments doesn't increase.

We should probably try to avoid adding new nested functions in the meantime.

Signature · 2 lines
Regards,
Pavel Roskin
#8 lore raw
In reply to Pavel Roskin
Quoted 15 lines · Pavel Roskin

I suggest that we eliminate all nested functions. The reasons are:

1) They make the code less readable, as they make the parent functions longer.

2) They have problems with some popular compilers, as recent as gcc-4.0 when regparm(3) is used.

3) We failed to implement a reliable test for such problems. We are using regparm(1) for all compilers.

4) The existing test is one of the obstacles making it impossible to compile without having libc for the target (x86_64->i386 would be really nice), as we need to run the compiled test executable.

Quoted 9 lines

5) Non-i386 architectures define NESTED_FUNC_ATTR as an empty symbol, so developers on such architectures don't see if they use it correctly.

6) NESTED_FUNC_ATTR tends to proliferate to the file scope functions, as it happened with grub_pci_iterate(). It only takes one caller using a nested function to force NESTED_FUNC_ATTR on all functions used as an argument to the same function.

7) The grub utils may not run on some 'security hardened' platform which does not allow to enable stack execution.

Signature · 1 line
Christian Franke
#10 lore raw

David Miller wrote on Friday 17 April 2009:

Quoted 6 lines

From: Pavel Roskin <[email protected]>
Date: Fri, 17 Apr 2009 11:54:57 -0400

Quoted 1 line

I suggest that we eliminate all nested functions.

I support this completely.

Me too.

While I like the idea of nested functions, since they are like closures and make a lot of common operations (such as iterating over a collection) a little more concise in the source code, you can certainly implement anything without nested functions that you can with them. Probably passing a pointer to a local structure is the easiest way to do it in most cases if the iteration function needs to access some state, right?

Regards,
Colin

#11 lore raw

From: Colin D Bennett <[email protected]>
Date: Sat, 18 Apr 2009 08:57:33 -0700

Quoted 3 lines

Probably passing a pointer to a local structure is the easiest way to do it in most cases if the iteration function needs to access some state, right?

Right.

The biggest surprise for me, easily, when reading the grub2 sources for the first time was seeing all of this masterbation with nested functions.

#4 lore raw
In reply to Pavel Roskin

Hi,

Yeah, I agree with you. The conversion will take some effort, but it could payoff in the long run. Perhaps we can achieve this in two steps:

1, Change nested function definition to accept only one parameter. For function with multiple parameters, place them in a structure and pass the pointer. This would eliminate NESTED_FUNC_ATTR, as the regparm issue won't occur in function with only one parameter.

2. Eliminate nested function. This would be easier after step 1. As we now pass parameters in a structure, we can append extra variables at the end, and cast it to the required type. Inside the callback function, we cast it back to use the extra fields.

Quoted 28 lines · Pavel Roskin
Quoted 6 lines · Bean

Hi,

One of the advantage of nested function is to use local variables. Without it, we would need to pass them as global variable, or add custom data pointer in many of the iterate function, which would make the code a lot uglier IMO.

Indeed, I tried to get rid on nested functions in fs/ext2.c, and it requires more changes than I expected.

However, my impression is that everything can be written nicely with more effort.  Some arguments could be put into the structures we need to pass, so the number of arguments doesn't increase.

We should probably try to avoid adding new nested functions in the meantime.

--
Regards,
Pavel Roskin

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 1 line
Bean
#5 lore raw

Quoting Bean <[email protected]>:

Quoted 9 lines

Yeah, I agree with you. The conversion will take some effort, but it could payoff in the long run. Perhaps we can achieve this in two steps:

1, Change nested function definition to accept only one parameter. For function with multiple parameters, place them in a structure and pass the pointer. This would eliminate NESTED_FUNC_ATTR, as the regparm issue won't occur in function with only one parameter.

Quoted 4 lines

2. Eliminate nested function. This would be easier after step 1. As we now pass parameters in a structure, we can append extra variables at the end, and cast it to the required type. Inside the callback function, we cast it back to use the extra fields.

That's an excellent plan! Thank you!

Considering the amount of changes, it may be a good idea to use the git mirror with stgit, so that several patches can be made and everything is well tested together before applying.

Signature · 2 lines
Regards,
Pavel Roskin
#6 lore raw

In my opinion a far better way would be to add an additional void * argument which is passed unchanged to the hook. Like:

struct local
{
...
};

main_func ()
{
struct local locvars;

grub_*_iterate (arg1,arg2,arg3, &locvars);
}

hook (arg1, arg2, arg3, voidparg)
{
struct local *locvars = (struct local *) voidparg;

}

Quoted 33 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Yeah, I agree with you. The conversion will take some effort, but it

Quoted 8 lines

could payoff in the long run. Perhaps we can achieve this in two steps:

1, Change nested function definition to accept only one parameter. For function with multiple parameters, place them in a structure and pass the pointer. This would eliminate NESTED_FUNC_ATTR, as the regparm issue won't occur in function with only one parameter.

Quoted 5 lines

2. Eliminate nested function. This would be easier after step 1. As we now pass parameters in a structure, we can append extra variables at the end, and cast it to the required type. Inside the callback function, we cast it back to use the extra fields.

That's an excellent plan! Thank you!

Considering the amount of changes, it may be a good idea to use the git mirror with stgit, so that several patches can be made and everything is well tested together before applying.

--
Regards,
Pavel Roskin

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

Hi,

Right, we can wrap the local variables in void* and pass them in a custom pointer, but I still think it's advantageous to put them in a structure rather than use multiple parameters:

1, We can still use nested function. Sometimes it may seem tedious to use external function for simple callback function. IMO, nested function is not that bad, we just have to use it safely.

2. It's extensible. For example, we may need to add new information for the hook function (such as adding modification time for dir hook). With the structure pointer, we just need to add a new field, old code that doesn' t use this information doesn't need to change. With parameters, we need to change the definition of all callback function, and the numerous __attribute__ ((unused)) is not pretty either.

Quoted 62 lines · Vladimir Serbinenko

In my opinion a far better way would be to add an additional void * argument which is passed unchanged to the hook. Like:

struct local
{
   ...
};

main_func ()
{
struct local locvars;

grub_*_iterate (arg1,arg2,arg3, &locvars);
}

hook (arg1, arg2, arg3, voidparg)
{
struct local *locvars = (struct local *) voidparg;

}

Quoted 28 lines · Pavel Roskin

Quoting Bean <[email protected]>:

Quoted 9 lines

Yeah, I agree with you. The conversion will take some effort, but it could payoff in the long run. Perhaps we can achieve this in two steps:

1, Change nested function definition to accept only one parameter. For function with multiple parameters, place them in a structure and pass the pointer. This would eliminate NESTED_FUNC_ATTR, as the regparm issue won't occur in function with only one parameter.

Quoted 4 lines

2. Eliminate nested function. This would be easier after step 1. As we now pass parameters in a structure, we can append extra variables at the end, and cast it to the required type. Inside the callback function, we cast it back to use the extra fields.

That's an excellent plan!  Thank you!

Considering the amount of changes, it may be a good idea to use the git mirror with stgit, so that several patches can be made and everything is well tested together before applying.

--
Regards,
Pavel Roskin

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 1 line
Bean