Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-object-private.hh
blob8a9ae34dbeeb06bb8089a37f91c446a64f52a8a7
1 /*
2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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.
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.
26 * Contributor(s):
27 * Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
32 #ifndef HB_OBJECT_PRIVATE_HH
33 #define HB_OBJECT_PRIVATE_HH
35 #include "hb-private.hh"
37 #include "hb-atomic-private.hh"
38 #include "hb-mutex-private.hh"
41 /* Debug */
43 #ifndef HB_DEBUG_OBJECT
44 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
45 #endif
48 /* reference_count */
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
54 hb_atomic_int_t ref_count;
56 inline void init (int v) { ref_count = v; }
57 inline int inc (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), 1); }
58 inline int dec (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), -1); }
59 inline void finish (void) { ref_count = HB_REFERENCE_COUNT_INVALID_VALUE; }
61 inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; }
66 /* user_data */
68 #define HB_USER_DATA_ARRAY_INIT {HB_MUTEX_INIT, HB_LOCKABLE_SET_INIT}
69 struct hb_user_data_array_t
71 /* TODO Add tracing. */
73 struct hb_user_data_item_t {
74 hb_user_data_key_t *key;
75 void *data;
76 hb_destroy_func_t destroy;
78 inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
79 inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
81 void finish (void) { if (destroy) destroy (data); }
84 hb_mutex_t lock;
85 hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
87 inline void init (void) { lock.init (); items.init (); }
89 HB_INTERNAL bool set (hb_user_data_key_t *key,
90 void * data,
91 hb_destroy_func_t destroy,
92 hb_bool_t replace);
94 HB_INTERNAL void *get (hb_user_data_key_t *key);
96 inline void finish (void) { items.finish (lock); lock.finish (); }
100 /* object_header */
102 struct hb_object_header_t
104 hb_reference_count_t ref_count;
105 hb_user_data_array_t user_data;
107 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID, HB_USER_DATA_ARRAY_INIT}
109 static inline void *create (unsigned int size) {
110 hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
112 if (likely (obj))
113 obj->init ();
115 return obj;
118 inline void init (void) {
119 ref_count.init (1);
120 user_data.init ();
123 inline bool is_inert (void) const {
124 return unlikely (ref_count.is_invalid ());
127 inline void reference (void) {
128 if (unlikely (!this || this->is_inert ()))
129 return;
130 ref_count.inc ();
133 inline bool destroy (void) {
134 if (unlikely (!this || this->is_inert ()))
135 return false;
136 if (ref_count.dec () != 1)
137 return false;
139 ref_count.finish (); /* Do this before user_data */
140 user_data.finish ();
142 return true;
145 inline bool set_user_data (hb_user_data_key_t *key,
146 void * data,
147 hb_destroy_func_t destroy_func,
148 hb_bool_t replace) {
149 if (unlikely (!this || this->is_inert ()))
150 return false;
152 return user_data.set (key, data, destroy_func, replace);
155 inline void *get_user_data (hb_user_data_key_t *key) {
156 if (unlikely (!this || this->is_inert ()))
157 return NULL;
159 return user_data.get (key);
162 inline void trace (const char *function) const {
163 if (unlikely (!this)) return;
164 /* TODO We cannot use DEBUG_MSG_FUNC here since that one currently only
165 * prints the class name and throws away the template info. */
166 DEBUG_MSG (OBJECT, (void *) this,
167 "%s refcount=%d",
168 function,
169 this ? ref_count.ref_count : 0);
172 private:
173 ASSERT_POD ();
177 /* object */
179 template <typename Type>
180 static inline void hb_object_trace (const Type *obj, const char *function)
182 obj->header.trace (function);
184 template <typename Type>
185 static inline Type *hb_object_create (void)
187 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
188 hb_object_trace (obj, HB_FUNC);
189 return obj;
191 template <typename Type>
192 static inline bool hb_object_is_inert (const Type *obj)
194 return unlikely (obj->header.is_inert ());
196 template <typename Type>
197 static inline Type *hb_object_reference (Type *obj)
199 hb_object_trace (obj, HB_FUNC);
200 obj->header.reference ();
201 return obj;
203 template <typename Type>
204 static inline bool hb_object_destroy (Type *obj)
206 hb_object_trace (obj, HB_FUNC);
207 return obj->header.destroy ();
209 template <typename Type>
210 static inline bool hb_object_set_user_data (Type *obj,
211 hb_user_data_key_t *key,
212 void * data,
213 hb_destroy_func_t destroy,
214 hb_bool_t replace)
216 return obj->header.set_user_data (key, data, destroy, replace);
219 template <typename Type>
220 static inline void *hb_object_get_user_data (Type *obj,
221 hb_user_data_key_t *key)
223 return obj->header.get_user_data (key);
227 #endif /* HB_OBJECT_PRIVATE_HH */