[glib/rc-align: 34/34] Test the alignment of the refcounted box allocations



commit c5c57beadeb5ae652a890b6ba3fe6babb427b2c6
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Nov 14 17:49:26 2018 +0000

    Test the alignment of the refcounted box allocations
    
    Check that the allocations are aligned regardless of the block size.

 glib/tests/rcbox.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
---
diff --git a/glib/tests/rcbox.c b/glib/tests/rcbox.c
index b1a1342bb..3aebd8cd2 100644
--- a/glib/tests/rcbox.c
+++ b/glib/tests/rcbox.c
@@ -220,6 +220,38 @@ test_atomic_rcbox_dup (void)
   g_assert_null (global_point_b);
 }
 
+#if GLIB_SIZEOF_VOID_P == 8
+static const gsize rcbox_alignment = 16;
+#else
+static const gsize rcbox_alignment = 8;
+#endif
+
+/* verify that the refcounted allocation is properly aligned */
+static void
+test_rcbox_alignment (void)
+{
+  gsize block_size = sizeof (gint32) * 3;
+  gpointer p = g_rc_box_alloc0 (block_size);
+
+  g_assert_nonnull (p);
+  g_assert_true (((guintptr) p & (rcbox_alignment - 1)) == 0);
+
+  g_rc_box_release (p);
+}
+
+/* verify that the atomically refcounted allocation is properly aligned */
+static void
+test_atomic_rcbox_alignment (void)
+{
+  gsize block_size = sizeof (gint32) * 3;
+  gpointer p = g_atomic_rc_box_alloc0 (block_size);
+
+  g_assert_nonnull (p);
+  g_assert_true (((guintptr) p & (rcbox_alignment - 1)) == 0);
+
+  g_atomic_rc_box_release (p);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -229,10 +261,12 @@ main (int   argc,
   g_test_add_func ("/rcbox/new", test_rcbox_new);
   g_test_add_func ("/rcbox/release-full", test_rcbox_release_full);
   g_test_add_func ("/rcbox/dup", test_rcbox_dup);
+  g_test_add_func ("/rcbox/alignment", test_rcbox_alignment);
 
   g_test_add_func ("/atomic-rcbox/new", test_atomic_rcbox_new);
   g_test_add_func ("/atomic-rcbox/release-full", test_atomic_rcbox_release_full);
   g_test_add_func ("/atomic-rcbox/dup", test_atomic_rcbox_dup);
+  g_test_add_func ("/atomic-rcbox/alignment", test_atomic_rcbox_alignment);
 
   return g_test_run ();
 }


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