forked from Qihoo360/Atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglib-ext-ref.c
More file actions
78 lines (63 loc) · 1.67 KB
/
glib-ext-ref.c
File metadata and controls
78 lines (63 loc) · 1.67 KB
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
/* $%BEGINLICENSE%$
Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the
License.
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 St, Fifth Floor, Boston, MA
02110-1301 USA
$%ENDLICENSE%$ */
#include <glib.h>
#include "glib-ext-ref.h"
/**
* create a new reference object
*
* @see g_ref_unref
*/
GRef *g_ref_new() {
GRef *ref;
ref = g_new0(GRef, 1);
ref->ref_count = 0;
ref->udata = NULL;
return ref;
}
/**
* set the referenced data and its free-function
*
* increments the ref-counter by one
*/
void g_ref_set(GRef *ref, gpointer udata, GDestroyNotify udata_free) {
g_return_if_fail(ref->ref_count == 0);
ref->udata = udata;
ref->udata_free = udata_free;
ref->ref_count = 1;
}
/**
* increment the ref counter
*/
void g_ref_ref(GRef *ref) {
g_return_if_fail(ref->ref_count > 0);
ref->ref_count++;
}
/**
* unreference a object
*
* if no other object references this free the object
*/
void g_ref_unref(GRef *ref) {
if (ref->ref_count == 0) {
/* not set yet */
} else if (--ref->ref_count == 0) {
if (ref->udata_free) {
ref->udata_free(ref->udata);
ref->udata = NULL;
}
g_free(ref);
}
}