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



commit 52f6caa485f9aac72317b47c81ed53c514073c21
Author: Thomas Haller <thaller redhat com>
Date:   Thu May 21 11:21:33 2020 +0200

    fixup! array: add support for %NULL termination in GPtrArray

 docs/reference/glib/glib-sections.txt |  2 ++
 glib/garray.c                         | 34 +++++++++++++++++++++-------------
 glib/garray.h                         |  2 +-
 3 files changed, 24 insertions(+), 14 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index ecac01b5e..c7382438f 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -2886,7 +2886,9 @@ g_ptr_array_sized_new
 g_ptr_array_new_with_free_func
 g_ptr_array_copy
 g_ptr_array_new_full
+g_ptr_array_new_null_terminated
 g_ptr_array_set_free_func
+g_ptr_array_is_null_terminated
 g_ptr_array_ref
 g_ptr_array_unref
 g_ptr_array_add
diff --git a/glib/garray.c b/glib/garray.c
index d76c517bd..6d814414d 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1052,7 +1052,7 @@ struct _GRealPtrArray
   guint           len;
   guint           alloc;
   gatomicrefcount ref_count;
-  guint8          null_terminated;
+  guint8          null_terminated; /* always either 0 or 1, so it can be added to array lengths */
   GDestroyNotify  element_free_func;
 };
 
@@ -1098,10 +1098,17 @@ ptr_array_new (guint reserved_size,
 
   if (reserved_size != 0)
     {
-      if (G_LIKELY (reserved_size < G_MAXUINT))
-        reserved_size += array->null_terminated;
+      if (G_LIKELY (reserved_size < G_MAXUINT) &&
+          null_terminated)
+        reserved_size++;
       g_ptr_array_maybe_expand (array, reserved_size);
-      ptr_array_null_terminate (array);
+      if (null_terminated)
+        {
+          /* don't use ptr_array_null_terminate(). It helps the compiler
+           * to see when @null_terminated is false and thereby inline
+           * ptr_array_new() and possibly remove the code entirely. */
+            array->pdata[0] = NULL;
+        }
     }
 
   return (GPtrArray *) array;
@@ -1420,7 +1427,7 @@ g_ptr_array_set_free_func (GPtrArray      *array,
  * Since: 2.66
  */
 gboolean
-g_ptr_array_is_null_terminated (const GPtrArray *array)
+g_ptr_array_is_null_terminated (GPtrArray *array)
 {
   g_return_val_if_fail (array, FALSE);
 
@@ -1490,8 +1497,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.
+ * If the array is %NULL terminated and @free_seg is %FALSE, then this will never
+ * return %NULL. This means, if pdata of a %NULL terminated array is %NULL, a new
+ * (empty) array will be allocated and returned.
  *
  * 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()
@@ -1617,8 +1625,8 @@ g_ptr_array_set_size  (GPtrArray *array,
     {
       guint i;
 
-      if (   G_UNLIKELY (rarray->null_terminated)
-          && length_unsigned - rarray->len > G_MAXUINT - 1)
+      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);
@@ -1667,8 +1675,8 @@ ptr_array_remove_index (GPtrArray *array,
 
   rarray->len -= 1;
 
-  if (   G_UNLIKELY (g_mem_gc_friendly)
-      || rarray->null_terminated)
+  if (G_UNLIKELY (g_mem_gc_friendly) ||
+      rarray->null_terminated)
     rarray->pdata[rarray->len] = NULL;
 
   return result;
@@ -1950,8 +1958,8 @@ g_ptr_array_extend (GPtrArray  *array_to_extend,
   if (array->len == 0u)
     return;
 
-  if (   G_UNLIKELY (array->len == G_MAXUINT)
-      && rarray_to_extend->null_terminated)
+  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);
diff --git a/glib/garray.h b/glib/garray.h
index f33172f46..137e19f7d 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -228,7 +228,7 @@ gboolean   g_ptr_array_find_with_equal_func (GPtrArray     *haystack,
                                              guint         *index_);
 
 GLIB_AVAILABLE_IN_2_66
-gboolean g_ptr_array_is_null_terminated (const GPtrArray *array);
+gboolean g_ptr_array_is_null_terminated (GPtrArray *array);
 
 
 /* Byte arrays, an array of guint8.  Implemented as a GArray,


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