4 /* We test deprecated functionality here */
5 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
8 test_slice_config (void)
10 if (g_test_subprocess ())
12 g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
15 g_test_trap_subprocess (NULL, 1000000, 0);
16 g_test_trap_assert_failed ();
21 test_slice_nodebug (void)
25 oldval = g_getenv ("G_SLICE");
26 g_unsetenv ("G_SLICE");
28 if (g_test_subprocess ())
32 p = g_slice_alloc (237);
33 q = g_slice_alloc (259);
34 g_slice_free1 (237, p);
35 g_slice_free1 (259, q);
37 g_slice_debug_tree_statistics ();
40 g_test_trap_subprocess (NULL, 1000000, 0);
41 g_test_trap_assert_passed ();
42 g_test_trap_assert_stderr ("*GSlice: MemChecker: root=NULL*");
45 g_setenv ("G_SLICE", oldval, TRUE);
49 test_slice_debug (void)
53 oldval = g_getenv ("G_SLICE");
54 g_setenv ("G_SLICE", "debug-blocks:always-malloc", TRUE);
56 if (g_test_subprocess ())
60 p = g_slice_alloc (237);
61 q = g_slice_alloc (259);
62 g_slice_free1 (237, p);
63 g_slice_free1 (259, q);
65 g_slice_debug_tree_statistics ();
68 g_test_trap_subprocess (NULL, 1000000, 0);
69 g_test_trap_assert_passed ();
70 g_test_trap_assert_stderr ("*GSlice: MemChecker: * trunks, * branches, * old branches*");
73 g_setenv ("G_SLICE", oldval, TRUE);
75 g_unsetenv ("G_SLICE");
80 test_slice_copy (void)
82 const gchar *block = "0123456789ABCDEF";
85 p = g_slice_copy (12, block);
86 g_assert (memcmp (p, block, 12) == 0);
87 g_slice_free1 (12, p);
101 TestStruct *ts, *head;
103 head = ts = g_slice_new (TestStruct);
104 ts->next = g_slice_new (TestStruct);
106 ts->next = g_slice_new (TestStruct);
110 g_slice_free_chain (TestStruct, head, next);
113 static gpointer chunks[4096][30];
116 thread_allocate (gpointer data)
122 volatile gpointer *loc;
124 for (i = 0; i < 10000; i++)
126 b = g_random_int_range (0, 30);
127 size = g_random_int_range (0, 4096);
128 loc = &(chunks[size][b]);
130 p = g_atomic_pointer_get (loc);
133 p = g_slice_alloc (size + 1);
134 if (!g_atomic_pointer_compare_and_exchange (loc, NULL, p))
135 g_slice_free1 (size + 1, p);
139 if (g_atomic_pointer_compare_and_exchange (loc, p, NULL))
140 g_slice_free1 (size + 1, p);
150 GThread *threads[30];
154 for (i = 0; i < 30; i++)
155 for (size = 1; size <= 4096; size++)
156 chunks[size - 1][i] = NULL;
158 for (i = 0; i < G_N_ELEMENTS(threads); i++)
159 threads[i] = g_thread_create (thread_allocate, NULL, TRUE, NULL);
161 for (i = 0; i < G_N_ELEMENTS(threads); i++)
162 g_thread_join (threads[i]);
166 main (int argc, char **argv)
168 /* have to do this before using gtester since it uses gslice */
171 was = g_slice_get_config (G_SLICE_CONFIG_ALWAYS_MALLOC);
172 g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, !was);
173 g_assert_cmpint (g_slice_get_config (G_SLICE_CONFIG_ALWAYS_MALLOC), !=, was);
174 g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, was);
176 g_test_init (&argc, &argv, NULL);
178 g_test_add_func ("/slice/config", test_slice_config);
179 #ifdef G_ENABLE_DEBUG
180 g_test_add_func ("/slice/nodebug", test_slice_nodebug);
181 g_test_add_func ("/slice/debug", test_slice_debug);
183 g_test_add_func ("/slice/copy", test_slice_copy);
184 g_test_add_func ("/slice/chain", test_chain);
185 g_test_add_func ("/slice/allocate", test_allocate);
187 return g_test_run ();