[kdbus] sync with kdbus (kdbus.h - commit: 5ae1ecac44cb)
[platform/upstream/glib.git] / glib / tests / slice.c
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 test_slice_config (void)
9 {
10   if (g_test_subprocess ())
11     {
12       g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
13       return;
14     }
15   g_test_trap_subprocess (NULL, 1000000, 0);
16   g_test_trap_assert_failed ();
17 }
18
19 #ifdef G_ENABLE_DEBUG
20 static void
21 test_slice_nodebug (void)
22 {
23   const gchar *oldval;
24
25   oldval = g_getenv ("G_SLICE");
26   g_unsetenv ("G_SLICE");
27
28   if (g_test_subprocess ())
29     {
30       gpointer p, q;
31
32       p = g_slice_alloc (237);
33       q = g_slice_alloc (259);
34       g_slice_free1 (237, p);
35       g_slice_free1 (259, q);
36
37       g_slice_debug_tree_statistics ();
38       return;
39     }
40   g_test_trap_subprocess (NULL, 1000000, 0);
41   g_test_trap_assert_passed ();
42   g_test_trap_assert_stderr ("*GSlice: MemChecker: root=NULL*");
43
44   if (oldval)
45     g_setenv ("G_SLICE", oldval, TRUE);
46 }
47
48 static void
49 test_slice_debug (void)
50 {
51   const gchar *oldval;
52
53   oldval = g_getenv ("G_SLICE");
54   g_setenv ("G_SLICE", "debug-blocks:always-malloc", TRUE);
55
56   if (g_test_subprocess ())
57     {
58       gpointer p, q;
59
60       p = g_slice_alloc (237);
61       q = g_slice_alloc (259);
62       g_slice_free1 (237, p);
63       g_slice_free1 (259, q);
64
65       g_slice_debug_tree_statistics ();
66       return;
67     }
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*");
71
72   if (oldval)
73     g_setenv ("G_SLICE", oldval, TRUE);
74   else
75     g_unsetenv ("G_SLICE");
76 }
77 #endif
78
79 static void
80 test_slice_copy (void)
81 {
82   const gchar *block = "0123456789ABCDEF";
83   gpointer p;
84
85   p = g_slice_copy (12, block);
86   g_assert (memcmp (p, block, 12) == 0);
87   g_slice_free1 (12, p);
88 }
89
90 typedef struct {
91   gint int1;
92   gint int2;
93   gchar byte;
94   gpointer next;
95   gint64 more;
96 } TestStruct;
97
98 static void
99 test_chain (void)
100 {
101   TestStruct *ts, *head;
102
103   head = ts = g_slice_new (TestStruct);
104   ts->next = g_slice_new (TestStruct);
105   ts = ts->next;
106   ts->next = g_slice_new (TestStruct);
107   ts = ts->next;
108   ts->next = NULL;
109
110   g_slice_free_chain (TestStruct, head, next);
111 }
112
113 static gpointer chunks[4096][30];
114
115 static gpointer
116 thread_allocate (gpointer data)
117 {
118   gint i;
119   gint b;
120   gint size;
121   gpointer p;
122   volatile gpointer *loc;
123
124   for (i = 0; i < 10000; i++)
125     {
126       b = g_random_int_range (0, 30);
127       size = g_random_int_range (0, 4096);
128       loc = &(chunks[size][b]);
129
130       p = g_atomic_pointer_get (loc);
131       if (p == NULL)
132         {
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);
136         }
137       else
138         {
139           if (g_atomic_pointer_compare_and_exchange (loc, p, NULL))
140             g_slice_free1 (size + 1, p);
141         }
142     }
143
144   return NULL;
145 }
146
147 static void
148 test_allocate (void)
149 {
150   GThread *threads[30];
151   gint size;
152   gint i;
153
154   for (i = 0; i < 30; i++)
155     for (size = 1; size <= 4096; size++)
156       chunks[size - 1][i] = NULL;
157
158   for (i = 0; i < G_N_ELEMENTS(threads); i++)
159     threads[i] = g_thread_create (thread_allocate, NULL, TRUE, NULL);
160
161   for (i = 0; i < G_N_ELEMENTS(threads); i++)
162     g_thread_join (threads[i]);
163 }
164
165 int
166 main (int argc, char **argv)
167 {
168   /* have to do this before using gtester since it uses gslice */
169   gboolean was;
170
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);
175
176   g_test_init (&argc, &argv, NULL);
177
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);
182 #endif
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);
186
187   return g_test_run ();
188 }