Tue, 18 Sep 2007 16:32:46 +0000
merge of 'bc4b48f6fbaaf411c3e9245a9d0e3f22d763aacf'
and 'f7a11b4aef9753768661d36921787fad7693ad43'
| 7763 | 1 | /** |
| 7786 | 2 | * @file stringref.c Reference-counted immutable strings |
| 7763 | 3 | * @ingroup core |
|
20330
650a7af9c238
remove gpl boilerplate from doxygen docs
Sean Egan <seanegan@pidgin.im>
parents:
19859
diff
changeset
|
4 | */ |
|
650a7af9c238
remove gpl boilerplate from doxygen docs
Sean Egan <seanegan@pidgin.im>
parents:
19859
diff
changeset
|
5 | |
|
650a7af9c238
remove gpl boilerplate from doxygen docs
Sean Egan <seanegan@pidgin.im>
parents:
19859
diff
changeset
|
6 | /* purple |
| 7763 | 7 | * |
| 15884 | 8 | * Purple is the legal property of its developers, whose names are too numerous |
| 8046 | 9 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 10 | * source distribution. | |
| 7763 | 11 | * |
| 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. | |
| 16 | * | |
| 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. | |
| 21 | * | |
| 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 | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 7763 | 25 | * |
| 26 | */ | |
| 27 | ||
| 28 | #include "internal.h" | |
| 29 | ||
| 30 | #include <string.h> | |
| 7767 | 31 | #include <stdarg.h> |
| 7763 | 32 | |
| 7789 | 33 | #include "debug.h" |
| 7763 | 34 | #include "stringref.h" |
| 35 | ||
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
36 | /** |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
37 | * The internal representation of a stringref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
38 | * |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
39 | * @note For this structure to be useful, the string contained within |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
40 | * it must be immutable -- for this reason, do _not_ access it |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
41 | * directly! |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
42 | */ |
| 15884 | 43 | struct _PurpleStringref { |
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
44 | guint32 ref; /**< The reference count of this string. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
45 | * Note that reference counts are only |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
46 | * 31 bits, and the high-order bit |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
47 | * indicates whether this string is up |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
48 | * for GC at the next idle handler... |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
49 | * But you aren't going to touch this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
50 | * anyway, right? */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
51 | char value[1]; /**< The string contained in this ref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
52 | * Notice that it is simply "hanging |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
53 | * off the end" of the ref ... this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
54 | * is to save an allocation. */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
55 | }; |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
56 | |
| 7795 | 57 | #define REFCOUNT(x) ((x) & 0x7fffffff) |
| 58 | ||
| 7786 | 59 | static GList *gclist = NULL; |
| 60 | ||
| 15884 | 61 | static void stringref_free(PurpleStringref *stringref); |
| 7786 | 62 | static gboolean gs_idle_cb(gpointer data); |
| 63 | ||
| 15884 | 64 | PurpleStringref *purple_stringref_new(const char *value) |
| 7763 | 65 | { |
| 15884 | 66 | PurpleStringref *newref; |
| 7763 | 67 | |
| 7772 | 68 | if (value == NULL) |
| 69 | return NULL; | |
| 70 | ||
| 15884 | 71 | newref = g_malloc(sizeof(PurpleStringref) + strlen(value)); |
| 7763 | 72 | strcpy(newref->value, value); |
| 73 | newref->ref = 1; | |
| 74 | ||
| 75 | return newref; | |
| 76 | } | |
| 77 | ||
| 15884 | 78 | PurpleStringref *purple_stringref_new_noref(const char *value) |
| 7786 | 79 | { |
| 15884 | 80 | PurpleStringref *newref; |
| 7786 | 81 | |
| 82 | if (value == NULL) | |
| 83 | return NULL; | |
| 84 | ||
| 15884 | 85 | newref = g_malloc(sizeof(PurpleStringref) + strlen(value)); |
| 7786 | 86 | strcpy(newref->value, value); |
| 87 | newref->ref = 0x80000000; | |
| 88 | ||
| 89 | if (gclist == NULL) | |
| 90 | g_idle_add(gs_idle_cb, NULL); | |
| 91 | gclist = g_list_prepend(gclist, newref); | |
| 92 | ||
| 93 | return newref; | |
| 94 | } | |
| 95 | ||
| 15884 | 96 | PurpleStringref *purple_stringref_printf(const char *format, ...) |
| 7767 | 97 | { |
| 15884 | 98 | PurpleStringref *newref; |
| 7767 | 99 | va_list ap; |
| 100 | ||
| 101 | if (format == NULL) | |
| 102 | return NULL; | |
| 103 | ||
| 104 | va_start(ap, format); | |
| 15884 | 105 | newref = g_malloc(sizeof(PurpleStringref) + g_printf_string_upper_bound(format, ap)); |
| 7767 | 106 | vsprintf(newref->value, format, ap); |
| 107 | va_end(ap); | |
| 7784 | 108 | newref->ref = 1; |
| 7767 | 109 | |
| 110 | return newref; | |
| 111 | } | |
| 112 | ||
| 15884 | 113 | PurpleStringref *purple_stringref_ref(PurpleStringref *stringref) |
| 7763 | 114 | { |
| 115 | if (stringref == NULL) | |
| 116 | return NULL; | |
| 117 | stringref->ref++; | |
| 118 | return stringref; | |
| 119 | } | |
| 120 | ||
| 15884 | 121 | void purple_stringref_unref(PurpleStringref *stringref) |
| 7763 | 122 | { |
| 7790 | 123 | if (stringref == NULL) |
| 124 | return; | |
| 7795 | 125 | if (REFCOUNT(--(stringref->ref)) == 0) { |
| 7786 | 126 | if (stringref->ref & 0x80000000) |
| 127 | gclist = g_list_remove(gclist, stringref); | |
| 7788 | 128 | stringref_free(stringref); |
| 7786 | 129 | } |
| 7763 | 130 | } |
| 131 | ||
| 15884 | 132 | const char *purple_stringref_value(const PurpleStringref *stringref) |
| 7763 | 133 | { |
| 134 | return (stringref == NULL ? NULL : stringref->value); | |
| 135 | } | |
| 7786 | 136 | |
| 15884 | 137 | int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2) |
| 7786 | 138 | { |
| 15884 | 139 | return (s1 == s2 ? 0 : strcmp(purple_stringref_value(s1), purple_stringref_value(s2))); |
| 7786 | 140 | } |
| 141 | ||
| 15884 | 142 | size_t purple_stringref_len(const PurpleStringref *stringref) |
| 7786 | 143 | { |
| 15884 | 144 | return strlen(purple_stringref_value(stringref)); |
| 7786 | 145 | } |
| 146 | ||
| 15884 | 147 | static void stringref_free(PurpleStringref *stringref) |
| 7786 | 148 | { |
| 149 | #ifdef DEBUG | |
| 7795 | 150 | if (REFCOUNT(stringref->ref) != 0) { |
| 15884 | 151 | purple_debug(PURPLE_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref)); |
| 7786 | 152 | return; |
| 153 | } | |
| 154 | #endif /* DEBUG */ | |
| 155 | g_free(stringref); | |
| 156 | } | |
| 157 | ||
| 158 | static gboolean gs_idle_cb(gpointer data) | |
| 159 | { | |
| 15884 | 160 | PurpleStringref *ref; |
| 7786 | 161 | GList *del; |
| 162 | ||
| 163 | while (gclist != NULL) { | |
| 164 | ref = gclist->data; | |
| 7795 | 165 | if (REFCOUNT(ref->ref) == 0) { |
| 7786 | 166 | stringref_free(ref); |
| 167 | } | |
| 168 | del = gclist; | |
| 169 | gclist = gclist->next; | |
| 170 | g_list_free_1(del); | |
| 171 | } | |
| 172 | ||
| 173 | return FALSE; | |
| 174 | } |