Test the alignment of the refcounted box allocations

Check that the allocations are aligned regardless of the block size.
This commit is contained in:
Emmanuele Bassi 2018-11-14 17:49:26 +00:00
parent 87f0a5a219
commit f81723e675

View File

@ -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 ();
}