diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2018-11-14 17:49:26 +0000 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2018-12-18 11:18:43 +0000 |
commit | f81723e6750981b33c0dbdd2dac3149e353ac332 (patch) | |
tree | 395849992ae60a800e398980b9c52b3c01257696 /glib | |
parent | 87f0a5a219714616874a6a55d1218f9b98181d5b (diff) | |
download | glib-f81723e6750981b33c0dbdd2dac3149e353ac332.tar.gz |
Test the alignment of the refcounted box allocations
Check that the allocations are aligned regardless of the block size.
Diffstat (limited to 'glib')
-rw-r--r-- | glib/tests/rcbox.c | 60 |
1 files changed, 60 insertions, 0 deletions
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 (); } |