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



commit 595a92644511a8a32151cd146b0a513a48edf6ac
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 | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
---
diff --git a/glib/tests/rcbox.c b/glib/tests/rcbox.c
index b1a1342bb..73126c7ea 100644
--- a/glib/tests/rcbox.c
+++ b/glib/tests/rcbox.c
@@ -220,6 +220,64 @@ test_atomic_rcbox_dup (void)
   g_assert_null (global_point_b);
 }
 
+/* The expected alignment of the refcounted data, absent any other
+ * alignment requirement, is `2 * sizeof(void*)`; GLib only really
+ * supports void* sized 8 or 4 (see the comment in gatomic.h)
+ */
+#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)
+{
+  const gsize block_sizes[] = {
+    1,
+    2,
+    4,
+    sizeof (gint32) * 3,
+  };
+
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (block_sizes); i++)
+    {
+      gpointer p = g_rc_box_alloc0 (block_sizes[i]);
+
+      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)
+{
+  const gsize block_sizes[] = {
+    1,
+    2,
+    4,
+    sizeof (gint32) * 3,
+  };
+
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (block_sizes); i++)
+    {
+      gpointer p = g_atomic_rc_box_alloc0 (block_sizes[i]);
+
+      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 +287,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]