grub-devel
[Top][All Lists]

## Re: [PATCH v2] Optimization calculation expression of FOR_MODULES(var)

 From: Vladimir 'phcoder' Serbinenko Subject: Re: [PATCH v2] Optimization calculation expression of FOR_MODULES(var) Date: Mon, 13 May 2019 21:25:32 +0700

As already noticed this is actually on purpose to align pointer on addr_t size.

On Thu, 11 Apr 2019, 20:04 Milo Wenxiang Niu, <address@hidden> wrote:
From: Milo Wenxiang X Niu <address@hidden>

It's just to remove the common factor: "sizeof (grub_addr_t)"  from the numerator and denominator of the fractional _expression_ of next var.
Let me explain it:
Shortly:
B: grub_uint32_t ;

Thus, original _expression_ can be expressed as:
var = (H *)((B*)var) + ( offset_exp ))
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(((H*)var)->size + sizeof(A) - 1)    sizeof(A)  |
offset_exp = --------------------------------- * ----------- |
sizeof(A)                   sizeof(B)  |
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Remove the common factor "sizeof(A)" of fractional offset_exp, we got the result.
offset_exp_new = ((H*)->size + sizeof(A) - 1) / sizeof(B)
= ((struct grub_module_header *) var)->size + sizeof (grub_addr_t) -1) / sizeof (grub_uint32_t)

so:
var =(H *)(((B*)var) + ( (((H*)var)->size + sizeof(A) - 1) / sizeof(B) ))
That's what I do.

Still, the new offset express is meaningfull:
it present the offset value united by byte.
*denominator: sizeof (grub_uint32_t)
it's means "struct grub_module_header" aligned by sizeof(grub_uint32_t)
---
include/grub/kernel.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/grub/kernel.h b/include/grub/kernel.h
index 133a37c..b257224 100644
--- a/include/grub/kernel.h
+++ b/include/grub/kernel.h
@@ -104,7 +104,7 @@ extern grub_addr_t EXPORT_VAR (grub_modbase);
< (grub_modbase + (((struct grub_module_info *) grub_modbase)->size));    \
var = (struct grub_module_header *)                                  \
-    (((grub_uint32_t *) var) + ((((struct grub_module_header *) var)->size + sizeof (grub_addr_t) - 1) / sizeof (grub_addr_t)) * (sizeof (grub_addr_t) / sizeof (grub_uint32_t))))
+    (((grub_uint32_t *) var) + ((((struct grub_module_header *) var)->size + sizeof (grub_addr_t) - 1) / sizeof (grub_uint32_t))))

--
1.8.3.1

_______________________________________________
Grub-devel mailing list