public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [Question] using Flexible Array Member in Structure.
@ 2024-04-03  9:27 levi.yun
  2024-04-03 16:17 ` Michael D Kinney
  2024-04-05  7:34 ` Pedro Falcato
  0 siblings, 2 replies; 5+ messages in thread
From: levi.yun @ 2024-04-03  9:27 UTC (permalink / raw)
  To: devel@edk2.groups.io; +Cc: nd@arm.com, sami.mujawar@arm.com

Hello all!

while I see the code. I have one question related using Flexible Array 
Member.

For example)

///
/// Socket Type Data.
///
typedef struct {
   EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
   UINT16 SocketIdentifier;
   UINT16 Reserved;
   // EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE MemoryDeviceStructure[];
} EFI_ACPI_6_4_PMTT_SOCKET_TYPE_DATA;

In here, why MemoryDeviceStructure should remain with comments?

IIUC, edk2 coding style guide doesn't seem to prevent use of Flexible 
Array Member which is C99 standard.

And consider the compiler used to compile edk2 uses defaults standard 
above of C99 standard.

Couldn't I use the flexible array member like:

///
/// Socket Type Data.
///
typedef struct {
   EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
   UINT16 SocketIdentifier;
   UINT16 Reserved;
   EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE  MemoryDeviceStructure[]
}

Thanks!


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117365): https://edk2.groups.io/g/devel/message/117365
Mute This Topic: https://groups.io/mt/105305209/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [Question] using Flexible Array Member in Structure.
  2024-04-03  9:27 [edk2-devel] [Question] using Flexible Array Member in Structure levi.yun
@ 2024-04-03 16:17 ` Michael D Kinney
  2024-04-05  6:43   ` levi.yun
  2024-04-05  7:34 ` Pedro Falcato
  1 sibling, 1 reply; 5+ messages in thread
From: Michael D Kinney @ 2024-04-03 16:17 UTC (permalink / raw)
  To: devel@edk2.groups.io, yeoreum.yun@arm.com
  Cc: nd@arm.com, sami.mujawar@arm.com, Kinney, Michael D

Yes.  Use of flexible array members is supported and encouraged.

There are challenges with existing structure definitions that declare
arrays of size [1] when a flexible array is really intended. Those 
were defined before flexible array members were supported by all the
compilers.  Converting these to a flexible array member would not be
a backwards compatible change.

Mike

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of
> levi.yun
> Sent: Wednesday, April 3, 2024 2:27 AM
> To: devel@edk2.groups.io
> Cc: nd@arm.com; sami.mujawar@arm.com
> Subject: [edk2-devel] [Question] using Flexible Array Member in
> Structure.
> 
> Hello all!
> 
> while I see the code. I have one question related using Flexible Array
> Member.
> 
> For example)
> 
> ///
> /// Socket Type Data.
> ///
> typedef struct {
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
>    UINT16 SocketIdentifier;
>    UINT16 Reserved;
>    // EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE MemoryDeviceStructure[];
> } EFI_ACPI_6_4_PMTT_SOCKET_TYPE_DATA;
> 
> In here, why MemoryDeviceStructure should remain with comments?
> 
> IIUC, edk2 coding style guide doesn't seem to prevent use of Flexible
> Array Member which is C99 standard.
> 
> And consider the compiler used to compile edk2 uses defaults standard
> above of C99 standard.
> 
> Couldn't I use the flexible array member like:
> 
> ///
> /// Socket Type Data.
> ///
> typedef struct {
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
>    UINT16 SocketIdentifier;
>    UINT16 Reserved;
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE  MemoryDeviceStructure[]
> }
> 
> Thanks!
> 
> 
> 
> 



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117373): https://edk2.groups.io/g/devel/message/117373
Mute This Topic: https://groups.io/mt/105305209/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [Question] using Flexible Array Member in Structure.
  2024-04-03 16:17 ` Michael D Kinney
@ 2024-04-05  6:43   ` levi.yun
  2024-04-05  7:29     ` Pedro Falcato
  0 siblings, 1 reply; 5+ messages in thread
From: levi.yun @ 2024-04-05  6:43 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io; +Cc: nd, Sami Mujawar

Hi Michael! Thanks for answer.

> Converting these to a flexible array member would not be
a backwards compatible change.

That's the point. But at least when I see the compiler used in tool_def.txt,
there's no compiler which doesn't support to flexible array member.

Do we still need to consider the case building edk2 with lower version of comipler
which isn't manifested in tool_def.txt?

Thanks!


________________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: 03 April 2024 17:17
To: devel@edk2.groups.io; Yeo Reum Yun
Cc: nd; Sami Mujawar; Kinney, Michael D
Subject: RE: [edk2-devel] [Question] using Flexible Array Member in Structure.

Yes.  Use of flexible array members is supported and encouraged.

There are challenges with existing structure definitions that declare
arrays of size [1] when a flexible array is really intended. Those
were defined before flexible array members were supported by all the
compilers.  Converting these to a flexible array member would not be
a backwards compatible change.

Mike

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of
> levi.yun
> Sent: Wednesday, April 3, 2024 2:27 AM
> To: devel@edk2.groups.io
> Cc: nd@arm.com; sami.mujawar@arm.com
> Subject: [edk2-devel] [Question] using Flexible Array Member in
> Structure.
>
> Hello all!
>
> while I see the code. I have one question related using Flexible Array
> Member.
>
> For example)
>
> ///
> /// Socket Type Data.
> ///
> typedef struct {
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
>    UINT16 SocketIdentifier;
>    UINT16 Reserved;
>    // EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE MemoryDeviceStructure[];
> } EFI_ACPI_6_4_PMTT_SOCKET_TYPE_DATA;
>
> In here, why MemoryDeviceStructure should remain with comments?
>
> IIUC, edk2 coding style guide doesn't seem to prevent use of Flexible
> Array Member which is C99 standard.
>
> And consider the compiler used to compile edk2 uses defaults standard
> above of C99 standard.
>
> Couldn't I use the flexible array member like:
>
> ///
> /// Socket Type Data.
> ///
> typedef struct {
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
>    UINT16 SocketIdentifier;
>    UINT16 Reserved;
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE  MemoryDeviceStructure[]
> }
>
> Thanks!
>
>
> 
>



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117453): https://edk2.groups.io/g/devel/message/117453
Mute This Topic: https://groups.io/mt/105305209/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [Question] using Flexible Array Member in Structure.
  2024-04-05  6:43   ` levi.yun
@ 2024-04-05  7:29     ` Pedro Falcato
  0 siblings, 0 replies; 5+ messages in thread
From: Pedro Falcato @ 2024-04-05  7:29 UTC (permalink / raw)
  To: devel, yeoreum.yun; +Cc: Kinney, Michael D, nd, Sami Mujawar

On Fri, Apr 5, 2024 at 7:43 AM levi.yun <yeoreum.yun@arm.com> wrote:
>
> Hi Michael! Thanks for answer.
>
> > Converting these to a flexible array member would not be
> a backwards compatible change.
>
> That's the point. But at least when I see the compiler used in tool_def.txt,
> there's no compiler which doesn't support to flexible array member.
>
> Do we still need to consider the case building edk2 with lower version of comipler
> which isn't manifested in tool_def.txt?

That's not in question. What Mike is referring to here is, e.g:

typedef struct {
BOOLEAN Supported;
UINT64 KeyCount;
UINT64 CapabilityCount;
EFI_BLOCK_IO_CRYPTO_CAPABILITY Capabilities [1];
} EFI_BLOCK_IO_CRYPTO_CAPABILITIES1;

typedef struct {
BOOLEAN Supported;
UINT64 KeyCount;
UINT64 CapabilityCount;
EFI_BLOCK_IO_CRYPTO_CAPABILITY Capabilities [];
} EFI_BLOCK_IO_CRYPTO_CAPABILITIES2;

sizeof(EFI_BLOCK_IO_CRYPTO_CAPABILITIES1) !=
sizeof(EFI_BLOCK_IO_CRYPTO_CAPABILITIES2), so it's an ABI break.
And unfortunately many of these structs need to be ABI-stable (I took
this one off the spec, you can find many such cases by ctrl+F'ing
[1]).

Basically, it comes down to the ABI. If it breaks the ABI, you need to
figure out *if* it matters (e.g is it part of a protocol, or is it an
internal lib struct that does not need to be stable, is it ABI to some
external component).
Also, worth noting that the "[1]" pattern is UB, and only works
because GCC (et al) don't want to break a bunch of code. If you're
defining a new struct, there's no reason to use [1] as a flexible
array.

-- 
Pedro


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117454): https://edk2.groups.io/g/devel/message/117454
Mute This Topic: https://groups.io/mt/105305209/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [edk2-devel] [Question] using Flexible Array Member in Structure.
  2024-04-03  9:27 [edk2-devel] [Question] using Flexible Array Member in Structure levi.yun
  2024-04-03 16:17 ` Michael D Kinney
@ 2024-04-05  7:34 ` Pedro Falcato
  1 sibling, 0 replies; 5+ messages in thread
From: Pedro Falcato @ 2024-04-05  7:34 UTC (permalink / raw)
  To: devel, yeoreum.yun; +Cc: nd@arm.com, sami.mujawar@arm.com

On Wed, Apr 3, 2024 at 10:27 AM levi.yun <yeoreum.yun@arm.com> wrote:
>
> Hello all!
>
> while I see the code. I have one question related using Flexible Array
> Member.
>
> For example)
>
> ///
> /// Socket Type Data.
> ///
> typedef struct {
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
>    UINT16 SocketIdentifier;
>    UINT16 Reserved;
>    // EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE MemoryDeviceStructure[];
> } EFI_ACPI_6_4_PMTT_SOCKET_TYPE_DATA;
>
>
> ///
> /// Socket Type Data.
> ///
> typedef struct {
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE CommonMemoryDeviceHeader;
>    UINT16 SocketIdentifier;
>    UINT16 Reserved;
>    EFI_ACPI_6_4_PMTT_COMMON_MEMORY_DEVICE  MemoryDeviceStructure[]
> }

This change might be okay. Both variants may be size (and
layout)-equivalent. See https://godbolt.org/z/364e4T4a7. You need to
check if e.g there's any padding after Reserved and before
MemoryDeviceStructure.
Basically make sure member offsets and sizes remain stable.

Thanks,
Pedro


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117455): https://edk2.groups.io/g/devel/message/117455
Mute This Topic: https://groups.io/mt/105305209/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-04-05  7:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-03  9:27 [edk2-devel] [Question] using Flexible Array Member in Structure levi.yun
2024-04-03 16:17 ` Michael D Kinney
2024-04-05  6:43   ` levi.yun
2024-04-05  7:29     ` Pedro Falcato
2024-04-05  7:34 ` Pedro Falcato

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox