[glib/glib-2-28] GArray, GPtrArray: factor out the actual freeing



commit 700558425a3c464bf7ba3821abcdff8ee4e427c4
Author: Simon McVittie <simon mcvittie collabora co uk>
Date:   Wed Dec 14 16:53:24 2011 +0000

    GArray, GPtrArray: factor out the actual freeing
    
    Depending how the array is freed, we may want to free the underlying
    array (the "segment"), the struct wrapper or both.
    
    Signed-off-by: Simon McVittie <simon mcvittie collabora co uk>
    Bug: https://bugzilla.gnome.org/show_bug.cgi?id=666113
    Reviewed-by: Emmanuele Bassi <ebassi linux intel com>

 glib/garray.c |   57 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 43 insertions(+), 14 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 80cee56..b602674 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -224,6 +224,14 @@ g_array_ref (GArray *array)
   return array;
 }
 
+typedef enum
+{
+  FREE_SEGMENT = 1 << 0,
+  PRESERVE_WRAPPER = 1 << 1
+} ArrayFreeFlags;
+
+static gchar *array_free (GRealArray *, ArrayFreeFlags);
+
 /**
  * g_array_unref:
  * @array: A #GArray.
@@ -242,7 +250,7 @@ g_array_unref (GArray *array)
   g_return_if_fail (array);
 
   if (g_atomic_int_dec_and_test (&rarray->ref_count))
-    g_array_free (array, TRUE);
+    array_free (rarray, FREE_SEGMENT);
 }
 
 /**
@@ -288,17 +296,26 @@ g_array_free (GArray   *farray,
 	      gboolean  free_segment)
 {
   GRealArray *array = (GRealArray*) farray;
-  gchar* segment;
-  gboolean preserve_wrapper;
+  ArrayFreeFlags flags;
 
   g_return_val_if_fail (array, NULL);
 
+  flags = (free_segment ? FREE_SEGMENT : 0);
+
   /* if others are holding a reference, preserve the wrapper but do free/return the data */
-  preserve_wrapper = FALSE;
   if (g_atomic_int_get (&array->ref_count) > 1)
-    preserve_wrapper = TRUE;
+    flags |= PRESERVE_WRAPPER;
+
+  return array_free (array, flags);
+}
+
+static gchar *
+array_free (GRealArray     *array,
+            ArrayFreeFlags  flags)
+{
+  gchar *segment;
 
-  if (free_segment)
+  if (flags & FREE_SEGMENT)
     {
       g_free (array->data);
       segment = NULL;
@@ -306,7 +323,7 @@ g_array_free (GArray   *farray,
   else
     segment = (gchar*) array->data;
 
-  if (preserve_wrapper)
+  if (flags & PRESERVE_WRAPPER)
     {
       array->data            = NULL;
       array->len             = 0;
@@ -879,6 +896,8 @@ g_ptr_array_ref (GPtrArray *array)
   return array;
 }
 
+static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
+
 /**
  * g_ptr_array_unref:
  * @array: A #GPtrArray.
@@ -897,7 +916,7 @@ g_ptr_array_unref (GPtrArray *array)
   g_return_if_fail (array);
 
   if (g_atomic_int_dec_and_test (&rarray->ref_count))
-    g_ptr_array_free (array, TRUE);
+    ptr_array_free (array, FREE_SEGMENT);
 }
 
 /**
@@ -923,17 +942,27 @@ g_ptr_array_free (GPtrArray *farray,
 		  gboolean   free_segment)
 {
   GRealPtrArray *array = (GRealPtrArray*) farray;
-  gpointer* segment;
-  gboolean preserve_wrapper;
+  ArrayFreeFlags flags;
 
   g_return_val_if_fail (array, NULL);
 
+  flags = (free_segment ? FREE_SEGMENT : 0);
+
   /* if others are holding a reference, preserve the wrapper but do free/return the data */
-  preserve_wrapper = FALSE;
   if (g_atomic_int_get (&array->ref_count) > 1)
-    preserve_wrapper = TRUE;
+    flags |= PRESERVE_WRAPPER;
+
+  return ptr_array_free (farray, flags);
+}
+
+static gpointer *
+ptr_array_free (GPtrArray      *farray,
+                ArrayFreeFlags  flags)
+{
+  GRealPtrArray *array = (GRealPtrArray*) farray;
+  gpointer *segment;
 
-  if (free_segment)
+  if (flags & FREE_SEGMENT)
     {
       if (array->element_free_func != NULL)
         g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
@@ -943,7 +972,7 @@ g_ptr_array_free (GPtrArray *farray,
   else
     segment = array->pdata;
 
-  if (preserve_wrapper)
+  if (flags & PRESERVE_WRAPPER)
     {
       array->pdata = NULL;
       array->len = 0;



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