[gvfs] Add refcounting for MetaTree



commit 66fb95bc205528a4ed3e0238e4b826027bd33a66
Author: Alexander Larsson <alexl redhat com>
Date:   Thu Jun 18 17:50:38 2009 +0200

    Add refcounting for MetaTree

 metadata/metatree.c |   31 ++++++++++++++++++++++---------
 metadata/metatree.h |    3 ++-
 2 files changed, 24 insertions(+), 10 deletions(-)
---
diff --git a/metadata/metatree.c b/metadata/metatree.c
index 0190809..465d067 100644
--- a/metadata/metatree.c
+++ b/metadata/metatree.c
@@ -104,6 +104,7 @@ typedef struct {
 } MetaJournal;
 
 struct _MetaTree {
+  int refcount;
   char *filename;
   gboolean for_write;
 
@@ -226,14 +227,6 @@ meta_tree_clear (MetaTree *tree)
     }
 }
 
-void
-meta_tree_free (MetaTree *tree)
-{
-  meta_tree_clear (tree);
-  g_free (tree->filename);
-  g_free (tree);
-}
-
 static gboolean
 meta_tree_init (MetaTree *tree)
 {
@@ -320,17 +313,37 @@ meta_tree_open (const char *filename,
   g_assert (sizeof (MetaFileDataEnt) == 8);
 
   tree = g_new0 (MetaTree, 1);
+  tree->refcount = 1;
   tree->filename = g_strdup (filename);
   tree->for_write = for_write;
 
   if (!meta_tree_init (tree))
     {
-      meta_tree_free (tree);
+      meta_tree_unref (tree);
       return NULL;
     }
   return tree;
 }
 
+MetaTree *
+meta_tree_ref (MetaTree *tree)
+{
+  tree->refcount++;
+  return tree;
+}
+
+void
+meta_tree_unref (MetaTree *tree)
+{
+  tree->refcount--;
+  if (tree->refcount == 0)
+    {
+      meta_tree_clear (tree);
+      g_free (tree->filename);
+      g_free (tree);
+    }
+}
+
 void
 meta_tree_refresh (MetaTree *tree)
 {
diff --git a/metadata/metatree.h b/metadata/metatree.h
index 424f0e9..734412c 100644
--- a/metadata/metatree.h
+++ b/metadata/metatree.h
@@ -26,9 +26,10 @@ MetaTree        *meta_lookup_cache_lookup_path (MetaLookupCache *cache,
 						const char *filename,
 						guint64 device);
 
-void      meta_tree_free    (MetaTree   *tree);
 MetaTree *meta_tree_open    (const char *filename,
 			     gboolean    for_write);
+MetaTree *meta_tree_ref     (MetaTree   *tree);
+void      meta_tree_unref   (MetaTree   *tree);
 void      meta_tree_refresh (MetaTree   *tree); /* May invalidates all strings */
 
 



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