(root)/
glib-2.79.0/
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_copy (void)
       9  {
      10    const gchar *block = "0123456789ABCDEF";
      11    gpointer p;
      12  
      13    p = g_slice_copy (12, block);
      14    g_assert (memcmp (p, block, 12) == 0);
      15    g_slice_free1 (12, p);
      16  }
      17  
      18  typedef struct {
      19    gint int1;
      20    gint int2;
      21    gchar byte;
      22    gpointer next;
      23    gint64 more;
      24  } TestStruct;
      25  
      26  static void
      27  test_chain (void)
      28  {
      29    TestStruct *ts, *head;
      30  
      31    head = ts = g_slice_new (TestStruct);
      32    ts->next = g_slice_new (TestStruct);
      33    ts = ts->next;
      34    ts->next = g_slice_new (TestStruct);
      35    ts = ts->next;
      36    ts->next = NULL;
      37  
      38    g_slice_free_chain (TestStruct, head, next);
      39  }
      40  
      41  static gpointer chunks[4096][30];
      42  
      43  static gpointer
      44  thread_allocate (gpointer data)
      45  {
      46    gint i;
      47    gint b;
      48    gint size;
      49    gpointer p;
      50    gpointer *loc;  /* (atomic) */
      51  
      52    for (i = 0; i < 10000; i++)
      53      {
      54        b = g_random_int_range (0, 30);
      55        size = g_random_int_range (0, 4096);
      56        loc = &(chunks[size][b]);
      57  
      58        p = g_atomic_pointer_get (loc);
      59        if (p == NULL)
      60          {
      61            p = g_slice_alloc (size + 1);
      62            if (!g_atomic_pointer_compare_and_exchange (loc, NULL, p))
      63              g_slice_free1 (size + 1, p);
      64          }
      65        else
      66          {
      67            if (g_atomic_pointer_compare_and_exchange (loc, p, NULL))
      68              g_slice_free1 (size + 1, p);
      69          }
      70      }
      71  
      72    return NULL;
      73  }
      74  
      75  static void
      76  test_allocate (void)
      77  {
      78    GThread *threads[30];
      79    gint size;
      80    gsize i;
      81  
      82    for (i = 0; i < 30; i++)
      83      for (size = 1; size <= 4096; size++)
      84        chunks[size - 1][i] = NULL;
      85  
      86    for (i = 0; i < G_N_ELEMENTS(threads); i++)
      87      threads[i] = g_thread_create (thread_allocate, NULL, TRUE, NULL);
      88  
      89    for (i = 0; i < G_N_ELEMENTS(threads); i++)
      90      g_thread_join (threads[i]);
      91  }
      92  
      93  int
      94  main (int argc, char **argv)
      95  {
      96    g_test_init (&argc, &argv, NULL);
      97  
      98    g_test_add_func ("/slice/copy", test_slice_copy);
      99    g_test_add_func ("/slice/chain", test_chain);
     100    g_test_add_func ("/slice/allocate", test_allocate);
     101  
     102    return g_test_run ();
     103  }