[gnome-builder] makecache: make makecache-target a reference counted type



commit 781689d86b882d2cdffe5500c41b934bc9f682a2
Author: Christian Hergert <christian hergert me>
Date:   Thu May 14 18:00:25 2015 -0700

    makecache: make makecache-target a reference counted type

 libide/autotools/ide-makecache-target.c |   48 ++++++++++++++++++++++++++----
 libide/autotools/ide-makecache-target.h |    8 ++++-
 libide/autotools/ide-makecache.c        |    2 +-
 3 files changed, 48 insertions(+), 10 deletions(-)
---
diff --git a/libide/autotools/ide-makecache-target.c b/libide/autotools/ide-makecache-target.c
index b4d0f48..6883d25 100644
--- a/libide/autotools/ide-makecache-target.c
+++ b/libide/autotools/ide-makecache-target.c
@@ -16,20 +16,51 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define G_LOG_DOMAIN "ide-makecache-target"
+
+#include "egg-counter.h"
+
 #include "ide-makecache-target.h"
 
+G_DEFINE_BOXED_TYPE (IdeMakecacheTarget, ide_makecache_target,
+                     ide_makecache_target_ref, ide_makecache_target_unref)
+
+EGG_DEFINE_COUNTER (instances, "IdeMakecacheTarget", "IdeMakecacheTarget Instances",
+                    "Number of IdeMakecacheTarget instances.")
+
 struct _IdeMakecacheTarget
 {
-  gchar *subdir;
-  gchar *target;
+  volatile gint  ref_count;
+
+  gchar         *subdir;
+  gchar         *target;
 };
 
 void
-ide_makecache_target_free (IdeMakecacheTarget *self)
+ide_makecache_target_unref (IdeMakecacheTarget *self)
 {
-  g_free (self->subdir);
-  g_free (self->target);
-  g_free (self);
+  g_return_if_fail (self != NULL);
+  g_return_if_fail (self->ref_count > 0);
+
+  if (g_atomic_int_dec_and_test (&self->ref_count))
+    {
+      g_free (self->subdir);
+      g_free (self->target);
+      g_slice_free (IdeMakecacheTarget, self);
+
+      EGG_COUNTER_DEC (instances);
+    }
+}
+
+IdeMakecacheTarget *
+ide_makecache_target_ref (IdeMakecacheTarget *self)
+{
+  g_return_val_if_fail (self != NULL, NULL);
+  g_return_val_if_fail (self->ref_count > 0, NULL);
+
+  g_atomic_int_inc (&self->ref_count);
+
+  return self;
 }
 
 IdeMakecacheTarget *
@@ -43,10 +74,13 @@ ide_makecache_target_new (const gchar *subdir,
   if (subdir != NULL && (subdir [0] == '.' || subdir [0] == '\0'))
     subdir = NULL;
 
-  self = g_new0 (IdeMakecacheTarget, 1);
+  self = g_slice_new0 (IdeMakecacheTarget);
+  self->ref_count = 1;
   self->subdir = g_strdup (subdir);
   self->target = g_strdup (target);
 
+  EGG_COUNTER_INC (instances);
+
   return self;
 }
 
diff --git a/libide/autotools/ide-makecache-target.h b/libide/autotools/ide-makecache-target.h
index 41ee1a6..58891d1 100644
--- a/libide/autotools/ide-makecache-target.h
+++ b/libide/autotools/ide-makecache-target.h
@@ -19,15 +19,19 @@
 #ifndef IDE_MAKECACHE_TARGET_H
 #define IDE_MAKECACHE_TARGET_H
 
-#include <glib.h>
+#include <glib-object.h>
 
 G_BEGIN_DECLS
 
+#define IDE_TYPE_MAKECACHE_TARGET (ide_makecache_target_get_type())
+
 typedef struct _IdeMakecacheTarget IdeMakecacheTarget;
 
+GType               ide_makecache_target_get_type   (void);
 IdeMakecacheTarget *ide_makecache_target_new        (const gchar        *subdir,
                                                      const gchar        *target);
-void                ide_makecache_target_free       (IdeMakecacheTarget *self);
+IdeMakecacheTarget *ide_makecache_target_ref        (IdeMakecacheTarget *self);
+void                ide_makecache_target_unref      (IdeMakecacheTarget *self);
 const gchar        *ide_makecache_target_get_target (IdeMakecacheTarget *self);
 const gchar        *ide_makecache_target_get_subdir (IdeMakecacheTarget *self);
 guint               ide_makecache_target_hash       (gconstpointer       data);
diff --git a/libide/autotools/ide-makecache.c b/libide/autotools/ide-makecache.c
index 43e2994..b1e8519 100644
--- a/libide/autotools/ide-makecache.c
+++ b/libide/autotools/ide-makecache.c
@@ -344,7 +344,7 @@ ide_makecache_get_file_targets_searched (GMappedFile *mapped,
   content = g_mapped_file_get_contents (mapped);
   len = g_mapped_file_get_length (mapped);
 
-  targets = g_ptr_array_new_with_free_func ((GDestroyNotify)ide_makecache_target_free);
+  targets = g_ptr_array_new_with_free_func ((GDestroyNotify)ide_makecache_target_unref);
   found = g_hash_table_new (ide_makecache_target_hash, ide_makecache_target_equal);
 
 #ifdef IDE_ENABLE_TRACE


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