Remove lock_instance()
[framework/uifw/harfbuzz.git] / src / hb-object-private.h
1 /*
2  * Copyright (C) 2007 Chris Wilson
3  * Copyright (C) 2009  Red Hat, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Contributor(s):
26  *      Chris Wilson <chris@chris-wilson.co.uk>
27  * Red Hat Author(s): Behdad Esfahbod
28  */
29
30 #ifndef HB_REFCOUNT_PRIVATE_H
31 #define HB_REFCOUNT_PRIVATE_H
32
33
34 #ifndef HB_DEBUG_OBJECT
35 #define HB_DEBUG_OBJECT HB_DEBUG
36 #endif
37
38 #if HB_DEBUG_OBJECT
39 #include <stdio.h>
40 #define HB_OBJECT_DEBUG_OUT(obj) fprintf (stderr, "%p refcount=%d %s\n", \
41                                           obj, \
42                                           HB_REFERENCE_COUNT_GET_VALUE (obj->ref_count), \
43                                           __FUNCTION__)
44 #else
45 #define HB_OBJECT_DEBUG_OUT(obj) (void) 0
46 #endif
47
48
49 /* Encapsulate operations on the object's reference count */
50 typedef struct {
51   hb_atomic_int_t ref_count;
52 } hb_reference_count_t;
53
54 #define hb_reference_count_inc(RC) hb_atomic_int_fetch_and_add ((RC).ref_count, 1)
55 #define hb_reference_count_dec(RC) hb_atomic_int_fetch_and_add ((RC).ref_count, -1)
56
57 #define HB_REFERENCE_COUNT_INIT(RC, VALUE) ((RC).ref_count = (VALUE))
58
59 #define HB_REFERENCE_COUNT_GET_VALUE(RC) hb_atomic_int_get ((RC).ref_count)
60 #define HB_REFERENCE_COUNT_SET_VALUE(RC, VALUE) hb_atomic_int_set ((RC).ref_count, (VALUE))
61
62 #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
63 #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
64
65 #define HB_REFERENCE_COUNT_IS_INVALID(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) == HB_REFERENCE_COUNT_INVALID_VALUE)
66
67 #define HB_REFERENCE_COUNT_HAS_REFERENCE(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) > 0)
68
69
70
71 /* Helper macros */
72
73 #define HB_OBJECT_IS_INERT(obj) \
74     (HB_UNLIKELY (HB_REFERENCE_COUNT_IS_INVALID ((obj)->ref_count)))
75
76 #define HB_OBJECT_DO_INIT_EXPR(obj) \
77     HB_REFERENCE_COUNT_INIT (obj->ref_count, 1)
78
79 #define HB_OBJECT_DO_INIT(obj) \
80   HB_STMT_START { \
81     HB_OBJECT_DO_INIT_EXPR (obj); \
82   } HB_STMT_END
83
84 #define HB_OBJECT_DO_CREATE(Type, obj) \
85   HB_LIKELY (( \
86              (obj) = (Type *) calloc (1, sizeof (Type)), \
87              HB_OBJECT_DO_INIT_EXPR (obj), \
88              HB_OBJECT_DEBUG_OUT (obj), \
89              (obj) \
90              ))
91
92 #define HB_OBJECT_DO_REFERENCE(obj) \
93   HB_STMT_START { \
94     int old_count; \
95     if (HB_UNLIKELY (!(obj) || HB_OBJECT_IS_INERT (obj))) \
96       return obj; \
97     HB_OBJECT_DEBUG_OUT (obj); \
98     old_count = hb_reference_count_inc (obj->ref_count); \
99     assert (old_count > 0); \
100     return obj; \
101   } HB_STMT_END
102
103 #define HB_OBJECT_DO_GET_REFERENCE_COUNT(obj) \
104   HB_STMT_START { \
105     if (HB_UNLIKELY (!(obj) || HB_OBJECT_IS_INERT (obj))) \
106       return 0; \
107     return HB_REFERENCE_COUNT_GET_VALUE (obj->ref_count); \
108   } HB_STMT_END
109
110 #define HB_OBJECT_DO_DESTROY(obj) \
111   HB_STMT_START { \
112     int old_count; \
113     if (HB_UNLIKELY (!(obj) || HB_OBJECT_IS_INERT (obj))) \
114       return; \
115     HB_OBJECT_DEBUG_OUT (obj); \
116     old_count = hb_reference_count_dec (obj->ref_count); \
117     assert (old_count > 0); \
118     if (old_count != 1) \
119       return; \
120   } HB_STMT_END
121
122
123 #endif /* HB_REFCOUNT_PRIVATE_H */