1 /*
2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
5 *
6 * This is part of HarfBuzz, a text shaping library.
7 *
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
13 *
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * DAMAGE.
19 *
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 *
26 * Contributor(s):
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
30 */
31
32 #ifndef HB_OBJECT_PRIVATE_HH
33 #define HB_OBJECT_PRIVATE_HH
34
35 #include "hb-private.hh"
36
37 #include "hb-atomic-private.hh"
38 #include "hb-mutex-private.hh"
39
40
41 /* Debug */
42
43 #ifndef HB_DEBUG_OBJECT
44 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
45 #endif
46
47
48 /* reference_count */
49
50 #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
51 #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
52 struct hb_reference_count_t
53 {
54 hb_atomic_int_t ref_count;
55
inithb_reference_count_t56 inline void init (int v) { ref_count = v; }
inchb_reference_count_t57 inline int inc (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), 1); }
dechb_reference_count_t58 inline int dec (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), -1); }
finishhb_reference_count_t59 inline void finish (void) { ref_count = HB_REFERENCE_COUNT_INVALID_VALUE; }
60
is_invalidhb_reference_count_t61 inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; }
62
63 };
64
65
66 /* user_data */
67
68 #define HB_USER_DATA_ARRAY_INIT {HB_MUTEX_INIT, HB_LOCKABLE_SET_INIT}
69 struct hb_user_data_array_t
70 {
71 struct hb_user_data_item_t {
72 hb_user_data_key_t *key;
73 void *data;
74 hb_destroy_func_t destroy;
75
operator ==hb_user_data_array_t::hb_user_data_item_t76 inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
operator ==hb_user_data_array_t::hb_user_data_item_t77 inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
78
finishhb_user_data_array_t::hb_user_data_item_t79 void finish (void) { if (destroy) destroy (data); }
80 };
81
82 hb_mutex_t lock;
83 hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
84
inithb_user_data_array_t85 inline void init (void) { lock.init (); items.init (); }
86
87 HB_INTERNAL bool set (hb_user_data_key_t *key,
88 void * data,
89 hb_destroy_func_t destroy,
90 hb_bool_t replace);
91
92 HB_INTERNAL void *get (hb_user_data_key_t *key);
93
finishhb_user_data_array_t94 inline void finish (void) { items.finish (lock); lock.finish (); }
95 };
96
97
98 /* object_header */
99
100 struct hb_object_header_t
101 {
102 hb_reference_count_t ref_count;
103 hb_user_data_array_t user_data;
104
105 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID, HB_USER_DATA_ARRAY_INIT}
106
107 private:
108 ASSERT_POD ();
109 };
110
111
112 /* object */
113
114 template <typename Type>
hb_object_trace(const Type * obj,const char * function)115 static inline void hb_object_trace (const Type *obj, const char *function)
116 {
117 DEBUG_MSG (OBJECT, (void *) obj,
118 "%s refcount=%d",
119 function,
120 obj ? obj->header.ref_count.ref_count : 0);
121 }
122
123 template <typename Type>
hb_object_create(void)124 static inline Type *hb_object_create (void)
125 {
126 Type *obj = (Type *) calloc (1, sizeof (Type));
127
128 if (unlikely (!obj))
129 return obj;
130
131 hb_object_init (obj);
132 hb_object_trace (obj, HB_FUNC);
133 return obj;
134 }
135 template <typename Type>
hb_object_init(Type * obj)136 static inline void hb_object_init (Type *obj)
137 {
138 obj->header.ref_count.init (1);
139 obj->header.user_data.init ();
140 }
141 template <typename Type>
hb_object_is_inert(const Type * obj)142 static inline bool hb_object_is_inert (const Type *obj)
143 {
144 return unlikely (obj->header.ref_count.is_invalid ());
145 }
146 template <typename Type>
hb_object_reference(Type * obj)147 static inline Type *hb_object_reference (Type *obj)
148 {
149 hb_object_trace (obj, HB_FUNC);
150 if (unlikely (!obj || hb_object_is_inert (obj)))
151 return obj;
152 obj->header.ref_count.inc ();
153 return obj;
154 }
155 template <typename Type>
hb_object_destroy(Type * obj)156 static inline bool hb_object_destroy (Type *obj)
157 {
158 hb_object_trace (obj, HB_FUNC);
159 if (unlikely (!obj || hb_object_is_inert (obj)))
160 return false;
161 if (obj->header.ref_count.dec () != 1)
162 return false;
163
164 obj->header.ref_count.finish (); /* Do this before user_data */
165 obj->header.user_data.finish ();
166 return true;
167 }
168 template <typename Type>
hb_object_set_user_data(Type * obj,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)169 static inline bool hb_object_set_user_data (Type *obj,
170 hb_user_data_key_t *key,
171 void * data,
172 hb_destroy_func_t destroy,
173 hb_bool_t replace)
174 {
175 if (unlikely (!obj || hb_object_is_inert (obj)))
176 return false;
177 return obj->header.user_data.set (key, data, destroy, replace);
178 }
179
180 template <typename Type>
hb_object_get_user_data(Type * obj,hb_user_data_key_t * key)181 static inline void *hb_object_get_user_data (Type *obj,
182 hb_user_data_key_t *key)
183 {
184 if (unlikely (!obj || hb_object_is_inert (obj)))
185 return NULL;
186 return obj->header.user_data.get (key);
187 }
188
189
190 #endif /* HB_OBJECT_PRIVATE_HH */
191