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