[glib/wip/chergert/fix-1231: 1/2] glib: add G_GNUC_BEGIN_ALIGNED, G_GNUC_END_ALIGNED




commit 430dd97a6cf1a242d487cac4dcf5f314567efd49
Author: Christian Hergert <chergert redhat com>
Date:   Mon Nov 9 10:04:37 2020 -0800

    glib: add G_GNUC_BEGIN_ALIGNED, G_GNUC_END_ALIGNED
    
    This adds two new macros for specifying the alignment of structures. By having
    a begin and end pair of macros, we can support GCC and MSVC like compilers
    which have different requirements for where the attribute or declspec are
    placed syntactically.
    
    For example, the following would request the compiler uses an 8-byte boundary
    when placing the union on the stack, even though the largest type within the
    union may only be 4 bytes.
    
      G_GNUC_BEGIN_ALIGNED(8)
      union u
      {
        char c;
        int i;
        bool b;
      }
      G_GNUC_END_ALIGNED(8);
    
    For allocated structures, this can also help inform the compiler that all
    allocations of the structure will be guaranteed to be aligned with a given
    size. However, in that situation programs are still required to allocate and
    align to that size.

 docs/reference/glib/glib-sections.txt |  2 ++
 glib/gmacros.h                        | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 02bb2ba50..347aa5431 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -692,6 +692,8 @@ G_GNUC_NO_INSTRUMENT
 G_HAVE_GNUC_VISIBILITY
 G_GNUC_INTERNAL
 G_GNUC_MAY_ALIAS
+G_GNUC_BEGIN_ALIGNED
+G_GNUC_END_ALIGNED
 
 <SUBSECTION>
 G_DEPRECATED
diff --git a/glib/gmacros.h b/glib/gmacros.h
index d294fa90f..180d917b5 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -1118,4 +1118,34 @@
     GLIB_AVAILABLE_MACRO_IN_2_64 \
     sizeof (((struct_type *) 0)->member)
 
+/**
+ * G_GNUC_BEGIN_ALIGNED:
+ * @n: the size of the alignment in bytes
+ *
+ * The %G_GNUC_BEGIN_ALIGNED macro should be used in conjunction with
+ * %G_GNUC_END_ALIGNED before and after a structure definition. Doing so
+ * allows both GNU-like compilers and MSVC-like compilers the ability to
+ * specify the boundary alignment for the structure.
+ */
+/**
+ * G_GNUC_END_ALIGNED:
+ * @n: the size of the alignment in bytes
+ *
+ * The %G_GNUC_END_ALIGNED macro should be used in conjunction with
+ * %G_GNUC_BEGIN_ALIGNED before and after a structure definition. Doing so
+ * allows both GNU-like compilers and MSVC-like compilers the ability to
+ * specify the boundary alignment for the structure.
+ */
+#if G_GNUC_CHECK_VERSION(3, 0)
+# define G_GNUC_BEGIN_ALIGNED(n) GLIB_AVAILABLE_MACRO_IN_2_68
+# define G_GNUC_END_ALIGNED(n) __attribute__((aligned(n))) GLIB_AVAILABLE_MACRO_IN_2_68
+#elif defined(_MSC_VER)
+# define G_GNUC_BEGIN_ALIGNED(n) __declspec(align(n)) GLIB_AVAILABLE_MACRO_IN_2_68
+# define G_GNUC_END_ALIGNED(n) GLIB_AVAILABLE_MACRO_IN_2_68
+#else
+# warning "Cannot specify structure alignment for unknown compiler. Runtime errors may occur."
+# define G_GNUC_BEGIN_ALIGNED(n) GLIB_AVAILABLE_MACRO_IN_2_68
+# define G_GNUC_END_ALIGNED(n) GLIB_AVAILABLE_MACRO_IN_2_68
+#endif
+
 #endif /* __G_MACROS_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]