summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Hergert <chergert@redhat.com>2020-11-09 10:10:07 -0800
committerChristian Hergert <chergert@redhat.com>2020-11-09 10:37:18 -0800
commit9260f6a83ad4dfbeac63e930caee3deb4c0b5a56 (patch)
tree1ba5e6802547d210c3bf6c9402bba29fad5c73c9
parent430dd97a6cf1a242d487cac4dcf5f314567efd49 (diff)
downloadglib-wip/chergert/fix-1231.tar.gz
gobject: denote GObject as 8-byte alignedwip/chergert/fix-1231
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
-rw-r--r--gobject/gobject.c4
-rw-r--r--gobject/gobject.h4
-rw-r--r--gobject/gtype.c10
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;
}