merge of '0d364079f80c7a3cfcf1dccba283f84ecc5fe329'
[pidgin-git.git] / libpurple / stringref.c
blob2ceda252c4caec1a9e2c2b033c4e715012a775ff
1 /**
2 * @file stringref.c Reference-counted immutable strings
3 * @ingroup core
4 */
6 /* purple
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
28 #include "internal.h"
30 #include <string.h>
31 #include <stdarg.h>
33 #include "debug.h"
34 #include "eventloop.h"
35 #include "stringref.h"
37 /**
38 * The internal representation of a stringref.
40 * @note For this structure to be useful, the string contained within
41 * it must be immutable -- for this reason, do _not_ access it
42 * directly!
44 struct _PurpleStringref {
45 guint32 ref; /**< The reference count of this string.
46 * Note that reference counts are only
47 * 31 bits, and the high-order bit
48 * indicates whether this string is up
49 * for GC at the next idle handler...
50 * But you aren't going to touch this
51 * anyway, right? */
52 char value[1]; /**< The string contained in this ref.
53 * Notice that it is simply "hanging
54 * off the end" of the ref ... this
55 * is to save an allocation. */
58 #define REFCOUNT(x) ((x) & 0x7fffffff)
60 static GList *gclist = NULL;
62 static void stringref_free(PurpleStringref *stringref);
63 static gboolean gs_idle_cb(gpointer data);
65 PurpleStringref *purple_stringref_new(const char *value)
67 PurpleStringref *newref;
68 size_t len;
70 if (value == NULL)
71 return NULL;
73 len = strlen(value);
75 newref = g_malloc(sizeof(PurpleStringref) + len);
76 g_strlcpy(newref->value, value, len);
77 newref->ref = 1;
79 return newref;
82 PurpleStringref *purple_stringref_new_noref(const char *value)
84 PurpleStringref *newref;
86 if (value == NULL)
87 return NULL;
89 newref = g_malloc(sizeof(PurpleStringref) + strlen(value));
90 strcpy(newref->value, value);
91 newref->ref = 0x80000000;
93 if (gclist == NULL)
94 purple_timeout_add(0, gs_idle_cb, NULL);
95 gclist = g_list_prepend(gclist, newref);
97 return newref;
100 PurpleStringref *purple_stringref_printf(const char *format, ...)
102 PurpleStringref *newref;
103 va_list ap;
105 if (format == NULL)
106 return NULL;
108 va_start(ap, format);
109 newref = g_malloc(sizeof(PurpleStringref) + g_printf_string_upper_bound(format, ap));
110 vsprintf(newref->value, format, ap);
111 va_end(ap);
112 newref->ref = 1;
114 return newref;
117 PurpleStringref *purple_stringref_ref(PurpleStringref *stringref)
119 if (stringref == NULL)
120 return NULL;
121 stringref->ref++;
122 return stringref;
125 void purple_stringref_unref(PurpleStringref *stringref)
127 if (stringref == NULL)
128 return;
129 if (REFCOUNT(--(stringref->ref)) == 0) {
130 if (stringref->ref & 0x80000000)
131 gclist = g_list_remove(gclist, stringref);
132 stringref_free(stringref);
136 const char *purple_stringref_value(const PurpleStringref *stringref)
138 return (stringref == NULL ? NULL : stringref->value);
141 int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2)
143 return (s1 == s2 ? 0 : strcmp(purple_stringref_value(s1), purple_stringref_value(s2)));
146 size_t purple_stringref_len(const PurpleStringref *stringref)
148 return strlen(purple_stringref_value(stringref));
151 static void stringref_free(PurpleStringref *stringref)
153 #ifdef DEBUG
154 if (REFCOUNT(stringref->ref) != 0) {
155 purple_debug(PURPLE_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref));
156 return;
158 #endif /* DEBUG */
159 g_free(stringref);
162 static gboolean gs_idle_cb(gpointer data)
164 PurpleStringref *ref;
165 GList *del;
167 while (gclist != NULL) {
168 ref = gclist->data;
169 if (REFCOUNT(ref->ref) == 0) {
170 stringref_free(ref);
172 del = gclist;
173 gclist = gclist->next;
174 g_list_free_1(del);
177 return FALSE;