public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Marvin Häuser" <mhaeuser@posteo.de>
To: devel@edk2.groups.io, ray.ni@intel.com
Cc: "Kinney, Michael D" <michael.d.kinney@intel.com>,
	Liming Gao <gaoliming@byosoft.com.cn>,
	"Liu, Zhiguang" <zhiguang.liu@intel.com>,
	Vitaly Cheptsov <vit9696@protonmail.com>
Subject: Re: [edk2-devel] [PATCH V2 2/3] MdePkg/Base.h: Introduce various alignment-related macros
Date: Mon, 16 Aug 2021 13:10:06 +0000	[thread overview]
Message-ID: <6b4798af-92e4-3411-1586-f9e1d922e0ab@posteo.de> (raw)
In-Reply-To: <CO1PR11MB4930B865265F96EF826367418CFD9@CO1PR11MB4930.namprd11.prod.outlook.com>

Hey Ray,

On 16/08/2021 11:42, Ni, Ray wrote:
> Marvin,
> So lucky to have you in the edk2 project looking into these fundamentals!

Thank you. :)

> +  #define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; TYPE A; }, A)
>
> 1. Does struct{} inside a macro conform to C standard? How is the compatibility with different compilers?

This should work, yes. The C standard defines offsetof as such:

"The macros are [...]

         offsetof(type, member-designator)

which expands to an integer constant expression that has type size_t, 
the value of
which is the offset in bytes, to the structure member (designated by 
member-designator),
from the beginning of its structure (designated by type). The type and 
member designator
shall be such that given

         static type t;

then the expression &(t.member-designator) evaluates to an address 
constant. [...]" [1]

If we plug in t:

         static struct { CHAR8 C; TYPE A; } t;

we get a valid static storage duration variable declaration that 
satisfies the the last condition because:

"An address constant is [...], a pointer to an lvalue designating an 
object of static
storage duration, or [...]" [2]

It worked with all compilers I tinkered with at https://godbolt.org/
I sadly do not have access to any of the compilers where this may be 
used effectively (RVCT, EBC).

> +#define IS_POW2(Value)  ((Value) != 0U && ((Value) & ((Value) - 1U)) ==
> +0U)
>
> 2. Good to me. I learned this trick when implementing the MtrrLib.
>
> +#define ALIGN_VALUE_ADDEND(Value, Alignment)  (((Alignment) - (Value))
> +& ((Alignment) - 1U))
>
> 3. Is any other open source project using the same macro for the addend?
> This is actually a general question to all new macros.
> I would like the macros look familiar to developers from other open source projects.

Good question, I never really saw it. I only came up with it because for 
the new PE loader, we may align the PE memory within an underaligned 
buffer, and for that we need the addend. I initially used to align up 
and then subtract, but I saw this could be simplified with 
ALIGN_VALUE_ADDEND, which was used in ALIGN_VALUE anyway. If you have a 
better name, I'll change it.

Best regards,
Marvin


[1] ISO/IEC 9899:2011, 7.19, 3.

[2] ISO/IEC 9899:2011, 6.6, 9.

>
>
> 
>
>


  reply	other threads:[~2021-08-16 13:10 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15 20:11 [PATCH V2 1/3] MdeModulePkg: Rename IS_ALIGNED macros to avoid name collisions Marvin Häuser
2021-08-15 20:11 ` [PATCH V2 2/3] MdePkg/Base.h: Introduce various alignment-related macros Marvin Häuser
2021-08-16  9:42   ` [edk2-devel] " Ni, Ray
2021-08-16 13:10     ` Marvin Häuser [this message]
2021-08-17  1:17       ` Ni, Ray
2021-11-23 10:12       ` Marvin Häuser
2021-12-08  9:10         ` mjsbeaton
2021-12-08 10:36           ` Marvin Häuser
2022-03-22 19:06         ` Marvin Häuser
2021-08-15 20:11 ` [PATCH V2 3/3] MdeModulePkg: Consume new " Marvin Häuser
2021-08-20  5:21   ` [edk2-devel] " Wu, Hao A
2021-08-20  5:21 ` [PATCH V2 1/3] MdeModulePkg: Rename IS_ALIGNED macros to avoid name collisions Wu, Hao A

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6b4798af-92e4-3411-1586-f9e1d922e0ab@posteo.de \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox