4 /* We test deprecated functionality here */
5 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
9 test_slice_nodebug (void)
13 oldval
= g_getenv ("G_SLICE");
14 g_unsetenv ("G_SLICE");
16 if (g_test_subprocess ())
20 p
= g_slice_alloc (237);
21 q
= g_slice_alloc (259);
22 g_slice_free1 (237, p
);
23 g_slice_free1 (259, q
);
25 g_slice_debug_tree_statistics ();
28 g_test_trap_subprocess (NULL
, 1000000, 0);
29 g_test_trap_assert_passed ();
30 g_test_trap_assert_stderr ("*GSlice: MemChecker: root=NULL*");
33 g_setenv ("G_SLICE", oldval
, TRUE
);
37 test_slice_debug (void)
41 oldval
= g_getenv ("G_SLICE");
42 g_setenv ("G_SLICE", "debug-blocks:always-malloc", TRUE
);
44 if (g_test_subprocess ())
48 p
= g_slice_alloc (237);
49 q
= g_slice_alloc (259);
50 g_slice_free1 (237, p
);
51 g_slice_free1 (259, q
);
53 g_slice_debug_tree_statistics ();
56 g_test_trap_subprocess (NULL
, 1000000, 0);
57 g_test_trap_assert_passed ();
58 g_test_trap_assert_stderr ("*GSlice: MemChecker: * trunks, * branches, * old branches*");
61 g_setenv ("G_SLICE", oldval
, TRUE
);
63 g_unsetenv ("G_SLICE");
68 test_slice_copy (void)
70 const gchar
*block
= "0123456789ABCDEF";
73 p
= g_slice_copy (12, block
);
74 g_assert (memcmp (p
, block
, 12) == 0);
75 g_slice_free1 (12, p
);
89 TestStruct
*ts
, *head
;
91 head
= ts
= g_slice_new (TestStruct
);
92 ts
->next
= g_slice_new (TestStruct
);
94 ts
->next
= g_slice_new (TestStruct
);
98 g_slice_free_chain (TestStruct
, head
, next
);
101 static gpointer chunks
[4096][30];
104 thread_allocate (gpointer data
)
110 volatile gpointer
*loc
;
112 for (i
= 0; i
< 10000; i
++)
114 b
= g_random_int_range (0, 30);
115 size
= g_random_int_range (0, 4096);
116 loc
= &(chunks
[size
][b
]);
118 p
= g_atomic_pointer_get (loc
);
121 p
= g_slice_alloc (size
+ 1);
122 if (!g_atomic_pointer_compare_and_exchange (loc
, NULL
, p
))
123 g_slice_free1 (size
+ 1, p
);
127 if (g_atomic_pointer_compare_and_exchange (loc
, p
, NULL
))
128 g_slice_free1 (size
+ 1, p
);
138 GThread
*threads
[30];
142 for (i
= 0; i
< 30; i
++)
143 for (size
= 1; size
<= 4096; size
++)
144 chunks
[size
- 1][i
] = NULL
;
146 for (i
= 0; i
< G_N_ELEMENTS(threads
); i
++)
147 threads
[i
] = g_thread_create (thread_allocate
, NULL
, TRUE
, NULL
);
149 for (i
= 0; i
< G_N_ELEMENTS(threads
); i
++)
150 g_thread_join (threads
[i
]);
154 main (int argc
, char **argv
)
156 g_test_init (&argc
, &argv
, NULL
);
158 #ifdef G_ENABLE_DEBUG
159 g_test_add_func ("/slice/nodebug", test_slice_nodebug
);
160 g_test_add_func ("/slice/debug", test_slice_debug
);
162 g_test_add_func ("/slice/copy", test_slice_copy
);
163 g_test_add_func ("/slice/chain", test_chain
);
164 g_test_add_func ("/slice/allocate", test_allocate
);
166 return g_test_run ();