[v6,02/30] eal/include: add new packing macros

Message ID 1732668761-5556-3-git-send-email-andremue@linux.microsoft.com (mailing list archive)
State Superseded
Delegated to: David Marchand
Headers
Series fix packing of structs when building with MSVC |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Andre Muezerie Nov. 27, 2024, 12:52 a.m. UTC
MSVC struct packing is not compatible with GCC. Add macro
__rte_packed_begin which can be used to push existing pack value
and set packing to 1-byte. Add macro __rte_packed_end to restore
the pack value prior to the push.

Macro __rte_packed_end is deliberately utilized to trigger a
MSVC compiler warning if no existing packing has been pushed allowing
easy identification of locations where the __rte_packed_begin is
missing.

Macro __rte_packed will be removed in a subsequent patch.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/eal/include/rte_common.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
  

Comments

Tyler Retzlaff Dec. 5, 2024, 12:09 a.m. UTC | #1
On Tue, Nov 26, 2024 at 04:52:13PM -0800, Andre Muezerie wrote:
> MSVC struct packing is not compatible with GCC. Add macro
> __rte_packed_begin which can be used to push existing pack value
> and set packing to 1-byte. Add macro __rte_packed_end to restore
> the pack value prior to the push.
> 
> Macro __rte_packed_end is deliberately utilized to trigger a
> MSVC compiler warning if no existing packing has been pushed allowing
> easy identification of locations where the __rte_packed_begin is
> missing.
> 
> Macro __rte_packed will be removed in a subsequent patch.
> 
> Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
> ---

Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
  

Patch

diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index 4d299f2b36..affdcaf3c1 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -108,6 +108,17 @@  typedef uint16_t unaligned_uint16_t;
 #define __rte_packed __attribute__((__packed__))
 #endif
 
+/**
+ * Force a structure to be packed
+ */
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __rte_packed_begin __pragma(pack(push, 1))
+#define __rte_packed_end __pragma(pack(pop))
+#else
+#define __rte_packed_begin
+#define __rte_packed_end __attribute__((__packed__))
+#endif
+
 /**
  * Macro to mark a type that is not subject to type-based aliasing rules
  */