[gnome-builder/wip/libide] libide: add IdeRefPtr to help with encapsulated pointers



commit 2745cee45e817f536ddc34886895a951f6871cb2
Author: Christian Hergert <christian hergert me>
Date:   Sat Mar 14 17:49:27 2015 -0700

    libide: add IdeRefPtr to help with encapsulated pointers

 libide/Makefile.am   |    2 +
 libide/ide-ref-ptr.c |   75 ++++++++++++++++++++++++++++++++++++++++++++++++++
 libide/ide-ref-ptr.h |   41 +++++++++++++++++++++++++++
 3 files changed, 118 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index c26381b..5eaa15f 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -214,6 +214,8 @@ libide_1_0_la_SOURCES = \
        libide/ide-line-change-gutter-renderer.h \
        libide/ide-line-diagnostics-gutter-renderer.c \
        libide/ide-line-diagnostics-gutter-renderer.h \
+       libide/ide-ref-ptr.c \
+       libide/ide-ref-ptr.h \
        libide/ide-search-reducer.c \
        libide/ide-search-reducer.h \
        libide/ide-source-iter.c \
diff --git a/libide/ide-ref-ptr.c b/libide/ide-ref-ptr.c
new file mode 100644
index 0000000..8633060
--- /dev/null
+++ b/libide/ide-ref-ptr.c
@@ -0,0 +1,75 @@
+/* ide-ref-ptr.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-ref-ptr.h"
+
+G_DEFINE_BOXED_TYPE (IdeRefPtr, ide_ref_ptr, ide_ref_ptr_ref, ide_ref_ptr_unref)
+
+struct _IdeRefPtr
+{
+  volatile gint  ref_count;
+  gpointer       data;
+  GDestroyNotify free_func;
+};
+
+IdeRefPtr *
+ide_ref_ptr_new (gpointer       data,
+                 GDestroyNotify free_func)
+{
+  IdeRefPtr *self;
+
+  self = g_new0 (IdeRefPtr, 1);
+  self->ref_count = 1;
+  self->data = data;
+  self->free_func = free_func;
+
+  return self;
+}
+
+IdeRefPtr *
+ide_ref_ptr_ref (IdeRefPtr *self)
+{
+  g_return_val_if_fail (self, NULL);
+  g_return_val_if_fail (self->ref_count > 0, NULL);
+
+  g_atomic_int_inc (&self->ref_count);
+
+  return self;
+}
+
+void
+ide_ref_ptr_unref (IdeRefPtr *self)
+{
+  g_return_val_if_fail (self, NULL);
+  g_return_val_if_fail (self->ref_count > 0, NULL);
+
+  if (g_atomic_int_dec_and_test (&self->ref_count))
+    {
+      if (self->free_func)
+        g_clear_pointer (&self->data, self->free_func);
+    }
+}
+
+gpointer
+ide_ref_ptr_get (IdeRefPtr *self)
+{
+  g_return_val_if_fail (self, NULL);
+  g_return_val_if_fail (self->ref_count > 0, NULL);
+
+  return self->data;
+}
diff --git a/libide/ide-ref-ptr.h b/libide/ide-ref-ptr.h
new file mode 100644
index 0000000..b3ecfa0
--- /dev/null
+++ b/libide/ide-ref-ptr.h
@@ -0,0 +1,41 @@
+/* ide-ref-ptr.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef IDE_REF_PTR_H
+#define IDE_REF_PTR_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_REF_PTR (ide_ref_ptr_get_type())
+
+typedef struct _IdeRefPtr IdeRefPtr;
+
+GType      ide_ref_ptr_get_type (void);
+IdeRefPtr *ide_ref_ptr_new      (gpointer        data,
+                                 GDestroyNotify  free_func);
+IdeRefPtr *ide_ref_ptr_ref      (IdeRefPtr      *self);
+void       ide_ref_ptr_unref    (IdeRefPtr      *self);
+gpointer   ide_ref_ptr_get      (IdeRefPtr      *self);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeRefPtr, ide_ref_ptr_unref)
+
+G_END_DECLS
+
+#endif /* IDE_REF_PTR_H */


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