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
|
#include <string.h>
#include <glib.h>
/* We test deprecated functionality here */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
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;
gpointer *loc; /* (atomic) */
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;
gsize 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)
{
g_test_init (&argc, &argv, NULL);
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 ();
}
|