[glib/th/g-ptr-array-set-null-terminated] array: add support for %NULL termination in GPtrArray



commit 1d4dcbb58cca9490ea57e366856ea3d4f39356e2
Author: Thomas Haller <thaller redhat com>
Date:   Wed May 6 19:35:37 2020 +0200

    array: add support for %NULL termination in GPtrArray
    
    GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
    This is odd, because especially for a pointer array it makes sense
    to have a NULL sentinel. This would be for example useful to track a
    strv array with a GPtrArray.
    
    As workaround for this missing feature you could use a GArray instead
    (ugly) or to explicitly add the NULL sentinel. However the latter increases
    the "len" of the array, which can be problematic if you want to still use
    the GPtrArray for other purposes.
    
    Add API for marking a GPtrArray as %NULL terminated. In that case, the
    API will ensure that there is always a valid %NULL sentinel after the
    array. Note that the API does not enforce that a %NULL terminated API
    actually has any data allocated. The only exception is g_ptr_array_free(),
    which never returns %NULL for %NULL terminated arrays.
    
    Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
    cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
    sticks. The purpose is that once a user checks whether a GPtrArray instance
    is safe to be treated as a %NULL terminated array, the decision does
    not need to be re-evaluated.
    
    Also add a g_ptr_array_is_null_terminated(). That is useful because it
    allows you to check whether a GPtrArray created by somebody else is safe
    to use as a %NULL terminated array. Since there is no API to make an
    array not %NULL terminated anymore, this is not error prone.
    
    The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
    architectures this does not increase the size of the struct as it fits
    in an existing hole. Note that this is not a bitfield because it's
    probably more efficient to access the entire guint8. However, there is
    still a 3 bytes hole (on common 32 and 64 architectures), so if we need
    to add more flags in the future, we still have space for 24 bits,
    despite the new flag not being a bitfield.
    
    The biggest downside of the patch is the runtime overhead that most
    operations now need to check whether %NULL termination is requested.
    
    https://gitlab.gnome.org/GNOME/glib/-/issues/353

 glib/garray.c           | 174 ++++++++++++++++++++++++++++++++++++++++--------
 glib/garray.h           |   7 ++
 glib/tests/array-test.c | 150 +++++++++++++++++++++++++++++++----------
 3 files changed, 266 insertions(+), 65 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 4d29bc068..d76c517bd 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1052,6 +1052,7 @@ struct _GRealPtrArray
   guint           len;
   guint           alloc;
   gatomicrefcount ref_count;
+  guint8          null_terminated;
   GDestroyNotify  element_free_func;
 };
 
@@ -1071,9 +1072,17 @@ struct _GRealPtrArray
 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
                                       guint          len);
 
+static void
+ptr_array_null_terminate (GRealPtrArray *rarray)
+{
+  if (G_UNLIKELY (rarray->null_terminated))
+    rarray->pdata[rarray->len] = NULL;
+}
+
 static GPtrArray *
 ptr_array_new (guint reserved_size,
-               GDestroyNotify element_free_func)
+               GDestroyNotify element_free_func,
+               gboolean null_terminated)
 {
   GRealPtrArray *array;
 
@@ -1082,12 +1091,18 @@ ptr_array_new (guint reserved_size,
   array->pdata = NULL;
   array->len = 0;
   array->alloc = 0;
+  array->null_terminated = (!!null_terminated);
   array->element_free_func = element_free_func;
 
   g_atomic_ref_count_init (&array->ref_count);
 
   if (reserved_size != 0)
-    g_ptr_array_maybe_expand (array, reserved_size);
+    {
+      if (G_LIKELY (reserved_size < G_MAXUINT))
+        reserved_size += array->null_terminated;
+      g_ptr_array_maybe_expand (array, reserved_size);
+      ptr_array_null_terminate (array);
+    }
 
   return (GPtrArray *) array;
 }
@@ -1102,7 +1117,7 @@ ptr_array_new (guint reserved_size,
 GPtrArray*
 g_ptr_array_new (void)
 {
-  return ptr_array_new (0, NULL);
+  return ptr_array_new (0, NULL, FALSE);
 }
 
 /**
@@ -1196,7 +1211,8 @@ g_ptr_array_steal (GPtrArray *array,
  * pointing to) are copied to the new #GPtrArray.
  *
  * The copy of @array will have the same #GDestroyNotify for its elements as
- * @array.
+ * @array. The copy will also be %NULL terminated if (and only if) the source
+ * array is.
  *
  * Returns: (transfer full): a deep copy of the initial #GPtrArray.
  *
@@ -1207,27 +1223,39 @@ g_ptr_array_copy (GPtrArray *array,
                   GCopyFunc  func,
                   gpointer   user_data)
 {
+  GRealPtrArray *rarray = (GRealPtrArray *) array;
   GPtrArray *new_array;
 
   g_return_val_if_fail (array != NULL, NULL);
 
-  new_array = ptr_array_new (array->len,
-                             ((GRealPtrArray *) array)->element_free_func);
+  new_array = ptr_array_new (0,
+                             rarray->element_free_func,
+                             rarray->null_terminated);
 
-  if (func != NULL)
+  if (rarray->alloc > 0)
     {
-      guint i;
+      g_ptr_array_maybe_expand ((GRealPtrArray *) new_array, array->len + rarray->null_terminated);
 
-      for (i = 0; i < array->len; i++)
-        new_array->pdata[i] = func (array->pdata[i], user_data);
-    }
-  else if (array->len > 0)
-    {
-      memcpy (new_array->pdata, array->pdata,
-              array->len * sizeof (*array->pdata));
-    }
+      if (array->len > 0)
+        {
+          if (func != NULL)
+            {
+              guint i;
 
-  new_array->len = array->len;
+              for (i = 0; i < array->len; i++)
+                new_array->pdata[i] = func (array->pdata[i], user_data);
+            }
+          else
+            {
+              memcpy (new_array->pdata, array->pdata,
+                      array->len * sizeof (*array->pdata));
+            }
+
+          new_array->len = array->len;
+        }
+
+      ptr_array_null_terminate (rarray);
+    }
 
   return new_array;
 }
@@ -1246,7 +1274,7 @@ g_ptr_array_copy (GPtrArray *array,
 GPtrArray*
 g_ptr_array_sized_new (guint reserved_size)
 {
-  return ptr_array_new (reserved_size, NULL);
+  return ptr_array_new (reserved_size, NULL, FALSE);
 }
 
 /**
@@ -1297,7 +1325,7 @@ g_array_copy (GArray *array)
 GPtrArray*
 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
 {
-  return ptr_array_new (0, element_free_func);
+  return ptr_array_new (0, element_free_func, FALSE);
 }
 
 /**
@@ -1322,7 +1350,42 @@ GPtrArray*
 g_ptr_array_new_full (guint          reserved_size,
                       GDestroyNotify element_free_func)
 {
-  return ptr_array_new (reserved_size, element_free_func);
+  return ptr_array_new (reserved_size, element_free_func, FALSE);
+}
+
+/**
+ * g_ptr_array_new_null_terminated:
+ * @reserved_size: number of pointers preallocated.
+ *     If @null_terminated is %TRUE, the actually allocated
+ *     buffer size is @reserved_size plus 1, unless @reserved_size
+ *     is zero, in which case no initial buffers gets allocated.
+ * @element_free_func: (nullable): A function to free elements with
+ *     destroy @array or %NULL
+ * @null_terminated: whether to make the array as %NULL terminated.
+ *
+ * Like g_ptr_array_new_full() but also allows to set the array to
+ * be %NULL terminated.
+ *
+ * #GPtrArray created by other constructors are not %NULL terminated.
+ *
+ * Note that if the @array's length is zero and currently no
+ * data array is allocated, then pdata will still be %NULL.
+ * %GPtrArray will only %NULL terminate pdata, if an actual
+ * array is allocated. It does not guarantee that an array
+ * is always allocated. In other words, if the length is zero,
+ * then pdata may either point to a %NULL terminated array of length
+ * zero or be %NULL.
+ *
+ * Returns: A new #GPtrArray
+ *
+ * Since: 2.66
+ */
+GPtrArray*
+g_ptr_array_new_null_terminated (guint          reserved_size,
+                                 GDestroyNotify element_free_func,
+                                 gboolean       null_terminated)
+{
+  return ptr_array_new (reserved_size, element_free_func, null_terminated);
 }
 
 /**
@@ -1348,6 +1411,22 @@ g_ptr_array_set_free_func (GPtrArray      *array,
   rarray->element_free_func = element_free_func;
 }
 
+/**
+ * g_ptr_array_is_null_terminated:
+ * @array: the #GPtrArray
+ *
+ * Returns: %TRUE if the array is made to be %NULL terminated.
+ *
+ * Since: 2.66
+ */
+gboolean
+g_ptr_array_is_null_terminated (const GPtrArray *array)
+{
+  g_return_val_if_fail (array, FALSE);
+
+  return ((GRealPtrArray *) array)->null_terminated;
+}
+
 /**
  * g_ptr_array_ref:
  * @array: a #GPtrArray
@@ -1411,6 +1490,9 @@ g_ptr_array_unref (GPtrArray *array)
  * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
  * function has been set for @array.
  *
+ * If the array is %NULL terminated, this will not return %NULL if %free_seg
+ * is unset.
+ *
  * This function is not thread-safe. If using a #GPtrArray from multiple
  * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
  * functions.
@@ -1466,7 +1548,12 @@ ptr_array_free (GPtrArray      *array,
       segment = NULL;
     }
   else
-    segment = rarray->pdata;
+    {
+      segment = rarray->pdata;
+      if (   !segment
+          && rarray->null_terminated)
+        segment = g_new0 (gpointer, 1);
+    }
 
   if (flags & PRESERVE_WRAPPER)
     {
@@ -1493,6 +1580,7 @@ g_ptr_array_maybe_expand (GRealPtrArray *array,
   if ((array->len + len) > array->alloc)
     {
       guint old_alloc = array->alloc;
+
       array->alloc = g_nearest_pow (array->len + len);
       array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
       array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
@@ -1528,7 +1616,13 @@ g_ptr_array_set_size  (GPtrArray *array,
   if (length_unsigned > rarray->len)
     {
       guint i;
-      g_ptr_array_maybe_expand (rarray, (length_unsigned - rarray->len));
+
+      if (   G_UNLIKELY (rarray->null_terminated)
+          && length_unsigned - rarray->len > G_MAXUINT - 1)
+         g_error ("array would overflow");
+
+      g_ptr_array_maybe_expand (rarray, (length_unsigned - rarray->len) + rarray->null_terminated);
+
       /* This is not 
        *     memset (array->pdata + array->len, 0,
        *            sizeof (gpointer) * (length_unsigned - array->len));
@@ -1537,11 +1631,13 @@ g_ptr_array_set_size  (GPtrArray *array,
        */
       for (i = rarray->len; i < length_unsigned; i++)
         rarray->pdata[i] = NULL;
+
+      rarray->len = length_unsigned;
+
+      ptr_array_null_terminate (rarray);
     }
   else if (length_unsigned < rarray->len)
     g_ptr_array_remove_range (array, length_unsigned, rarray->len - length_unsigned);
-
-  rarray->len = length_unsigned;
 }
 
 static gpointer
@@ -1571,7 +1667,8 @@ ptr_array_remove_index (GPtrArray *array,
 
   rarray->len -= 1;
 
-  if (G_UNLIKELY (g_mem_gc_friendly))
+  if (   G_UNLIKELY (g_mem_gc_friendly)
+      || rarray->null_terminated)
     rarray->pdata[rarray->len] = NULL;
 
   return result;
@@ -1687,6 +1784,10 @@ g_ptr_array_remove_range (GPtrArray *array,
   g_return_val_if_fail (rarray != NULL, NULL);
   g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
   g_return_val_if_fail (index_ <= rarray->len, NULL);
+
+  if (length == 0)
+    return array;
+
   g_return_val_if_fail (index_ + length <= rarray->len, NULL);
 
   if (rarray->element_free_func != NULL)
@@ -1708,6 +1809,8 @@ g_ptr_array_remove_range (GPtrArray *array,
       for (i = 0; i < length; i++)
         rarray->pdata[rarray->len + i] = NULL;
     }
+  else
+    ptr_array_null_terminate (rarray);
 
   return array;
 }
@@ -1804,9 +1907,11 @@ g_ptr_array_add (GPtrArray *array,
   g_return_if_fail (rarray);
   g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
 
-  g_ptr_array_maybe_expand (rarray, 1);
+  g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated);
 
   rarray->pdata[rarray->len++] = data;
+
+  ptr_array_null_terminate (rarray);
 }
 
 /**
@@ -1842,7 +1947,14 @@ g_ptr_array_extend (GPtrArray  *array_to_extend,
   g_return_if_fail (array_to_extend != NULL);
   g_return_if_fail (array != NULL);
 
-  g_ptr_array_maybe_expand (rarray_to_extend, array->len);
+  if (array->len == 0u)
+    return;
+
+  if (   G_UNLIKELY (array->len == G_MAXUINT)
+      && rarray_to_extend->null_terminated)
+    g_error ("adding %u to array would overflow", array->len);
+
+  g_ptr_array_maybe_expand (rarray_to_extend, array->len + rarray_to_extend->null_terminated);
 
   if (func != NULL)
     {
@@ -1852,13 +1964,15 @@ g_ptr_array_extend (GPtrArray  *array_to_extend,
         rarray_to_extend->pdata[i + rarray_to_extend->len] =
           func (array->pdata[i], user_data);
     }
-  else if (array->len > 0)
+  else
     {
       memcpy (rarray_to_extend->pdata + rarray_to_extend->len, array->pdata,
               array->len * sizeof (*array->pdata));
     }
 
   rarray_to_extend->len += array->len;
+
+  ptr_array_null_terminate (rarray_to_extend);
 }
 
 /**
@@ -1916,7 +2030,7 @@ g_ptr_array_insert (GPtrArray *array,
   g_return_if_fail (index_ >= -1);
   g_return_if_fail (index_ <= (gint)rarray->len);
 
-  g_ptr_array_maybe_expand (rarray, 1);
+  g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated);
 
   if (index_ < 0)
     index_ = rarray->len;
@@ -1928,6 +2042,8 @@ g_ptr_array_insert (GPtrArray *array,
 
   rarray->len++;
   rarray->pdata[index_] = data;
+
+  ptr_array_null_terminate (rarray);
 }
 
 /* Please keep this doc-comment in sync with pointer_array_sort_example()
diff --git a/glib/garray.h b/glib/garray.h
index 67131b5b3..f33172f46 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -152,6 +152,10 @@ GPtrArray* g_ptr_array_sized_new          (guint             reserved_size);
 GLIB_AVAILABLE_IN_ALL
 GPtrArray* g_ptr_array_new_full           (guint             reserved_size,
                                           GDestroyNotify    element_free_func);
+GLIB_AVAILABLE_IN_2_66
+GPtrArray* g_ptr_array_new_null_terminated (guint          reserved_size,
+                                            GDestroyNotify element_free_func,
+                                            gboolean       null_terminated);
 GLIB_AVAILABLE_IN_ALL
 gpointer*  g_ptr_array_free               (GPtrArray        *array,
                                           gboolean          free_seg);
@@ -223,6 +227,9 @@ gboolean   g_ptr_array_find_with_equal_func (GPtrArray     *haystack,
                                              GEqualFunc     equal_func,
                                              guint         *index_);
 
+GLIB_AVAILABLE_IN_2_66
+gboolean g_ptr_array_is_null_terminated (const GPtrArray *array);
+
 
 /* Byte arrays, an array of guint8.  Implemented as a GArray,
  * but type-safe.
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c
index 1da514a3e..3e2914ab8 100644
--- a/glib/tests/array-test.c
+++ b/glib/tests/array-test.c
@@ -837,6 +837,73 @@ test_array_copy_sized (void)
   g_array_unref (array1);
 }
 
+static GPtrArray *
+ptr_array_new (guint          reserved_size,
+               GDestroyNotify element_free_func,
+               gboolean       null_terminated)
+ {
+    guint32 r;
+
+    /* All GPtrArray constructs are essentially the same (with slightly
+     * different parameters). That means, the tests should pass regardless
+     * which constructor is called.
+     *
+     * Randomly choose one of the constructors to increase the test
+     * coverage.
+     */
+
+    if ((g_random_int () % 4) == 0)
+      {
+        /* Also, randomly create %NULL terminated arrays. %NULL terminated
+         * arrays should behave like regular arrays, with the added behavior
+         * to always add a %NULL sentinel. But that shouldn't affect the
+         * remainder of the tests. */
+        null_terminated = TRUE;
+      }
+
+    r = (g_random_int () % 5);
+
+    if (   r <= 0
+        && reserved_size == 0
+        && !element_free_func
+        && !null_terminated)
+      return g_ptr_array_new ();
+
+    if (   r <= 1
+        && reserved_size == 0
+        && !null_terminated)
+      return g_ptr_array_new_with_free_func (element_free_func);
+
+    if (   r <= 2
+        && !element_free_func
+        && !null_terminated)
+      return g_ptr_array_sized_new (reserved_size);
+
+    if (   r <= 3
+        && !null_terminated)
+      return g_ptr_array_new_full (reserved_size, element_free_func);
+
+    return g_ptr_array_new_null_terminated (reserved_size, element_free_func, null_terminated);
+}
+
+#define ptr_array_assert_state(array) \
+  G_STMT_START { \
+    GPtrArray *_array = (array); \
+    \
+    if (_array) \
+      { \
+        if (!_array->pdata) \
+          { \
+            g_assert_cmpint (_array->len, ==, 0); \
+          } \
+        else \
+          { \
+            if (g_ptr_array_is_null_terminated (_array)) \
+              g_assert (_array->pdata[_array->len] == NULL); \
+        } \
+    } \
+  } G_STMT_END
+
 /* Check g_ptr_array_steal() function */
 static void
 pointer_array_steal (void)
@@ -847,7 +914,8 @@ pointer_array_steal (void)
   guint i;
   gsize len, past_len;
 
-  gparray = g_ptr_array_new ();
+  gparray = ptr_array_new (0, NULL, FALSE);
+
   pdata = g_ptr_array_steal (gparray, NULL);
   g_assert_null (pdata);
 
@@ -864,6 +932,7 @@ pointer_array_steal (void)
   g_assert_cmpint (past_len, ==, len);
   g_ptr_array_add (gparray, GINT_TO_POINTER (10));
 
+  ptr_array_assert_state (gparray);
   g_assert_cmpint ((gsize) pdata[0], ==, (gsize) GINT_TO_POINTER (0));
   g_assert_cmpint ((gsize) g_ptr_array_index (gparray, 0), ==,
                    (gsize) GINT_TO_POINTER (10));
@@ -872,11 +941,16 @@ pointer_array_steal (void)
   g_ptr_array_remove_index (gparray, 0);
 
   for (i = 0; i < array_size; i++)
-    g_ptr_array_add (gparray, GINT_TO_POINTER (i));
+    {
+      g_ptr_array_add (gparray, GINT_TO_POINTER (i));
+      ptr_array_assert_state (gparray);
+    }
   g_assert_cmpmem (pdata, array_size * sizeof (gpointer),
                    gparray->pdata, array_size * sizeof (gpointer));
   g_free (pdata);
 
+  ptr_array_assert_state (gparray);
+
   g_ptr_array_free (gparray, TRUE);
 }
 
@@ -888,7 +962,7 @@ pointer_array_add (void)
   gint sum = 0;
   gpointer *segment;
 
-  gparray = g_ptr_array_sized_new (1000);
+  gparray = ptr_array_new (1000, NULL, FALSE);
 
   for (i = 0; i < 10000; i++)
     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
@@ -913,7 +987,7 @@ pointer_array_insert (void)
   gint sum = 0;
   gint index;
 
-  gparray = g_ptr_array_sized_new (1000);
+  gparray = ptr_array_new (1000, NULL, FALSE);
 
   for (i = 0; i < 10000; i++)
     {
@@ -935,7 +1009,7 @@ pointer_array_ref_count (void)
   gint i;
   gint sum = 0;
 
-  gparray = g_ptr_array_new ();
+  gparray = ptr_array_new (0, NULL, FALSE);
   for (i = 0; i < 10000; i++)
     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
 
@@ -975,16 +1049,16 @@ pointer_array_free_func (void)
   gchar *s;
 
   num_free_func_invocations = 0;
-  gparray = g_ptr_array_new_with_free_func (my_free_func);
+  gparray = ptr_array_new (0, my_free_func, FALSE);
   g_ptr_array_unref (gparray);
   g_assert_cmpint (num_free_func_invocations, ==, 0);
 
-  gparray = g_ptr_array_new_with_free_func (my_free_func);
+  gparray = ptr_array_new (0, my_free_func, FALSE);
   g_ptr_array_free (gparray, TRUE);
   g_assert_cmpint (num_free_func_invocations, ==, 0);
 
   num_free_func_invocations = 0;
-  gparray = g_ptr_array_new_with_free_func (my_free_func);
+  gparray = ptr_array_new (0, my_free_func, FALSE);
   g_ptr_array_add (gparray, g_strdup ("foo"));
   g_ptr_array_add (gparray, g_strdup ("bar"));
   g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1009,7 +1083,7 @@ pointer_array_free_func (void)
   g_assert_cmpint (num_free_func_invocations, ==, 5);
 
   num_free_func_invocations = 0;
-  gparray = g_ptr_array_new_full (10, my_free_func);
+  gparray = ptr_array_new (10, my_free_func, FALSE);
   g_ptr_array_add (gparray, g_strdup ("foo"));
   g_ptr_array_add (gparray, g_strdup ("bar"));
   g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1023,7 +1097,7 @@ pointer_array_free_func (void)
   g_assert_cmpint (num_free_func_invocations, ==, 0);
 
   num_free_func_invocations = 0;
-  gparray = g_ptr_array_new_with_free_func (my_free_func);
+  gparray = ptr_array_new (0, my_free_func, FALSE);
   g_ptr_array_add (gparray, g_strdup ("foo"));
   g_ptr_array_add (gparray, g_strdup ("bar"));
   g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1032,7 +1106,7 @@ pointer_array_free_func (void)
   g_assert_cmpint (num_free_func_invocations, ==, 3);
 
   num_free_func_invocations = 0;
-  gparray = g_ptr_array_new_with_free_func (my_free_func);
+  gparray = ptr_array_new (0, my_free_func, FALSE);
   g_ptr_array_add (gparray, g_strdup ("foo"));
   g_ptr_array_add (gparray, g_strdup ("bar"));
   g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1040,7 +1114,7 @@ pointer_array_free_func (void)
   g_assert_cmpint (num_free_func_invocations, ==, 3);
 
   num_free_func_invocations = 0;
-  gparray = g_ptr_array_new_with_free_func (my_free_func);
+  gparray = ptr_array_new (0, my_free_func, FALSE);
   g_ptr_array_add (gparray, "foo");
   g_ptr_array_add (gparray, "bar");
   g_ptr_array_add (gparray, "baz");
@@ -1085,7 +1159,7 @@ pointer_array_copy (void)
     array_test[i] = i;
 
   /* Test copy an empty array */
-  ptr_array = g_ptr_array_sized_new (0);
+  ptr_array = ptr_array_new (0, NULL, FALSE);
   ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
 
   g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
@@ -1094,7 +1168,7 @@ pointer_array_copy (void)
   g_ptr_array_unref (ptr_array2);
 
   /* Test simple copy */
-  ptr_array = g_ptr_array_sized_new (array_size);
+  ptr_array = ptr_array_new (array_size, NULL, FALSE);
 
   for (i = 0; i < array_size; i++)
     g_ptr_array_add (ptr_array, &array_test[i]);
@@ -1142,7 +1216,7 @@ pointer_array_extend (void)
   if (g_test_undefined ())
     {
       /* Testing degenerated cases */
-      ptr_array = g_ptr_array_sized_new (0);
+      ptr_array = ptr_array_new (0, NULL, FALSE);
       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
                              "*assertion*!= NULL*");
       g_ptr_array_extend (NULL, ptr_array, NULL, NULL);
@@ -1161,8 +1235,8 @@ pointer_array_extend (void)
     array_test[i] = i;
 
   /* Testing extend with array of size zero */
-  ptr_array = g_ptr_array_sized_new (0);
-  ptr_array2 = g_ptr_array_sized_new (0);
+  ptr_array = ptr_array_new (0, NULL, FALSE);
+  ptr_array2 = ptr_array_new (0, NULL, FALSE);
 
   g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
 
@@ -1173,8 +1247,8 @@ pointer_array_extend (void)
   g_ptr_array_unref (ptr_array2);
 
   /* Testing extend an array of size zero */
-  ptr_array = g_ptr_array_sized_new (array_size);
-  ptr_array2 = g_ptr_array_sized_new (0);
+  ptr_array = ptr_array_new (array_size, NULL, FALSE);
+  ptr_array2 = ptr_array_new (0, NULL, FALSE);
 
   for (i = 0; i < array_size; i++)
     {
@@ -1190,8 +1264,8 @@ pointer_array_extend (void)
   g_ptr_array_unref (ptr_array2);
 
   /* Testing extend an array of size zero */
-  ptr_array = g_ptr_array_sized_new (0);
-  ptr_array2 = g_ptr_array_sized_new (array_size);
+  ptr_array = ptr_array_new (0, NULL, FALSE);
+  ptr_array2 = ptr_array_new (array_size, NULL, FALSE);
 
   for (i = 0; i < array_size; i++)
     {
@@ -1207,8 +1281,8 @@ pointer_array_extend (void)
   g_ptr_array_unref (ptr_array2);
 
   /* Testing simple extend */
-  ptr_array = g_ptr_array_sized_new (array_size / 2);
-  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+  ptr_array = ptr_array_new (array_size / 2, NULL, FALSE);
+  ptr_array2 = ptr_array_new (array_size / 2, NULL, FALSE);
 
   for (i = 0; i < array_size / 2; i++)
     {
@@ -1225,8 +1299,8 @@ pointer_array_extend (void)
   g_ptr_array_unref (ptr_array2);
 
   /* Testing extend with GCopyFunc */
-  ptr_array = g_ptr_array_sized_new (array_size / 2);
-  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+  ptr_array = ptr_array_new (array_size / 2, NULL, FALSE);
+  ptr_array2 = ptr_array_new (array_size / 2, NULL, FALSE);
 
   for (i = 0; i < array_size / 2; i++)
     {
@@ -1262,8 +1336,8 @@ pointer_array_extend_and_steal (void)
     array_test[i] = i;
 
   /* Testing simple extend_and_steal() */
-  ptr_array = g_ptr_array_sized_new (array_size / 2);
-  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+  ptr_array = ptr_array_new (array_size / 2, NULL, FALSE);
+  ptr_array2 = ptr_array_new (array_size / 2, NULL, FALSE);
 
   for (i = 0; i < array_size / 2; i++)
     {
@@ -1271,16 +1345,20 @@ pointer_array_extend_and_steal (void)
       g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
     }
 
+  ptr_array_assert_state (ptr_array2);
+
   g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
 
+  ptr_array_assert_state (ptr_array);
+
   for (i = 0; i < array_size; i++)
     g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
 
   g_ptr_array_free (ptr_array, TRUE);
 
   /* Testing extend_and_steal() with a pending reference to stolen array */
-  ptr_array = g_ptr_array_sized_new (array_size / 2);
-  ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+  ptr_array = ptr_array_new (array_size / 2, NULL, FALSE);
+  ptr_array2 = ptr_array_new (array_size / 2, NULL, FALSE);
 
   for (i = 0; i < array_size / 2; i++)
     {
@@ -1333,7 +1411,7 @@ pointer_array_sort (void)
   gint val;
   gint prev, cur;
 
-  gparray = g_ptr_array_new ();
+  gparray = ptr_array_new (0, NULL, FALSE);
   for (i = 0; i < 10000; i++)
     {
       val = g_random_int_range (0, 10000);
@@ -1388,7 +1466,7 @@ pointer_array_sort_example (void)
 
   g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
 
-  file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
+  file_list = ptr_array_new (0, file_list_entry_free, FALSE);
 
   entry = g_new0 (FileListEntry, 1);
   entry->name = g_strdup ("README");
@@ -1455,7 +1533,7 @@ pointer_array_sort_with_data_example (void)
 
   g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
 
-  file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
+  file_list = ptr_array_new (0, file_list_entry_free, FALSE);
 
   entry = g_new0 (FileListEntry, 1);
   entry->name = g_strdup ("README");
@@ -1504,7 +1582,7 @@ pointer_array_sort_with_data (void)
   gint i;
   gint prev, cur;
 
-  gparray = g_ptr_array_new ();
+  gparray = ptr_array_new (0, NULL, FALSE);
   for (i = 0; i < 10000; i++)
     g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
 
@@ -1527,7 +1605,7 @@ pointer_array_find_empty (void)
   GPtrArray *array;
   guint idx;
 
-  array = g_ptr_array_new ();
+  array = ptr_array_new (0, NULL, FALSE);
 
   g_assert_false (g_ptr_array_find (array, "some-value", NULL));  /* NULL index */
   g_assert_false (g_ptr_array_find (array, "some-value", &idx));  /* non-NULL index */
@@ -1544,7 +1622,7 @@ pointer_array_find_non_empty (void)
   guint idx;
   const gchar *str_pointer = "static-string";
 
-  array = g_ptr_array_new ();
+  array = ptr_array_new (0, NULL, FALSE);
 
   g_ptr_array_add (array, "some");
   g_ptr_array_add (array, "random");
@@ -1588,7 +1666,7 @@ pointer_array_steal_index (void)
 {
   guint i1 = 0, i2 = 0, i3 = 0, i4 = 0;
   gpointer out1, out2;
-  GPtrArray *array = g_ptr_array_new_with_free_func (steal_destroy_notify);
+  GPtrArray *array = ptr_array_new (0, steal_destroy_notify, FALSE);
 
   g_ptr_array_add (array, &i1);
   g_ptr_array_add (array, &i2);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]