Removing nested functions, part one of lots
PATCH 1/1 Removing nested functions, part one of lots
(Part zero was a patch I already committed that dealt with some trivial cases.)
As I mentioned on #grub, and following up on
https://lists.gnu.org/archive/html/grub-devel/2009-04/msg00406.html and thread, I'm working on a patch set to eliminate nested functions from GRUB. I'd like to add a couple of extra reasons to those given by Pavel nearly four years ago:
* The trampolines used when taking the address of a nested function are a net cost in terms of code size. With my full set of patches so far, the i386-pc (compressed) kernel gets 52 bytes smaller and a sample core image profile (biosdisk ext2 part_msdos lvm mdraid1x) gets 39 bytes smaller. That's admittedly not lots, but there are some situations on i386-pc that are very close to the wire and where every byte in the core image counts.
* Even several years on, we have failed to solve all the build system problems associated with nested functions. See Savannah bugs #36669 and #37938.
* It is revealing that if you search the web for "gcc nested functions" or similar, you find several GCC bug reports from GRUB developers. We appear to be in a small minority of free programs making use of this facility, and I for one would rather concentrate on producing a high-quality boot loader rather than fighting arcane compiler features.
The vast majority of the nested functions we use - or at any rate the particularly problematic ones that involve trampolines - are iterator hook functions. In the general case, un-nesting these involves passing a context structure as an additional argument to the hook function. I have adopted a mostly formulaic approach to this, exemplified by the attached patch which converts grub_pci_iterate to the new scheme. My approach, which I'll spell out for the sake of those maintaining patches, is as follows:
* Whenever a function (usually *_iterate) takes a hook function pointer "foo" as a parameter, add an additional "void *foo_data" parameter immediately after it.
* If there is not already a typedef for the hook function type, add one.
* Update all implementations of the hook type in question to take an additional "void *data" parameter.
* Move each hook that was previously a nested function to the top level of its file, and mark it static.
* If a hook requires no local variables from its parent function, mark the data parameter __attribute__ ((unused)) and pass NULL when calling it (either directly or via the relevant *_iterate function).
* If a hook only requires a single local variable from its parent function, pass a reference to that variable as its data parameter, and have the hook declare an pointer variable at the top initialised to data (e.g. "static int *found = data").
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure. I made use of GCC's non-constant aggregate automatic initialisers extension when initialising the context structure in the parent, which I found clearest.
* If a hook has a reasonably clear name already, leave it alone. However, if it is called something excessively general such as "hook", then rename it either to something describing its purpose or else simply to "<name-of-parent>_iter".
* Remove any NESTED_FUNC_ATTR from hook declarations, and from any pre-existing typedef for the hook function type.
I have a number of patches mostly ready to go, but I'd prefer to make sure that this general approach is agreed before preparing and sending more than one of them. I'd like to work one *_iterate function at a time (except where multiple iterators are entangled in a stack such that we need to change several at once, as is the case in parts of the disk/filesystem stacks), as that's roughly the minimum sensible unit and it makes it reasonably easy to grep for missing changes.
Please review.
Thanks,
Signature · 1 line
Colin Watson [[email protected]]
=== modified file 'grub-core/bus/cs5536.c'
bus/cs5536.c +28 -17
@@ -29,28 +29,39 @@GRUB_MOD_LICENSE ("GPLv3+" ); +/* Context for grub_cs5536_find. */ +struct grub_cs5536_find_ctx +{ +grub_pci_device_t *devp; +int found; +}; + +/* Helper for grub_cs5536_find. */ +static int +grub_cs5536_find_iter (grub_pci_device_t dev,grub_pci_id_t pciid,void *data) +{ +struct grub_cs5536_find_ctx *ctx = data; + +if (pciid ==GRUB_CS5536_PCIID ) + { + *ctx->devp = dev; + ctx->found =1 ; +return 1 ; + } +return 0 ; +} +int grub_cs5536_find (grub_pci_device_t *devp) { -int found =0 ; - auto int NESTED_FUNC_ATTR hook (grub_pci_device_t dev, -grub_pci_id_t pciid); - - int NESTED_FUNC_ATTR hook (grub_pci_device_t dev, -grub_pci_id_t pciid) - { -if (pciid ==GRUB_CS5536_PCIID ) - { - *devp = dev; - found =1 ; -return 1 ; - } -return 0 ; - } +struct grub_cs5536_find_ctx ctx = { + .devp = devp, + .found =0 + }; -grub_pci_iterate (hook); +grub_pci_iterate (grub_cs5536_find_iter, &ctx); -return found; +return ctx.found; } grub_uint64_t
=== modified file 'grub-core/bus/emu/pci.c'
bus/emu/pci.c +2 -2
@@ -32,7 +32,7 @@ grub_pci_make_address (grub_pci_device_t }void -grub_pci_iterate (grub_pci_iteratefunc_t hook) +grub_pci_iterate (grub_pci_iteratefunc_t hook,void *hook_data) {struct pci_device_iterator *iter;struct pci_slot_match slot;
@@ -43,7 +43,7 @@ grub_pci_iterate (grub_pci_iteratefunc_t slot.func =PCI_MATCH_ANY ; iter =pci_slot_match_iterator_create (&slot);while ((dev =pci_device_next (iter))) -hook (dev, dev->vendor_id | (dev->device_id <<16 )); +hook (dev, dev->vendor_id | (dev->device_id <<16 ), hook_data);pci_iterator_destroy (iter); }
=== modified file 'grub-core/bus/pci.c'
bus/pci.c +2 -2
@@ -98,7 +98,7 @@ grub_pci_make_address (grub_pci_device_t }void -grub_pci_iterate (grub_pci_iteratefunc_t hook) +grub_pci_iterate (grub_pci_iteratefunc_t hook,void *hook_data) {grub_pci_device_t dev;grub_pci_address_t addr;
@@ -125,7 +125,7 @@ grub_pci_iterate (grub_pci_iteratefunc_tcontinue ; } -if (hook (dev, id)) +if (hook (dev, id, hook_data))return ;/* Probe only func = 0 if the device if not multifunction */
=== modified file 'grub-core/bus/usb/ehci.c'
bus/usb/ehci.c +4 -3
@@ -454,8 +454,9 @@ grub_ehci_reset (struct grub_ehci *e) }/* PCI iteration function... */ -static int NESTED_FUNC_ATTR -grub_ehci_pci_iter (grub_pci_device_t dev,grub_pci_id_t pciid) +static int +grub_ehci_pci_iter (grub_pci_device_t dev,grub_pci_id_t pciid, +void *data __attribute__ ((unused))) {grub_uint8_t release;grub_uint32_t class_code;
@@ -1814,7 +1815,7 @@ grub_ehci_detect_dev (grub_usb_controllestatic void grub_ehci_inithw (void ) { -grub_pci_iterate (grub_ehci_pci_iter); +grub_pci_iterate (grub_ehci_pci_iter,NULL ); } static grub_err_t
=== modified file 'grub-core/bus/usb/ohci.c'
bus/usb/ohci.c +4 -4
@@ -213,9 +213,9 @@ grub_ohci_writereg32 (struct grub_ohci */* Iterate over all PCI devices. Determine if a device is an OHCI controller. If this is the case, initialize it. */ -static int NESTED_FUNC_ATTR -grub_ohci_pci_iter (grub_pci_device_t dev, -grub_pci_id_t pciid) +static int +grub_ohci_pci_iter (grub_pci_device_t dev,grub_pci_id_t pciid, +void *data __attribute__ ((unused))) {grub_uint32_t interf;grub_uint32_t base;
@@ -477,7 +477,7 @@ grub_ohci_pci_iter (grub_pci_device_t destatic void grub_ohci_inithw (void ) { -grub_pci_iterate (grub_ohci_pci_iter); +grub_pci_iterate (grub_ohci_pci_iter,NULL ); }
=== modified file 'grub-core/bus/usb/uhci.c'
bus/usb/uhci.c +4 -3
@@ -185,9 +185,10 @@ grub_uhci_portstatus (grub_usb_controlle/* Iterate over all PCI devices. Determine if a device is an UHCI controller. If this is the case, initialize it. */ -static int NESTED_FUNC_ATTR +static int grub_uhci_pci_iter (grub_pci_device_t dev, -grub_pci_id_t pciid __attribute__((unused))) +grub_pci_id_t pciid __attribute__((unused)), +void *data __attribute__ ((unused))) {grub_uint32_t class_code;grub_uint32_t class;
@@ -351,7 +352,7 @@ grub_uhci_pci_iter (grub_pci_device_t destatic void grub_uhci_inithw (void ) { -grub_pci_iterate (grub_uhci_pci_iter); +grub_pci_iterate (grub_uhci_pci_iter,NULL ); } static grub_uhci_td_t
=== modified file 'grub-core/commands/efi/fixvideo.c'
commands/efi/fixvideo.c +5 -3
@@ -23,6 +23,7 @@#include <grub/pci.h> #include <grub/command.h> #include <grub/i18n.h> +#include <grub/mm.h> GRUB_MOD_LICENSE ("GPLv3+" );
@@ -40,8 +41,9 @@ static struct grub_video_patch {0, 0, 0, 0, 0} }; -static int NESTED_FUNC_ATTR -scan_card (grub_pci_device_t dev, grub_pci_id_t pciid) +static int +scan_card (grub_pci_device_t dev,grub_pci_id_t pciid, +void *data __attribute__ ((unused))) {grub_pci_address_t addr;
@@ -93,7 +95,7 @@ grub_cmd_fixvideo (grub_command_t cmd __ int argc __attribute__ ((unused)), char *argv[] __attribute__ ((unused))) { -grub_pci_iterate (scan_card); +grub_pci_iterate (scan_card,NULL );return 0 ; }
=== modified file 'grub-core/commands/lspci.c'
commands/lspci.c +5 -3
@@ -22,6 +22,7 @@#include <grub/misc.h> #include <grub/extcmd.h> #include <grub/i18n.h> +#include <grub/mm.h> GRUB_MOD_LICENSE ("GPLv3+" );
@@ -126,8 +127,9 @@ static const struct grub_arg_option optistatic int iospace; -static int NESTED_FUNC_ATTR -grub_lspci_iter (grub_pci_device_t dev, grub_pci_id_t pciid) +static int +grub_lspci_iter (grub_pci_device_t dev,grub_pci_id_t pciid, +void *data __attribute__ ((unused))) {grub_uint32_t class;const char *sclass;
@@ -218,7 +220,7 @@ grub_cmd_lspci (grub_extcmd_context_t ct char **args __attribute__ ((unused))) { iospace = ctxt->state[0 ].set; -grub_pci_iterate (grub_lspci_iter); +grub_pci_iterate (grub_lspci_iter,NULL );return GRUB_ERR_NONE ; }
=== modified file 'grub-core/commands/setpci.c'
commands/setpci.c +4 -3
@@ -83,8 +83,9 @@ static int regsize;static grub_uint16_t regaddr;static const char *varname; -static int NESTED_FUNC_ATTR -grub_setpci_iter (grub_pci_device_t dev,grub_pci_id_t pciid) +static int +grub_setpci_iter (grub_pci_device_t dev,grub_pci_id_t pciid, +void *data __attribute__ ((unused))) {grub_uint32_t regval =0 ;grub_pci_address_t addr;
@@ -320,7 +321,7 @@ grub_cmd_setpci (grub_extcmd_context_t creturn grub_error (GRUB_ERR_BAD_ARGUMENT ,"option -v isn't valid for writes" ); -grub_pci_iterate (grub_setpci_iter); +grub_pci_iterate (grub_setpci_iter,NULL );return GRUB_ERR_NONE ; }
=== modified file 'grub-core/disk/ahci.c'
disk/ahci.c +4 -3
@@ -254,9 +254,10 @@ init_port (struct grub_ahci_device *dev)return 1 ; } -static int NESTED_FUNC_ATTR +static int grub_ahci_pciinit (grub_pci_device_t dev, -grub_pci_id_t pciid __attribute__ ((unused))) +grub_pci_id_t pciid __attribute__ ((unused)), +void *data __attribute__ ((unused))) {grub_pci_address_t addr;grub_uint32_t class;
@@ -394,7 +395,7 @@ grub_ahci_pciinit (grub_pci_device_t devstatic grub_err_t grub_ahci_initialize (void ) { -grub_pci_iterate (grub_ahci_pciinit); +grub_pci_iterate (grub_ahci_pciinit,NULL );return grub_errno; }
=== modified file 'grub-core/disk/pata.c'
disk/pata.c +4 -3
@@ -338,9 +338,10 @@ grub_pata_device_initialize (int port, i }#ifndef GRUB_MACHINE_MIPS_QEMU_MIPS -static int NESTED_FUNC_ATTR +static int grub_pata_pciinit (grub_pci_device_t dev, -grub_pci_id_t pciid) +grub_pci_id_t pciid, +void *data __attribute__ ((unused))) {static int compat_use[2 ] = {0 };grub_pci_address_t addr;
@@ -446,7 +447,7 @@ grub_pata_pciinit (grub_pci_device_t devstatic grub_err_t grub_pata_initialize (void ) { -grub_pci_iterate (grub_pata_pciinit); +grub_pci_iterate (grub_pata_pciinit,NULL );return 0 ; }#else
=== modified file 'grub-core/kern/efi/mm.c'
kern/efi/mm.c +27 -28
64 lines not on this page · raw message
=== modified file 'grub-core/kern/mips/loongson/init.c'
kern/mips/loongson/init.c +39 -37
91 lines not on this page · raw message
=== modified file 'grub-core/kern/vga_init.c'
kern/vga_init.c +33 -28
77 lines not on this page · raw message
=== modified file 'grub-core/video/bochs.c'
video/bochs.c +24 -22
64 lines not on this page · raw message
=== modified file 'grub-core/video/cirrus.c'
video/cirrus.c +24 -22
64 lines not on this page · raw message
=== modified file 'grub-core/video/efi_uga.c'
video/efi_uga.c +69 -58
146 lines not on this page · raw message
=== modified file 'grub-core/video/radeon_fuloong2e.c'
video/radeon_fuloong2e.c +27 -23
68 lines not on this page · raw message
=== modified file 'grub-core/video/sis315pro.c'
video/sis315pro.c +32 -28
78 lines not on this page · raw message
=== modified file 'grub-core/video/sm712.c'
video/sm712.c +27 -23
68 lines not on this page · raw message
=== modified file 'include/grub/pci.h'
grub/pci.h +4 -3
@@ -132,13 +132,14 @@ grub_pci_get_function (grub_pci_device_t#include <grub/cpu/pci.h> #endif -typedef int NESTED_FUNC_ATTR (*grub_pci_iteratefunc_t) - (grub_pci_device_t dev,grub_pci_id_t pciid); +typedef int (*grub_pci_iteratefunc_t ) + (grub_pci_device_t dev,grub_pci_id_t pciid,void *data);grub_pci_address_t EXPORT_FUNC (grub_pci_make_address ) (grub_pci_device_t dev,int reg); -void EXPORT_FUNC (grub_pci_iterate ) (grub_pci_iteratefunc_t hook); +void EXPORT_FUNC (grub_pci_iterate ) (grub_pci_iteratefunc_t hook, +void *hook_data);struct grub_pci_dma_chunk ;
Yay!! Does this change the minimum GCC version needed to build?
Thanks!!
-S
Quoted 94 lines · Colin Watson
(Part zero was a patch I already committed that dealt with some trivial cases.)
As I mentioned on #grub, and following up on
https://lists.gnu.org/archive/html/grub-devel/2009-04/msg00406.html and thread, I'm working on a patch set to eliminate nested functions from GRUB. I'd like to add a couple of extra reasons to those given by Pavel nearly four years ago:* The trampolines used when taking the address of a nested function are a net cost in terms of code size. With my full set of patches so far, the i386-pc (compressed) kernel gets 52 bytes smaller and a sample core image profile (biosdisk ext2
part_msdoslvm mdraid1x) gets 39 bytes smaller. That's admittedly not lots, but there are some situations on i386-pc that are very close to the wire and where every byte in the core image counts.* Even several years on, we have failed to solve all the build system problems associated with nested functions. See Savannah bugs #36669 and #37938.
* It is revealing that if you search the web for "gcc nested functions" or similar, you find several GCC bug reports from GRUB developers. We appear to be in a small minority of free programs making use of this facility, and I for one would rather concentrate on producing a high-quality boot loader rather than fighting arcane compiler features.
The vast majority of the nested functions we use - or at any rate the particularly problematic ones that involve trampolines - are iterator hook functions. In the general case, un-nesting these involves passing a context structure as an additional argument to the hook function. I have adopted a mostly formulaic approach to this, exemplified by the attached patch which converts
grub_pci_iterateto the new scheme. My approach, which I'll spell out for the sake of those maintaining patches, is as follows:* Whenever a function (usually
*_iterate) takes a hook function pointer "foo" as a parameter, add an additional "void*foo_data" parameter immediately after it.* If there is not already a typedef for the hook function type, add one.
* Update all implementations of the hook type in question to take an additional "void *data" parameter.
* Move each hook that was previously a nested function to the top level of its file, and mark it static.
* If a hook requires no local variables from its parent function, mark the data parameter
__attribute__((unused)) and pass NULL when calling it (either directly or via the relevant*_iteratefunction).* If a hook only requires a single local variable from its parent function, pass a reference to that variable as its data parameter, and have the hook declare an pointer variable at the top initialised to data (e.g. "static int *found = data").
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure. I made use of GCC's non-constant aggregate automatic initialisers extension when initialising the context structure in the parent, which I found clearest.
* If a hook has a reasonably clear name already, leave it alone. However, if it is called something excessively general such as "hook", then rename it either to something describing its purpose or else simply to "<name-of-parent>_iter".
* Remove any
NESTED_FUNC_ATTRfrom hook declarations, and from any pre-existing typedef for the hook function type.I have a number of patches mostly ready to go, but I'd prefer to make sure that this general approach is agreed before preparing and sending more than one of them. I'd like to work one
*_iteratefunction at a time (except where multiple iterators are entangled in a stack such that we need to change several at once, as is the case in parts of the disk/filesystem stacks), as that's roughly the minimum sensible unit and it makes it reasonably easy to grep for missing changes.Please review.
Thanks,
Signature · 6 lines
Colin Watson [[email protected]] <unnest-pci-iterate.patch> _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 1 line · Seth Goldberg
Yay!! Does this change the minimum GCC version needed to build?
We don't support anything older than 4.2. That's unlikely to change.
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 1 line · Seth Goldberg
Yay!! Does this change the minimum GCC version needed to build?
As Vladimir said, it probably isn't worth the effort at this point for us to wind back the version we recommend, as I suspect not enough of us can conveniently test with older versions on a regular basis. That said, since I think the increased GCC requirement was due to a bug related to nested functions, it may be that this improves the situation for those who need to use older versions despite our documented requirement.
Signature · 1 line
Colin Watson [[email protected]]
Quoted 11 lines · Colin Watson
Quoted 1 line · Seth Goldberg
Yay!! Does this change the minimum GCC version needed to build?
As Vladimir said, it probably isn't worth the effort at this point for us to wind back the version we recommend, as I suspect not enough of us can conveniently test with older versions on a regular basis. That said, since I think the increased GCC requirement was due to a bug related to nested functions, it may be that this improves the situation for those who need to use older versions despite our documented requirement.
Older than 4.2 versions of GCC can compile GRUB 2.00. It's just not supported. As for 3.x, I don't think it's worth even going there
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko
В Tue, 1 Jan 2013 14:42:04 +0000
Colin Watson <[email protected]> пишет:
Quoted 5 lines
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure.
Personally I find "ctx" part a bit confusing. It is not really execution context in usual sense, it is just collection of random variables. I would rather go with "struct <name-of-parent>_data" here.
Quoted 4 lines · Andrey Borzenkov
В Tue, 1 Jan 2013 14:42:04 +0000
Colin Watson <[email protected]> пишет:Quoted 1 line
Quoted 9 lines
Quoted 4 lines
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure.
Personally I find "ctx" part a bit confusing. It is not really execution context in usual sense, it is just collection of random variables. I would rather go with "struct <name-of-parent>_data" here.
It's acting as a closure. That might be a more exact name.
Quoted 6 lines
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 8 lines · On Tue, Jan 01, 2013 at 05:24:00PM -0500, [email protected] wrote:
Quoted 7 lines · Andrey Borzenkov
В Tue, 1 Jan 2013 14:42:04 +0000
Colin Watson <[email protected]> пишет:Quoted 4 lines
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure.
Quoted 5 lines
Quoted 3 lines
Personally I find "ctx" part a bit confusing. It is not really execution context in usual sense, it is just collection of random variables. I would rather go with "struct <name-of-parent>_data" here.
It's acting as a closure. That might be a more exact name.
"Closure" has all sorts of baggage for different people that I would rather not invoke. In particular the context/data/whatever structure does not contain any reference to the hook function, so in my book calling it a closure would be confusing; it's really only part of a closure, namely the set of associated free variables.
Signature · 1 line
Colin Watson [[email protected]]
Quoted 10 lines · Andrey Borzenkov
В Tue, 1 Jan 2013 14:42:04 +0000
Colin Watson <[email protected]> пишет:Quoted 4 lines
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure.
Personally I find "ctx" part a bit confusing. It is not really execution context in usual sense, it is just collection of random variables. I would rather go with "struct <name-of-parent>_data" here.
I'm fine with that (and this is exactly why I posted this for a bit of a bikeshedding opportunity :-) ). Vladimir, any opinions on the naming?
Signature · 1 line
Colin Watson [[email protected]]
Quoted 8 lines · Colin Watson
Quoted 7 lines · Andrey Borzenkov
В Tue, 1 Jan 2013 14:42:04 +0000
Colin Watson <[email protected]> пишет:Quoted 4 lines
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure.
Quoted 6 lines
Quoted 3 lines
Personally I find "ctx" part a bit confusing. It is not really execution context in usual sense, it is just collection of random variables. I would rather go with "struct <name-of-parent>_data" here.
I'm fine with that (and this is exactly why I posted this for a bit of a bikeshedding opportunity :-) ). Vladimir, any opinions on the naming?
Actually, "*_data" is suboptimal because (particularly in filesystem code) there are many other variables and types called "data". How about "*_vars"? Then I can use "struct foo_vars *vars = data;" or similar as well and it should work out reasonably well.
Signature · 1 line
Colin Watson [[email protected]]
Quoted 13 lines · Colin Watson
Quoted 12 lines · Colin Watson
Quoted 10 lines · Andrey Borzenkov
В Tue, 1 Jan 2013 14:42:04 +0000
Colin Watson <[email protected]> пишет:Quoted 4 lines
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure.
Personally I find "ctx" part a bit confusing. It is not really execution context in usual sense, it is just collection of random variables. I would rather go with "struct <name-of-parent>_data" here.
Quoted 8 lines
Quoted 2 lines
I'm fine with that (and this is exactly why I posted this for a bit of a bikeshedding opportunity :-) ). Vladimir, any opinions on the naming?
Actually, "
*_data" is suboptimal because (particularly in filesystem code) there are many other variables and types called "data". How about "*_vars"? Then I can use "structfoo_vars*vars = data;" or similar as well and it should work out reasonably well.
I feel like "_ctx" is a good one. It is the executional context, it's just trimmed to what we really need.
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 7 lines · Colin Watson
I have a number of patches mostly ready to go, but I'd prefer to make sure that this general approach is agreed before preparing and sending more than one of them. I'd like to work one
*_iteratefunction at a time (except where multiple iterators are entangled in a stack such that we need to change several at once, as is the case in parts of the disk/filesystem stacks), as that's roughly the minimum sensible unit and it makes it reasonably easy to grep for missing changes.
There is also another approach of using iterators of the kind:
struct pci_iterator;
void pci_init (struct pci_iterator *pci);
int pci_iter (struct pci_iterator *pci);
#define FOR_PCI_DEVICE(x) for (pci_init (&x); pci_iter (&x);)
I have done it for PCI but seem to have bzr problem:
bzr: ERROR: Revision {[email protected]}
not present in "Graph(StackedParentsProvider(bzrlib.repository._LazyListJoin(([CachingParentsProvider(CallableToParentsProviderAdapter(<bound method CHKInventoryRepository._get_parent_map_no_fallbacks of CHKInventoryRepository('file:///home/phcoder/grub2/bzr/.bzr/repository/')>))],
[]))))".
Admittingly for disk/filesystems it's not viable and even for PCI it was somewhat messy but may be viable in some cases like where we already switch to FOR_* macros.
Quoted 12 lines
Please review.
Thanks,
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko
Looks like nobody objects and I'm fine with this patch. Go ahead.
Quoted 94 lines · Colin Watson
(Part zero was a patch I already committed that dealt with some trivial cases.)
As I mentioned on #grub, and following up on
https://lists.gnu.org/archive/html/grub-devel/2009-04/msg00406.html and thread, I'm working on a patch set to eliminate nested functions from GRUB. I'd like to add a couple of extra reasons to those given by Pavel nearly four years ago:* The trampolines used when taking the address of a nested function are a net cost in terms of code size. With my full set of patches so far, the i386-pc (compressed) kernel gets 52 bytes smaller and a sample core image profile (biosdisk ext2
part_msdoslvm mdraid1x) gets 39 bytes smaller. That's admittedly not lots, but there are some situations on i386-pc that are very close to the wire and where every byte in the core image counts.* Even several years on, we have failed to solve all the build system problems associated with nested functions. See Savannah bugs #36669 and #37938.
* It is revealing that if you search the web for "gcc nested functions" or similar, you find several GCC bug reports from GRUB developers. We appear to be in a small minority of free programs making use of this facility, and I for one would rather concentrate on producing a high-quality boot loader rather than fighting arcane compiler features.
The vast majority of the nested functions we use - or at any rate the particularly problematic ones that involve trampolines - are iterator hook functions. In the general case, un-nesting these involves passing a context structure as an additional argument to the hook function. I have adopted a mostly formulaic approach to this, exemplified by the attached patch which converts
grub_pci_iterateto the new scheme. My approach, which I'll spell out for the sake of those maintaining patches, is as follows:* Whenever a function (usually
*_iterate) takes a hook function pointer "foo" as a parameter, add an additional "void*foo_data" parameter immediately after it.* If there is not already a typedef for the hook function type, add one.
* Update all implementations of the hook type in question to take an additional "void *data" parameter.
* Move each hook that was previously a nested function to the top level of its file, and mark it static.
* If a hook requires no local variables from its parent function, mark the data parameter
__attribute__((unused)) and pass NULL when calling it (either directly or via the relevant*_iteratefunction).* If a hook only requires a single local variable from its parent function, pass a reference to that variable as its data parameter, and have the hook declare an pointer variable at the top initialised to data (e.g. "static int *found = data").
* If a hook requires more than one local variable from its parent function, declare "struct <name-of-parent>_ctx" with the necessary variables, and convert both the hook and the parent to access the variables in question via that structure. I made use of GCC's non-constant aggregate automatic initialisers extension when initialising the context structure in the parent, which I found clearest.
* If a hook has a reasonably clear name already, leave it alone. However, if it is called something excessively general such as "hook", then rename it either to something describing its purpose or else simply to "<name-of-parent>_iter".
* Remove any
NESTED_FUNC_ATTRfrom hook declarations, and from any pre-existing typedef for the hook function type.I have a number of patches mostly ready to go, but I'd prefer to make sure that this general approach is agreed before preparing and sending more than one of them. I'd like to work one
*_iteratefunction at a time (except where multiple iterators are entangled in a stack such that we need to change several at once, as is the case in parts of the disk/filesystem stacks), as that's roughly the minimum sensible unit and it makes it reasonably easy to grep for missing changes.Please review.
Thanks,
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 2 lines
Regards Vladimir 'φ-coder/phcoder' Serbinenko