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