[glib/wip/refptr: 2/5] Add reference counted strings



commit 3939c50e0e832c512ae50e24dc9e2f727dfbd90f
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Mon Jan 26 14:54:15 2015 +0000

    Add reference counted strings
    
    It's useful to have a string type that is reference counted, instead of
    copied and freed on demand.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=622721

 docs/reference/glib/glib-sections.txt |    5 ++++
 glib/gref.c                           |   42 +++++++++++++++++++++++++++++++++
 glib/gref.h                           |   41 ++++++++++++++++++++++++++++++++
 3 files changed, 88 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index b6b6f35..ab001d4 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1034,6 +1034,11 @@ g_ref_pointer_acquire
 g_ref_pointer_release
 
 <SUBSECTION>
+g_string_ref_new
+g_string_ref
+g_string_unref
+
+<SUBSECTION>
 g_ref_count_init
 g_ref_count_inc
 g_ref_count_dec
diff --git a/glib/gref.c b/glib/gref.c
index 93e3da3..9bb478a 100644
--- a/glib/gref.c
+++ b/glib/gref.c
@@ -462,3 +462,45 @@ g_ref_pointer_make_atomic (gpointer ref)
 
   g_ref_count_make_atomic (&real->ref_count);
 }
+
+/**
+ * g_string_ref_new:
+ * @str: the string to reference
+ *
+ * Creates a new reference counted string.
+ *
+ * You can acquire a reference on the string using g_string_ref()
+ * release it using g_string_unref().
+ *
+ * The returned string can be used with any string utility function
+ * transparently. Instead of copying the string, use the reference
+ * counting API to acquire and release references when needed.
+ *
+ * Once the last reference on the string is released, the string will
+ * be freed.
+ *
+ * Returns: a newly allocated reference counted string
+ *
+ * Since: 2.44
+ */
+const char *
+g_string_ref_new (const char *str)
+{
+  gsize len;
+  char *res;
+
+  g_return_val_if_fail (str != NULL || *str != '\0', NULL);
+
+#ifdef G_ENABLE_DEBUG
+  if (g_is_ref_pointer (str))
+    return g_ref_pointer_acquire ((char *) str);
+#endif
+
+  len = strlen (str);
+  res = g_ref_pointer_alloc (len + 1, NULL);
+
+  memcpy (res, str, len);
+  res[len] = '\0';
+
+  return res;
+}
diff --git a/glib/gref.h b/glib/gref.h
index dbbd2d2..9cbd8ec 100644
--- a/glib/gref.h
+++ b/glib/gref.h
@@ -60,6 +60,47 @@ gboolean        g_ref_count_dec         (volatile int  *refcount);
 GLIB_AVAILABLE_IN_2_44
 void            g_ref_count_make_atomic (volatile int  *refcount);
 
+GLIB_AVAILABLE_IN_2_44
+const char *    g_string_ref_new        (const char    *str);
+
+/**
+ * g_string_ref:
+ * @str: a reference counted string
+ *
+ * Acquires a reference on the given string.
+ *
+ * Returns: (transfer none): the string, with its reference count increased
+ *
+ * Since: 2.44
+ */
+static inline gconstpointer
+(g_string_ref) (const char *str)
+{
+  return g_ref_pointer_acquire ((char *) str);
+}
+
+/**
+ * g_string_unref:
+ * @str: a reference counted string
+ *
+ * Releases a reference acquired on the given string.
+ *
+ * If it was the last reference, the string will be freed.
+ *
+ * Since: 2.44
+ */
+static inline void
+(g_string_unref) (const char *str)
+{
+  g_ref_pointer_release ((char *) str);
+}
+
+#define g_string_ref(str) \
+  (0 ? (str) : (g_string_ref) (str))
+
+#define g_string_unref(str) \
+  (0 ? (str) : (g_string_unref) (str))
+
 G_END_DECLS
 
 #endif /* __G_REF_H__ */


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