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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2017 Red Hat, Inc. (www.redhat.com)
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION: camel-weak-ref-group
* @include: camel/camel.h
* @short_description: A weak ref group
*
* A #GWeakRef as such is not suitable for large sets, because
* it causes big performance impact on free. This #CamelWeakRefGroup
* groups together weak references for the same object to minimize
* the performance issue of the #GWeakRef.
**/
#include "evolution-data-server-config.h"
#include <glib.h>
#include "camel-weak-ref-group.h"
struct _CamelWeakRefGroup {
guint ref_count;
gpointer object;
};
G_DEFINE_BOXED_TYPE (CamelWeakRefGroup, camel_weak_ref_group, camel_weak_ref_group_ref, camel_weak_ref_group_unref)
typedef struct _ObjectData {
guint64 use_count;
GWeakRef weakref;
} ObjectData;
static GHashTable *groups = NULL; /* gpointer ~> ObjectData */
G_LOCK_DEFINE_STATIC (groups);
static ObjectData *
object_data_new (gpointer object)
{
ObjectData *od;
od = g_slice_new (ObjectData);
od->use_count = 1;
g_weak_ref_init (&od->weakref, object);
return od;
}
static void
object_data_free (gpointer ptr)
{
ObjectData *od = ptr;
if (od) {
g_warn_if_fail (od->use_count == 0);
g_weak_ref_set (&od->weakref, NULL);
g_weak_ref_clear (&od->weakref);
g_slice_free (ObjectData, od);
}
}
/**
* camel_weak_ref_group_new:
*
* Returns: (transfer full): A new #CamelWeakRefGroup instance, which should
* be freed with camel_weak_ref_group_unref() when no longer needed.
*
* Since: 3.24
**/
CamelWeakRefGroup *
camel_weak_ref_group_new (void)
{
CamelWeakRefGroup *wrg;
wrg = g_slice_new (CamelWeakRefGroup);
wrg->ref_count = 1;
wrg->object = NULL;
return wrg;
}
/**
* camel_weak_ref_group_ref:
* @group: a #CamelWeakRefGroup
*
* Increases a reference count of the @group.
*
* Returns: the @group
*
* Since: 3.24
**/
CamelWeakRefGroup *
camel_weak_ref_group_ref (CamelWeakRefGroup *group)
{
g_return_val_if_fail (group != NULL, NULL);
G_LOCK (groups);
group->ref_count++;
G_UNLOCK (groups);
return group;
}
/**
* camel_weak_ref_group_unref:
* @group: a #CamelWeakRefGroup
*
* Decreases a reference count of the @group. The @group is
* freed when the reference count reaches zero.
*
* Since: 3.24
**/
void
camel_weak_ref_group_unref (CamelWeakRefGroup *group)
{
g_return_if_fail (group != NULL);
g_return_if_fail (group->ref_count > 0);
G_LOCK (groups);
group->ref_count--;
G_UNLOCK (groups);
if (!group->ref_count) {
camel_weak_ref_group_set (group, NULL);
g_slice_free (CamelWeakRefGroup, group);
}
}
/**
* camel_weak_ref_group_set:
* @group: a #CamelWeakRefGroup
* @object: (nullable): a #GObject descendant, or %NULL
*
* Sets the @object as the object help by this @group. If
* the @object is %NULL, then unsets any previously set.
*
* Since: 3.24
**/
void
camel_weak_ref_group_set (CamelWeakRefGroup *group,
gpointer object)
{
g_return_if_fail (group != NULL);
g_return_if_fail (!object || G_IS_OBJECT (object));
G_LOCK (groups);
if (object != group->object) {
ObjectData *od;
if (group->object) {
od = g_hash_table_lookup (groups, group->object);
g_warn_if_fail (od != NULL);
if (od) {
od->use_count--;
if (!od->use_count)
g_hash_table_remove (groups, group->object);
}
} else if (!groups) {
groups = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, object_data_free);
}
group->object = object;
if (group->object) {
od = g_hash_table_lookup (groups, group->object);
if (od) {
od->use_count++;
} else {
od = object_data_new (group->object);
g_hash_table_insert (groups, group->object, od);
}
}
if (groups && !g_hash_table_size (groups)) {
g_hash_table_destroy (groups);
groups = NULL;
}
}
G_UNLOCK (groups);
}
/**
* camel_weak_ref_group_get:
* @group: a #CamelWeakRefGroup
*
* Returns: (transfer full) (nullable): A referenced object associated with
* @group, or %NULL, when no object had been set to it. Use g_object_unref()
* to free it, when no longer needed.
*
* Since: 3.24
**/
gpointer
camel_weak_ref_group_get (CamelWeakRefGroup *group)
{
gpointer object = NULL;
g_return_val_if_fail (group != NULL, NULL);
G_LOCK (groups);
if (group->object) {
ObjectData *od = g_hash_table_lookup (groups, group->object);
g_warn_if_fail (od != NULL);
object = g_weak_ref_get (&od->weakref);
}
G_UNLOCK (groups);
return object;
}
|