[glib/th/g-ptr-array-set-null-terminated] fixup! array: add support for g_ptr_array_null_terminated()



commit f3b87b436dd9962694fb10cbf2571f9b3363fea2
Author: Thomas Haller <thaller redhat com>
Date:   Fri May 8 08:15:56 2020 +0200

    fixup! array: add support for g_ptr_array_null_terminated()

 glib/garray.c           | 67 ++++++++++++++++++++++++++-----------------------
 glib/garray.h           |  4 ++-
 glib/tests/array-test.c |  7 +++---
 3 files changed, 42 insertions(+), 36 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 730351b4d..9cc53f17d 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1206,7 +1206,7 @@ g_ptr_array_copy (GPtrArray *array,
   if (rarray->null_terminated)
     {
       /* propagate the null terminated flag. */
-      g_ptr_array_null_terminated (new_array, TRUE);
+      g_ptr_array_set_null_terminated (new_array);
     }
 
   if (((GRealPtrArray *) array)->alloc > 0)
@@ -1376,54 +1376,59 @@ g_ptr_array_set_free_func (GPtrArray      *array,
 }
 
 /**
- * g_ptr_array_null_terminated:
+ * g_ptr_array_get_null_terminated:
+ * @array: the #GPtrArray
+ *
+ * Returns: %TRUE if the array is made to be %NULL terminated.
+ *
+ * Since: 2.66
+ */
+gboolean
+g_ptr_array_get_null_terminated (const GPtrArray *array)
+{
+  g_return_val_if_fail (array, FALSE);
+
+  return ((GRealPtrArray *) array)->null_terminated;
+}
+
+/**
+ * g_ptr_array_set_null_terminated:
  * @array: A #GPtrArray
- * @make_null_terminated: if %FALSE, the function only returns
- *   whether @array is already %NULL terminated. If %TRUE,
- *   this marks @array as %NULL terminated.
  *
  * #GPtrArray is not %NULL terminated by default. By calling
- * this function with @make_null_terminated, the instance gets
- * marked to be %NULL terminated. Once the instance is marked
- * to be %NULL terminated, it cannot be reverted.
+ * this function, the instance gets marked to be %NULL terminated.
+ * Once the instance is marked to be %NULL terminated, it cannot be
+ * reverted.
  *
  * 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.
- *
- * When calling this function on a non empty array, the initial
- * null termination may cause a reallocation to grow the buffer.
+ * is always allocated. In other words, if the length is zero,
+ * then pdata may either point to a %NULL terminated pointer array of length
+ * zero or be %NULL.
  *
- * Calling this function with %make_null_terminated %FALSE, then
- * @array is not modified.
- *
- * Returns: %TRUE if @array is marked to be %NULL terminated.
+ * When making an array %NULL terminated, the buffer may need to grow
+ * and get reallocated in the process.
  *
  * Since: 2.66
  */
-gboolean
-g_ptr_array_null_terminated (GPtrArray *array, gboolean make_null_terminated)
+void
+g_ptr_array_set_null_terminated (const GPtrArray *array)
 {
-  GRealPtrArray *rarray = (GRealPtrArray *)array;
+  GRealPtrArray *rarray = (GRealPtrArray *) array;
 
-  g_return_val_if_fail (array, FALSE);
+  g_return_if_fail (array);
 
-  if (!make_null_terminated)
-    return rarray->null_terminated;
+  if (rarray->null_terminated)
+    return;
 
-  if (!rarray->null_terminated)
+  rarray->null_terminated = TRUE;
+  if (rarray->alloc > 0)
     {
-      rarray->null_terminated = TRUE;
-      if (rarray->alloc > 0)
-        {
-          g_ptr_array_maybe_expand (rarray, 1);
-          rarray->pdata[rarray->len] = NULL;
-        }
+      g_ptr_array_maybe_expand (rarray, 1);
+      rarray->pdata[rarray->len] = NULL;
     }
-
-  return TRUE;
 }
 
 /**
diff --git a/glib/garray.h b/glib/garray.h
index c98403ad1..6ebb81726 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -224,7 +224,9 @@ gboolean   g_ptr_array_find_with_equal_func (GPtrArray     *haystack,
                                              guint         *index_);
 
 GLIB_AVAILABLE_IN_2_66
-gboolean g_ptr_array_null_terminated (GPtrArray *array, gboolean set_null_terminated);
+gboolean g_ptr_array_get_null_terminated (const GPtrArray *array);
+GLIB_AVAILABLE_IN_2_66
+void g_ptr_array_set_null_terminated (const GPtrArray *array);
 
 
 /* Byte arrays, an array of guint8.  Implemented as a GArray,
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c
index eb261cca4..634bfb549 100644
--- a/glib/tests/array-test.c
+++ b/glib/tests/array-test.c
@@ -840,15 +840,14 @@ test_array_copy_sized (void)
 static GPtrArray *
 ptr_array_init (GPtrArray *array)
 {
-  gboolean rnd = ((g_random_int () % 3u) == 0u);
-
   g_assert (array);
 
   /* Randomly make the GPtrArray %NULL terminated.
    * A %NULL terminated array gives stronger guarantees, so wherever
    * the test checks something, it must also hold for a %NULL terminated
    * array. To increase the test coverage, randomly set the flag. */
-  g_ptr_array_null_terminated (array, rnd);
+  if ((g_random_int () % 3u) == 0u)
+    g_ptr_array_set_null_terminated (array);
   return array;
 }
 
@@ -864,7 +863,7 @@ ptr_array_init (GPtrArray *array)
           } \
         else \
           { \
-            if (g_ptr_array_null_terminated (_array, FALSE)) \
+            if (g_ptr_array_get_null_terminated (_array)) \
               g_assert (_array->pdata[_array->len] == NULL); \
         } \
     } \


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