[glib/wip/ebassi/rc: 2/4] Port GArray and GPtrArray to g_ref_counter_*
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/ebassi/rc: 2/4] Port GArray and GPtrArray to g_ref_counter_*
- Date: Tue, 15 Nov 2016 11:51:11 +0000 (UTC)
commit e5e2c07a8df4a4211790a9dfd42a2ffb5a1d1d5a
Author: Emmanuele Bassi <ebassi gnome org>
Date: Tue Nov 15 11:26:44 2016 +0000
Port GArray and GPtrArray to g_ref_counter_*
Instead of using atomic operations directly, use an atomic reference
counter.
glib/garray.c | 24 +++++++++++++-----------
1 files changed, 13 insertions(+), 11 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 727c102..47166f3 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -40,7 +40,7 @@
#include "gthread.h"
#include "gmessages.h"
#include "gqsort.h"
-
+#include "grefcount.h"
/**
* SECTION:arrays
@@ -105,7 +105,7 @@ struct _GRealArray
guint elt_size;
guint zero_terminated : 1;
guint clear : 1;
- gint ref_count;
+ volatile int ref_count;
GDestroyNotify clear_func;
};
@@ -198,9 +198,10 @@ g_array_sized_new (gboolean zero_terminated,
array->zero_terminated = (zero_terminated ? 1 : 0);
array->clear = (clear ? 1 : 0);
array->elt_size = elt_size;
- array->ref_count = 1;
array->clear_func = NULL;
+ g_ref_counter_init (&array->ref_count, TRUE);
+
if (array->zero_terminated || reserved_size != 0)
{
g_array_maybe_expand (array, reserved_size);
@@ -255,7 +256,7 @@ g_array_ref (GArray *array)
GRealArray *rarray = (GRealArray*) array;
g_return_val_if_fail (array, NULL);
- g_atomic_int_inc (&rarray->ref_count);
+ g_ref_counter_acquire (&rarray->ref_count);
return array;
}
@@ -285,7 +286,7 @@ g_array_unref (GArray *array)
GRealArray *rarray = (GRealArray*) array;
g_return_if_fail (array);
- if (g_atomic_int_dec_and_test (&rarray->ref_count))
+ if (g_ref_counter_release (&rarray->ref_count))
array_free (rarray, FREE_SEGMENT);
}
@@ -340,7 +341,7 @@ g_array_free (GArray *farray,
flags = (free_segment ? FREE_SEGMENT : 0);
/* if others are holding a reference, preserve the wrapper but do free/return the data */
- if (!g_atomic_int_dec_and_test (&array->ref_count))
+ if (!g_ref_counter_release (&array->ref_count))
flags |= PRESERVE_WRAPPER;
return array_free (array, flags);
@@ -848,7 +849,7 @@ struct _GRealPtrArray
gpointer *pdata;
guint len;
guint alloc;
- gint ref_count;
+ volatile int ref_count;
GDestroyNotify element_free_func;
};
@@ -902,9 +903,10 @@ g_ptr_array_sized_new (guint reserved_size)
array->pdata = NULL;
array->len = 0;
array->alloc = 0;
- array->ref_count = 1;
array->element_free_func = NULL;
+ g_ref_counter_init (&array->ref_count, TRUE);
+
if (reserved_size != 0)
g_ptr_array_maybe_expand (array, reserved_size);
@@ -1007,7 +1009,7 @@ g_ptr_array_ref (GPtrArray *array)
g_return_val_if_fail (array, NULL);
- g_atomic_int_inc (&rarray->ref_count);
+ g_ref_counter_acquire (&rarray->ref_count);
return array;
}
@@ -1032,7 +1034,7 @@ g_ptr_array_unref (GPtrArray *array)
g_return_if_fail (array);
- if (g_atomic_int_dec_and_test (&rarray->ref_count))
+ if (g_ref_counter_release (&rarray->ref_count))
ptr_array_free (array, FREE_SEGMENT);
}
@@ -1069,7 +1071,7 @@ g_ptr_array_free (GPtrArray *array,
/* if others are holding a reference, preserve the wrapper but
* do free/return the data
*/
- if (!g_atomic_int_dec_and_test (&rarray->ref_count))
+ if (!g_ref_counter_release (&rarray->ref_count))
flags |= PRESERVE_WRAPPER;
return ptr_array_free (array, flags);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]