[glib/wip/ebassi/rc-new: 3/4] Port GBytes to gatomicrefcount



commit 617923c140a07ace68e94c1b6f69de3a4470b7b9
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Jan 17 17:01:52 2018 +0000

    Port GBytes to gatomicrefcount
    
    Use the newly added API for reference counting instead of rolling our
    own.

 glib/gbytes.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
---
diff --git a/glib/gbytes.c b/glib/gbytes.c
index 81dc9db..e1272a7 100644
--- a/glib/gbytes.c
+++ b/glib/gbytes.c
@@ -30,6 +30,7 @@
 #include <glib/gtestutils.h>
 #include <glib/gmem.h>
 #include <glib/gmessages.h>
+#include <glib/grefcount.h>
 
 #include <string.h>
 
@@ -68,7 +69,7 @@ struct _GBytes
 {
   gconstpointer data;  /* may be NULL iff (size == 0) */
   gsize size;  /* may be 0 */
-  gint ref_count;
+  gatomicrefcount ref_count;
   GDestroyNotify free_func;
   gpointer user_data;
 };
@@ -186,7 +187,7 @@ g_bytes_new_with_free_func (gconstpointer  data,
   bytes->size = size;
   bytes->free_func = free_func;
   bytes->user_data = user_data;
-  bytes->ref_count = 1;
+  g_atomic_ref_count_init (&bytes->ref_count);
 
   return (GBytes *)bytes;
 }
@@ -284,7 +285,7 @@ g_bytes_ref (GBytes *bytes)
 {
   g_return_val_if_fail (bytes != NULL, NULL);
 
-  g_atomic_int_inc (&bytes->ref_count);
+  g_atomic_ref_count_inc (&bytes->ref_count);
 
   return bytes;
 }
@@ -304,7 +305,7 @@ g_bytes_unref (GBytes *bytes)
   if (bytes == NULL)
     return;
 
-  if (g_atomic_int_dec_and_test (&bytes->ref_count))
+  if (g_atomic_ref_count_dec (&bytes->ref_count))
     {
       if (bytes->free_func != NULL)
         bytes->free_func (bytes->user_data);
@@ -412,7 +413,7 @@ try_steal_and_unref (GBytes         *bytes,
     return NULL;
 
   /* Are we the only reference? */
-  if (g_atomic_int_get (&bytes->ref_count) == 1)
+  if (g_atomic_ref_count_compare (&bytes->ref_count, 1))
     {
       *size = bytes->size;
       result = (gpointer)bytes->data;


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