Mercurial > pidgin
comparison libgaim/stringref.c @ 14192:60b1bc8dbf37
[gaim-migrate @ 16863]
Renamed 'core' to 'libgaim'
committer: Tailor Script <tailor@pidgin.im>
| author | Evan Schoenberg <evan.s@dreskin.net> |
|---|---|
| date | Sat, 19 Aug 2006 01:50:10 +0000 |
| parents | |
| children | 500a8f54354e |
comparison
equal
deleted
inserted
replaced
| 14191:009db0b357b5 | 14192:60b1bc8dbf37 |
|---|---|
| 1 /** | |
| 2 * @file stringref.c Reference-counted immutable strings | |
| 3 * @ingroup core | |
| 4 * | |
| 5 * gaim | |
| 6 * | |
| 7 * Gaim is the legal property of its developers, whose names are too numerous | |
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 9 * source distribution. | |
| 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> | |
| 30 #include <stdarg.h> | |
| 31 | |
| 32 #include "debug.h" | |
| 33 #include "stringref.h" | |
| 34 | |
| 35 #define REFCOUNT(x) ((x) & 0x7fffffff) | |
| 36 | |
| 37 static GList *gclist = NULL; | |
| 38 | |
| 39 static void stringref_free(GaimStringref *stringref); | |
| 40 static gboolean gs_idle_cb(gpointer data); | |
| 41 | |
| 42 GaimStringref *gaim_stringref_new(const char *value) | |
| 43 { | |
| 44 GaimStringref *newref; | |
| 45 | |
| 46 if (value == NULL) | |
| 47 return NULL; | |
| 48 | |
| 49 newref = g_malloc(sizeof(GaimStringref) + strlen(value)); | |
| 50 strcpy(newref->value, value); | |
| 51 newref->ref = 1; | |
| 52 | |
| 53 return newref; | |
| 54 } | |
| 55 | |
| 56 GaimStringref *gaim_stringref_new_noref(const char *value) | |
| 57 { | |
| 58 GaimStringref *newref; | |
| 59 | |
| 60 if (value == NULL) | |
| 61 return NULL; | |
| 62 | |
| 63 newref = g_malloc(sizeof(GaimStringref) + strlen(value)); | |
| 64 strcpy(newref->value, value); | |
| 65 newref->ref = 0x80000000; | |
| 66 | |
| 67 if (gclist == NULL) | |
| 68 g_idle_add(gs_idle_cb, NULL); | |
| 69 gclist = g_list_prepend(gclist, newref); | |
| 70 | |
| 71 return newref; | |
| 72 } | |
| 73 | |
| 74 GaimStringref *gaim_stringref_printf(const char *format, ...) | |
| 75 { | |
| 76 GaimStringref *newref; | |
| 77 va_list ap; | |
| 78 | |
| 79 if (format == NULL) | |
| 80 return NULL; | |
| 81 | |
| 82 va_start(ap, format); | |
| 83 newref = g_malloc(sizeof(GaimStringref) + g_printf_string_upper_bound(format, ap)); | |
| 84 vsprintf(newref->value, format, ap); | |
| 85 va_end(ap); | |
| 86 newref->ref = 1; | |
| 87 | |
| 88 return newref; | |
| 89 } | |
| 90 | |
| 91 GaimStringref *gaim_stringref_ref(GaimStringref *stringref) | |
| 92 { | |
| 93 if (stringref == NULL) | |
| 94 return NULL; | |
| 95 stringref->ref++; | |
| 96 return stringref; | |
| 97 } | |
| 98 | |
| 99 void gaim_stringref_unref(GaimStringref *stringref) | |
| 100 { | |
| 101 if (stringref == NULL) | |
| 102 return; | |
| 103 if (REFCOUNT(--(stringref->ref)) == 0) { | |
| 104 if (stringref->ref & 0x80000000) | |
| 105 gclist = g_list_remove(gclist, stringref); | |
| 106 stringref_free(stringref); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 const char *gaim_stringref_value(const GaimStringref *stringref) | |
| 111 { | |
| 112 return (stringref == NULL ? NULL : stringref->value); | |
| 113 } | |
| 114 | |
| 115 int gaim_stringref_cmp(const GaimStringref *s1, const GaimStringref *s2) | |
| 116 { | |
| 117 return (s1 == s2 ? 0 : strcmp(gaim_stringref_value(s1), gaim_stringref_value(s2))); | |
| 118 } | |
| 119 | |
| 120 size_t gaim_stringref_len(const GaimStringref *stringref) | |
| 121 { | |
| 122 return strlen(gaim_stringref_value(stringref)); | |
| 123 } | |
| 124 | |
| 125 static void stringref_free(GaimStringref *stringref) | |
| 126 { | |
| 127 #ifdef DEBUG | |
| 128 if (REFCOUNT(stringref->ref) != 0) { | |
| 129 gaim_debug(GAIM_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref)); | |
| 130 return; | |
| 131 } | |
| 132 #endif /* DEBUG */ | |
| 133 g_free(stringref); | |
| 134 } | |
| 135 | |
| 136 static gboolean gs_idle_cb(gpointer data) | |
| 137 { | |
| 138 GaimStringref *ref; | |
| 139 GList *del; | |
| 140 | |
| 141 while (gclist != NULL) { | |
| 142 ref = gclist->data; | |
| 143 if (REFCOUNT(ref->ref) == 0) { | |
| 144 stringref_free(ref); | |
| 145 } | |
| 146 del = gclist; | |
| 147 gclist = gclist->next; | |
| 148 g_list_free_1(del); | |
| 149 } | |
| 150 | |
| 151 return FALSE; | |
| 152 } |
