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