[glib/wip/chergert/fix-1231] gobject: denote GObject as 8-byte aligned




commit cfed0f6d2de9bac8e6d0de5fc0f3e6bd66133b2b
Author: Christian Hergert <chergert redhat com>
Date:   Mon Nov 9 10:10:07 2020 -0800

    gobject: denote GObject as 8-byte aligned
    
    We already ensure that alignments happen to an 8-byte boundary so that
    you can store any type within the structures. This lets the compiler know
    about that requirement so that we can squash a number of warnings on
    various compiler and architecture configurations.
    
    We also ensure this for private instance data as that is a reasonable
    expectation given the standalone nature of Private typedef's.
    
    Fixes #1231

 gobject/gobject.c |  4 +++-
 gobject/gobject.h |  4 +++-
 gobject/gtype.c   | 10 ++++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)
---
diff --git a/gobject/gobject.c b/gobject/gobject.c
index d8a31a3df..48b1c8b69 100644
--- a/gobject/gobject.c
+++ b/gobject/gobject.c
@@ -169,6 +169,7 @@ enum {
 #define HAVE_OPTIONAL_FLAGS
 #endif
 
+G_GNUC_BEGIN_ALIGNED(8)
 typedef struct
 {
   GTypeInstance  g_type_instance;
@@ -179,7 +180,8 @@ typedef struct
   volatile guint optional_flags;
 #endif
   GData         *qdata;
-} GObjectReal;
+} GObjectReal
+G_GNUC_BEGIN_ALIGNED(8);
 
 G_STATIC_ASSERT(sizeof(GObject) == sizeof(GObjectReal));
 G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, ref_count) == G_STRUCT_OFFSET(GObjectReal, ref_count));
diff --git a/gobject/gobject.h b/gobject/gobject.h
index bf5496c54..3802a640f 100644
--- a/gobject/gobject.h
+++ b/gobject/gobject.h
@@ -242,6 +242,7 @@ typedef void (*GWeakNotify)         (gpointer      data,
  * All the fields in the GObject structure are private 
  * to the #GObject implementation and should never be accessed directly.
  */
+G_GNUC_BEGIN_ALIGNED(8)
 struct  _GObject
 {
   GTypeInstance  g_type_instance;
@@ -249,7 +250,8 @@ struct  _GObject
   /*< private >*/
   volatile guint ref_count;
   GData         *qdata;
-};
+}
+G_GNUC_END_ALIGNED(8);
 /**
  * GObjectClass:
  * @g_type_class: the parent class
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 12ad8be28..c8e3d667e 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -1878,6 +1878,9 @@ g_type_create_instance (GType type)
   private_size = node->data->instance.private_size;
   ivar_size = node->data->instance.instance_size;
 
+  g_assert ((private_size & 0x7) == 0);
+  g_assert ((ivar_size & 0x7) == 0);
+
 #ifdef ENABLE_VALGRIND
   if (private_size && RUNNING_ON_VALGRIND)
     {
@@ -1923,6 +1926,8 @@ g_type_create_instance (GType type)
 
   TRACE(GOBJECT_OBJECT_NEW(instance, type));
 
+  g_assert ((GPOINTER_TO_SIZE (instance) & 0x7) == 0);
+
   return instance;
 }
 
@@ -4777,6 +4782,9 @@ g_type_instance_get_private (GTypeInstance *instance,
       return NULL;
     }
 
+  /* Ensure we maintain alignment requirements */
+  g_assert ((node->data->instance.private_size & 0x7) == 0);
+
   return ((gchar *) instance) - node->data->instance.private_size;
 }
 
@@ -4825,6 +4833,8 @@ g_type_class_get_instance_private_offset (gpointer g_class)
     g_error ("g_type_class_get_instance_private_offset() called on class %s but it has no private data",
              g_type_name (instance_type));
 
+  g_assert ((node->data->instance.private_size & 0x7) == 0);
+
   return -(gint) node->data->instance.private_size;
 }
 


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