Fri, 18 May 2007 22:04:01 +0000
Added to gtkutils.c:
GtkWidget *pidgin_create_window(const char *title, guint border_width, const char *role, gboolean resizable);
| 7763 | 1 | /** |
| 7786 | 2 | * @file stringref.c Reference-counted immutable strings |
| 7763 | 3 | * @ingroup core |
| 4 | * | |
| 15884 | 5 | * purple |
| 7763 | 6 | * |
| 15884 | 7 | * Purple is the legal property of its developers, whose names are too numerous |
| 8046 | 8 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 9 | * source distribution. | |
| 7763 | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify | |
| 12 | * it under the terms of the GNU General Public License as published by | |
| 13 | * the Free Software Foundation; either version 2 of the License, or | |
| 14 | * (at your option) any later version. | |
| 15 | * | |
| 16 | * This program is distributed in the hope that it will be useful, | |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 19 | * GNU General Public License for more details. | |
| 20 | * | |
| 21 | * You should have received a copy of the GNU General Public License | |
| 22 | * along with this program; if not, write to the Free Software | |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 24 | * | |
| 25 | */ | |
| 26 | ||
| 27 | #include "internal.h" | |
| 28 | ||
| 29 | #include <string.h> | |
| 7767 | 30 | #include <stdarg.h> |
| 7763 | 31 | |
| 7789 | 32 | #include "debug.h" |
| 7763 | 33 | #include "stringref.h" |
| 34 | ||
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
35 | /** |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
36 | * The internal representation of a stringref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
37 | * |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
38 | * @note For this structure to be useful, the string contained within |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
39 | * it must be immutable -- for this reason, do _not_ access it |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
40 | * directly! |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
41 | */ |
| 15884 | 42 | struct _PurpleStringref { |
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
43 | guint32 ref; /**< The reference count of this string. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
44 | * Note that reference counts are only |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
45 | * 31 bits, and the high-order bit |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
46 | * indicates whether this string is up |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
47 | * for GC at the next idle handler... |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
48 | * But you aren't going to touch this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
49 | * anyway, right? */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
50 | char value[1]; /**< The string contained in this ref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
51 | * Notice that it is simply "hanging |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
52 | * off the end" of the ref ... this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
53 | * is to save an allocation. */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
54 | }; |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
55 | |
| 7795 | 56 | #define REFCOUNT(x) ((x) & 0x7fffffff) |
| 57 | ||
| 7786 | 58 | static GList *gclist = NULL; |
| 59 | ||
| 15884 | 60 | static void stringref_free(PurpleStringref *stringref); |
| 7786 | 61 | static gboolean gs_idle_cb(gpointer data); |
| 62 | ||
| 15884 | 63 | PurpleStringref *purple_stringref_new(const char *value) |
| 7763 | 64 | { |
| 15884 | 65 | PurpleStringref *newref; |
| 7763 | 66 | |
| 7772 | 67 | if (value == NULL) |
| 68 | return NULL; | |
| 69 | ||
| 15884 | 70 | newref = g_malloc(sizeof(PurpleStringref) + strlen(value)); |
| 7763 | 71 | strcpy(newref->value, value); |
| 72 | newref->ref = 1; | |
| 73 | ||
| 74 | return newref; | |
| 75 | } | |
| 76 | ||
| 15884 | 77 | PurpleStringref *purple_stringref_new_noref(const char *value) |
| 7786 | 78 | { |
| 15884 | 79 | PurpleStringref *newref; |
| 7786 | 80 | |
| 81 | if (value == NULL) | |
| 82 | return NULL; | |
| 83 | ||
| 15884 | 84 | newref = g_malloc(sizeof(PurpleStringref) + strlen(value)); |
| 7786 | 85 | strcpy(newref->value, value); |
| 86 | newref->ref = 0x80000000; | |
| 87 | ||
| 88 | if (gclist == NULL) | |
| 89 | g_idle_add(gs_idle_cb, NULL); | |
| 90 | gclist = g_list_prepend(gclist, newref); | |
| 91 | ||
| 92 | return newref; | |
| 93 | } | |
| 94 | ||
| 15884 | 95 | PurpleStringref *purple_stringref_printf(const char *format, ...) |
| 7767 | 96 | { |
| 15884 | 97 | PurpleStringref *newref; |
| 7767 | 98 | va_list ap; |
| 99 | ||
| 100 | if (format == NULL) | |
| 101 | return NULL; | |
| 102 | ||
| 103 | va_start(ap, format); | |
| 15884 | 104 | newref = g_malloc(sizeof(PurpleStringref) + g_printf_string_upper_bound(format, ap)); |
| 7767 | 105 | vsprintf(newref->value, format, ap); |
| 106 | va_end(ap); | |
| 7784 | 107 | newref->ref = 1; |
| 7767 | 108 | |
| 109 | return newref; | |
| 110 | } | |
| 111 | ||
| 15884 | 112 | PurpleStringref *purple_stringref_ref(PurpleStringref *stringref) |
| 7763 | 113 | { |
| 114 | if (stringref == NULL) | |
| 115 | return NULL; | |
| 116 | stringref->ref++; | |
| 117 | return stringref; | |
| 118 | } | |
| 119 | ||
| 15884 | 120 | void purple_stringref_unref(PurpleStringref *stringref) |
| 7763 | 121 | { |
| 7790 | 122 | if (stringref == NULL) |
| 123 | return; | |
| 7795 | 124 | if (REFCOUNT(--(stringref->ref)) == 0) { |
| 7786 | 125 | if (stringref->ref & 0x80000000) |
| 126 | gclist = g_list_remove(gclist, stringref); | |
| 7788 | 127 | stringref_free(stringref); |
| 7786 | 128 | } |
| 7763 | 129 | } |
| 130 | ||
| 15884 | 131 | const char *purple_stringref_value(const PurpleStringref *stringref) |
| 7763 | 132 | { |
| 133 | return (stringref == NULL ? NULL : stringref->value); | |
| 134 | } | |
| 7786 | 135 | |
| 15884 | 136 | int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2) |
| 7786 | 137 | { |
| 15884 | 138 | return (s1 == s2 ? 0 : strcmp(purple_stringref_value(s1), purple_stringref_value(s2))); |
| 7786 | 139 | } |
| 140 | ||
| 15884 | 141 | size_t purple_stringref_len(const PurpleStringref *stringref) |
| 7786 | 142 | { |
| 15884 | 143 | return strlen(purple_stringref_value(stringref)); |
| 7786 | 144 | } |
| 145 | ||
| 15884 | 146 | static void stringref_free(PurpleStringref *stringref) |
| 7786 | 147 | { |
| 148 | #ifdef DEBUG | |
| 7795 | 149 | if (REFCOUNT(stringref->ref) != 0) { |
| 15884 | 150 | purple_debug(PURPLE_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref)); |
| 7786 | 151 | return; |
| 152 | } | |
| 153 | #endif /* DEBUG */ | |
| 154 | g_free(stringref); | |
| 155 | } | |
| 156 | ||
| 157 | static gboolean gs_idle_cb(gpointer data) | |
| 158 | { | |
| 15884 | 159 | PurpleStringref *ref; |
| 7786 | 160 | GList *del; |
| 161 | ||
| 162 | while (gclist != NULL) { | |
| 163 | ref = gclist->data; | |
| 7795 | 164 | if (REFCOUNT(ref->ref) == 0) { |
| 7786 | 165 | stringref_free(ref); |
| 166 | } | |
| 167 | del = gclist; | |
| 168 | gclist = gclist->next; | |
| 169 | g_list_free_1(del); | |
| 170 | } | |
| 171 | ||
| 172 | return FALSE; | |
| 173 | } |