7763
|
1 /**
|
7786
|
2 * @file stringref.c Reference-counted immutable strings
|
7763
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Copyright (C) 2003 Ethan Blanton <elb@elitists.net>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 *
|
|
23 */
|
|
24
|
|
25 #include "internal.h"
|
|
26
|
|
27 #include <string.h>
|
7767
|
28 #include <stdarg.h>
|
7763
|
29
|
7789
|
30 #include "debug.h"
|
7763
|
31 #include "stringref.h"
|
|
32
|
7795
|
33 #define REFCOUNT(x) ((x) & 0x7fffffff)
|
|
34
|
7786
|
35 static GList *gclist = NULL;
|
|
36
|
|
37 static void stringref_free(GaimStringref *stringref);
|
|
38 static gboolean gs_idle_cb(gpointer data);
|
|
39
|
7763
|
40 GaimStringref *gaim_stringref_new(const char *value)
|
|
41 {
|
|
42 GaimStringref *newref;
|
|
43
|
7772
|
44 if (value == NULL)
|
|
45 return NULL;
|
|
46
|
7763
|
47 newref = g_malloc(sizeof(GaimStringref) + strlen(value) + 1);
|
|
48 strcpy(newref->value, value);
|
|
49 newref->ref = 1;
|
|
50
|
|
51 return newref;
|
|
52 }
|
|
53
|
7786
|
54 GaimStringref *gaim_stringref_new_noref(const char *value)
|
|
55 {
|
|
56 GaimStringref *newref;
|
|
57
|
|
58 if (value == NULL)
|
|
59 return NULL;
|
|
60
|
|
61 newref = g_malloc(sizeof(GaimStringref) + strlen(value) + 1);
|
|
62 strcpy(newref->value, value);
|
|
63 newref->ref = 0x80000000;
|
|
64
|
|
65 if (gclist == NULL)
|
|
66 g_idle_add(gs_idle_cb, NULL);
|
|
67 gclist = g_list_prepend(gclist, newref);
|
|
68
|
|
69 return newref;
|
|
70 }
|
|
71
|
7767
|
72 GaimStringref *gaim_stringref_printf(const char *format, ...)
|
|
73 {
|
|
74 GaimStringref *newref;
|
|
75 va_list ap;
|
|
76
|
|
77 if (format == NULL)
|
|
78 return NULL;
|
|
79
|
|
80 va_start(ap, format);
|
|
81 newref = g_malloc(sizeof(GaimStringref) + g_printf_string_upper_bound(format, ap));
|
|
82 vsprintf(newref->value, format, ap);
|
|
83 va_end(ap);
|
7784
|
84 newref->ref = 1;
|
7767
|
85
|
|
86 return newref;
|
|
87 }
|
|
88
|
7763
|
89 GaimStringref *gaim_stringref_ref(GaimStringref *stringref)
|
|
90 {
|
|
91 if (stringref == NULL)
|
|
92 return NULL;
|
|
93 stringref->ref++;
|
|
94 return stringref;
|
|
95 }
|
|
96
|
|
97 void gaim_stringref_unref(GaimStringref *stringref)
|
|
98 {
|
7790
|
99 if (stringref == NULL)
|
|
100 return;
|
7795
|
101 if (REFCOUNT(--(stringref->ref)) == 0) {
|
7786
|
102 if (stringref->ref & 0x80000000)
|
|
103 gclist = g_list_remove(gclist, stringref);
|
7788
|
104 stringref_free(stringref);
|
7786
|
105 }
|
7763
|
106 }
|
|
107
|
7768
|
108 const char *gaim_stringref_value(const GaimStringref *stringref)
|
7763
|
109 {
|
|
110 return (stringref == NULL ? NULL : stringref->value);
|
|
111 }
|
7786
|
112
|
|
113 int gaim_stringref_cmp(const GaimStringref *s1, const GaimStringref *s2)
|
|
114 {
|
|
115 return (s1 == s2 ? 0 : strcmp(gaim_stringref_value(s1), gaim_stringref_value(s2)));
|
|
116 }
|
|
117
|
|
118 size_t gaim_stringref_len(const GaimStringref *stringref)
|
|
119 {
|
|
120 return strlen(gaim_stringref_value(stringref));
|
|
121 }
|
|
122
|
|
123 static void stringref_free(GaimStringref *stringref)
|
|
124 {
|
|
125 #ifdef DEBUG
|
7795
|
126 if (REFCOUNT(stringref->ref) != 0) {
|
7796
|
127 gaim_debug(GAIM_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref));
|
7786
|
128 return;
|
|
129 }
|
|
130 #endif /* DEBUG */
|
|
131 g_free(stringref);
|
|
132 }
|
|
133
|
|
134 static gboolean gs_idle_cb(gpointer data)
|
|
135 {
|
|
136 GaimStringref *ref;
|
|
137 GList *del;
|
|
138
|
|
139 while (gclist != NULL) {
|
|
140 ref = gclist->data;
|
7795
|
141 if (REFCOUNT(ref->ref) == 0) {
|
7786
|
142 stringref_free(ref);
|
|
143 }
|
|
144 del = gclist;
|
|
145 gclist = gclist->next;
|
|
146 g_list_free_1(del);
|
|
147 }
|
|
148
|
|
149 return FALSE;
|
|
150 }
|