Branch data Line data Source code
1 : : #include <string.h> 2 : : #include <glib.h> 3 : : 4 : : /* We test deprecated functionality here */ 5 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS 6 : : 7 : : static void 8 : 1 : test_slice_copy (void) 9 : : { 10 : 1 : const gchar *block = "0123456789ABCDEF"; 11 : : gpointer p; 12 : : 13 : 1 : p = g_slice_copy (12, block); 14 : 1 : g_assert (memcmp (p, block, 12) == 0); 15 : 1 : g_slice_free1 (12, p); 16 : 1 : } 17 : : 18 : : typedef struct { 19 : : gint int1; 20 : : gint int2; 21 : : gchar byte; 22 : : gpointer next; 23 : : gint64 more; 24 : : } TestStruct; 25 : : 26 : : static void 27 : 1 : test_chain (void) 28 : : { 29 : : TestStruct *ts, *head; 30 : : 31 : 1 : head = ts = g_slice_new (TestStruct); 32 : 1 : ts->next = g_slice_new (TestStruct); 33 : 1 : ts = ts->next; 34 : 1 : ts->next = g_slice_new (TestStruct); 35 : 1 : ts = ts->next; 36 : 1 : ts->next = NULL; 37 : : 38 : 1 : g_slice_free_chain (TestStruct, head, next); 39 : 1 : } 40 : : 41 : : static gpointer chunks[4096][30]; 42 : : 43 : : static gpointer 44 : 30 : thread_allocate (gpointer data) 45 : : { 46 : : gint i; 47 : : gint b; 48 : : gint size; 49 : : gpointer p; 50 : : gpointer *loc; /* (atomic) */ 51 : : 52 [ + + ]: 300030 : for (i = 0; i < 10000; i++) 53 : : { 54 : 300000 : b = g_random_int_range (0, 30); 55 : 300000 : size = g_random_int_range (0, 4096); 56 : 300000 : loc = &(chunks[size][b]); 57 : : 58 : 300000 : p = g_atomic_pointer_get (loc); 59 [ + + ]: 300000 : if (p == NULL) 60 : : { 61 : 180613 : p = g_slice_alloc (size + 1); 62 [ + + ]: 180613 : if (!g_atomic_pointer_compare_and_exchange (loc, NULL, p)) 63 : 2 : g_slice_free1 (size + 1, p); 64 : : } 65 : : else 66 : : { 67 [ + - ]: 119387 : if (g_atomic_pointer_compare_and_exchange (loc, p, NULL)) 68 : 119387 : g_slice_free1 (size + 1, p); 69 : : } 70 : : } 71 : : 72 : 30 : return NULL; 73 : : } 74 : : 75 : : static void 76 : 1 : test_allocate (void) 77 : : { 78 : : GThread *threads[30]; 79 : : gint size; 80 : : gsize i; 81 : : 82 [ + + ]: 31 : for (i = 0; i < 30; i++) 83 [ + + ]: 122910 : for (size = 1; size <= 4096; size++) 84 : 122880 : chunks[size - 1][i] = NULL; 85 : : 86 [ + + ]: 31 : for (i = 0; i < G_N_ELEMENTS(threads); i++) 87 : 30 : threads[i] = g_thread_create (thread_allocate, NULL, TRUE, NULL); 88 : : 89 [ + + ]: 31 : for (i = 0; i < G_N_ELEMENTS(threads); i++) 90 : 30 : g_thread_join (threads[i]); 91 : 1 : } 92 : : 93 : : int 94 : 1 : main (int argc, char **argv) 95 : : { 96 : 1 : g_test_init (&argc, &argv, NULL); 97 : : 98 : 1 : g_test_add_func ("/slice/copy", test_slice_copy); 99 : 1 : g_test_add_func ("/slice/chain", test_chain); 100 : 1 : g_test_add_func ("/slice/allocate", test_allocate); 101 : : 102 : 1 : return g_test_run (); 103 : : }