Fri, 14 Feb 2020 01:49:34 -0600
closing merged branch
|
20147
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
1 | /* purple |
| 7763 | 2 | * |
| 15884 | 3 | * Purple is the legal property of its developers, whose names are too numerous |
| 8046 | 4 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 5 | * source distribution. | |
| 7763 | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU General Public License as published by | |
| 9 | * the Free Software Foundation; either version 2 of the License, or | |
| 10 | * (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU General Public License | |
| 18 | * 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
|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 7763 | 20 | * |
| 21 | */ | |
| 22 | ||
| 23 | #include "internal.h" | |
| 24 | ||
| 25 | #include <string.h> | |
| 7767 | 26 | #include <stdarg.h> |
| 7763 | 27 | |
| 7789 | 28 | #include "debug.h" |
|
22351
6ca0640b3d31
Change some g_idle_add(...) calls in libpurple to purple_timeout_add(0, ...)
Mark Doliner <markdoliner@pidgin.im>
parents:
20147
diff
changeset
|
29 | #include "eventloop.h" |
| 7763 | 30 | #include "stringref.h" |
| 31 | ||
|
35466
e259e75f1c6f
Fix some gtk-doc warnings till xmlnode.h
Ankit Vani <a@nevitus.org>
parents:
35454
diff
changeset
|
32 | /* |
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
33 | * The internal representation of a stringref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
34 | * |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
35 | * @note For this structure to be useful, the string contained within |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
36 | * it must be immutable -- for this reason, do _not_ access it |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
37 | * directly! |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
38 | */ |
| 15884 | 39 | struct _PurpleStringref { |
|
35466
e259e75f1c6f
Fix some gtk-doc warnings till xmlnode.h
Ankit Vani <a@nevitus.org>
parents:
35454
diff
changeset
|
40 | guint32 ref; /* The reference count of this string. |
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
41 | * Note that reference counts are only |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
42 | * 31 bits, and the high-order bit |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
43 | * indicates whether this string is up |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
44 | * for GC at the next idle handler... |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
45 | * But you aren't going to touch this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
46 | * anyway, right? */ |
|
35466
e259e75f1c6f
Fix some gtk-doc warnings till xmlnode.h
Ankit Vani <a@nevitus.org>
parents:
35454
diff
changeset
|
47 | char value[1]; /* The string contained in this ref. |
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
48 | * Notice that it is simply "hanging |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
49 | * off the end" of the ref ... this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
50 | * is to save an allocation. */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
51 | }; |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
52 | |
| 7795 | 53 | #define REFCOUNT(x) ((x) & 0x7fffffff) |
| 54 | ||
| 7786 | 55 | static GList *gclist = NULL; |
| 56 | ||
| 15884 | 57 | static void stringref_free(PurpleStringref *stringref); |
| 7786 | 58 | static gboolean gs_idle_cb(gpointer data); |
| 59 | ||
| 15884 | 60 | PurpleStringref *purple_stringref_new(const char *value) |
| 7763 | 61 | { |
| 15884 | 62 | PurpleStringref *newref; |
|
32024
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
63 | size_t len; |
| 7763 | 64 | |
| 7772 | 65 | if (value == NULL) |
| 66 | return NULL; | |
| 67 | ||
|
32024
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
68 | len = strlen(value); |
|
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
69 | |
|
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
70 | newref = g_malloc(sizeof(PurpleStringref) + len); |
|
33594
ef3d115642d7
Fix purple_stringref_new()
Richard Laager <rlaager@pidgin.im>
parents:
32024
diff
changeset
|
71 | /* g_strlcpy() takes the size of the buffer, including the NUL. |
|
ef3d115642d7
Fix purple_stringref_new()
Richard Laager <rlaager@pidgin.im>
parents:
32024
diff
changeset
|
72 | strlen() returns the length of the string, without the NUL. */ |
|
ef3d115642d7
Fix purple_stringref_new()
Richard Laager <rlaager@pidgin.im>
parents:
32024
diff
changeset
|
73 | g_strlcpy(newref->value, value, len + 1); |
| 7763 | 74 | newref->ref = 1; |
| 75 | ||
| 76 | return newref; | |
| 77 | } | |
| 78 | ||
| 15884 | 79 | PurpleStringref *purple_stringref_new_noref(const char *value) |
| 7786 | 80 | { |
| 15884 | 81 | PurpleStringref *newref; |
| 7786 | 82 | |
| 83 | if (value == NULL) | |
| 84 | return NULL; | |
| 85 | ||
| 15884 | 86 | newref = g_malloc(sizeof(PurpleStringref) + strlen(value)); |
| 7786 | 87 | strcpy(newref->value, value); |
| 88 | newref->ref = 0x80000000; | |
| 89 | ||
| 90 | if (gclist == NULL) | |
|
38433
361c801c4536
Remove purple_timeout_* function usage
Mike Ruprecht <cmaiku@gmail.com>
parents:
35466
diff
changeset
|
91 | g_timeout_add(0, gs_idle_cb, NULL); |
| 7786 | 92 | gclist = g_list_prepend(gclist, newref); |
| 93 | ||
| 94 | return newref; | |
| 95 | } | |
| 96 | ||
| 15884 | 97 | PurpleStringref *purple_stringref_printf(const char *format, ...) |
| 7767 | 98 | { |
| 15884 | 99 | PurpleStringref *newref; |
| 7767 | 100 | va_list ap; |
| 101 | ||
| 102 | if (format == NULL) | |
| 103 | return NULL; | |
| 104 | ||
| 105 | va_start(ap, format); | |
| 15884 | 106 | newref = g_malloc(sizeof(PurpleStringref) + g_printf_string_upper_bound(format, ap)); |
| 7767 | 107 | vsprintf(newref->value, format, ap); |
| 108 | va_end(ap); | |
| 7784 | 109 | newref->ref = 1; |
| 7767 | 110 | |
| 111 | return newref; | |
| 112 | } | |
| 113 | ||
| 15884 | 114 | PurpleStringref *purple_stringref_ref(PurpleStringref *stringref) |
| 7763 | 115 | { |
| 116 | if (stringref == NULL) | |
| 117 | return NULL; | |
| 118 | stringref->ref++; | |
| 119 | return stringref; | |
| 120 | } | |
| 121 | ||
| 15884 | 122 | void purple_stringref_unref(PurpleStringref *stringref) |
| 7763 | 123 | { |
| 7790 | 124 | if (stringref == NULL) |
| 125 | return; | |
| 7795 | 126 | if (REFCOUNT(--(stringref->ref)) == 0) { |
| 7786 | 127 | if (stringref->ref & 0x80000000) |
| 128 | gclist = g_list_remove(gclist, stringref); | |
| 7788 | 129 | stringref_free(stringref); |
| 7786 | 130 | } |
| 7763 | 131 | } |
| 132 | ||
| 15884 | 133 | const char *purple_stringref_value(const PurpleStringref *stringref) |
| 7763 | 134 | { |
| 135 | return (stringref == NULL ? NULL : stringref->value); | |
| 136 | } | |
| 7786 | 137 | |
| 15884 | 138 | int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2) |
| 7786 | 139 | { |
| 15884 | 140 | return (s1 == s2 ? 0 : strcmp(purple_stringref_value(s1), purple_stringref_value(s2))); |
| 7786 | 141 | } |
| 142 | ||
| 15884 | 143 | size_t purple_stringref_len(const PurpleStringref *stringref) |
| 7786 | 144 | { |
| 15884 | 145 | return strlen(purple_stringref_value(stringref)); |
| 7786 | 146 | } |
| 147 | ||
| 15884 | 148 | static void stringref_free(PurpleStringref *stringref) |
| 7786 | 149 | { |
| 40139 | 150 | g_return_if_fail(stringref != NULL); |
| 151 | ||
| 40135 | 152 | if (REFCOUNT(stringref->ref) == 0) { |
| 153 | g_free(stringref); | |
| 7786 | 154 | return; |
| 155 | } | |
| 40135 | 156 | #ifdef DEBUG |
| 157 | purple_debug(PURPLE_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref)); | |
| 7786 | 158 | #endif /* DEBUG */ |
| 159 | } | |
| 160 | ||
| 161 | static gboolean gs_idle_cb(gpointer data) | |
| 162 | { | |
| 40135 | 163 | g_list_free_full(gclist, (GDestroyNotify)stringref_free); |
| 40141 | 164 | gclist = NULL; |
| 7786 | 165 | |
| 166 | return FALSE; | |
| 167 | } |