Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-object-private.hh
blob635d62dc1aed00029d65bcbd62ae64248f59d65e
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 -1
51 #define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT(HB_REFERENCE_COUNT_INVALID_VALUE)}
53 struct hb_reference_count_t
55 hb_atomic_int_t ref_count;
57 inline void init (int v) { ref_count.set_unsafe (v); }
58 inline int get_unsafe (void) const { return ref_count.get_unsafe (); }
59 inline int inc (void) { return ref_count.inc (); }
60 inline int dec (void) { return ref_count.dec (); }
61 inline void finish (void) { ref_count.set_unsafe (HB_REFERENCE_COUNT_INVALID_VALUE); }
63 inline bool is_invalid (void) const { return ref_count.get_unsafe () == HB_REFERENCE_COUNT_INVALID_VALUE; }
67 /* user_data */
69 #define HB_USER_DATA_ARRAY_INIT {HB_MUTEX_INIT, HB_LOCKABLE_SET_INIT}
70 struct hb_user_data_array_t
72 struct hb_user_data_item_t {
73 hb_user_data_key_t *key;
74 void *data;
75 hb_destroy_func_t destroy;
77 inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
78 inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
80 void finish (void) { if (destroy) destroy (data); }
83 hb_mutex_t lock;
84 hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
86 inline void init (void) { lock.init (); items.init (); }
88 HB_INTERNAL bool set (hb_user_data_key_t *key,
89 void * data,
90 hb_destroy_func_t destroy,
91 hb_bool_t replace);
93 HB_INTERNAL void *get (hb_user_data_key_t *key);
95 inline void finish (void) { items.finish (lock); lock.finish (); }
99 /* object_header */
101 struct hb_object_header_t
103 hb_reference_count_t ref_count;
104 hb_user_data_array_t user_data;
106 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INIT, HB_USER_DATA_ARRAY_INIT}
108 private:
109 ASSERT_POD ();
113 /* object */
115 template <typename Type>
116 static inline void hb_object_trace (const Type *obj, const char *function)
118 DEBUG_MSG (OBJECT, (void *) obj,
119 "%s refcount=%d",
120 function,
121 obj ? obj->header.ref_count.get_unsafe () : 0);
124 template <typename Type>
125 static inline Type *hb_object_create (void)
127 Type *obj = (Type *) calloc (1, sizeof (Type));
129 if (unlikely (!obj))
130 return obj;
132 hb_object_init (obj);
133 hb_object_trace (obj, HB_FUNC);
134 return obj;
136 template <typename Type>
137 static inline void hb_object_init (Type *obj)
139 obj->header.ref_count.init (1);
140 obj->header.user_data.init ();
142 template <typename Type>
143 static inline bool hb_object_is_inert (const Type *obj)
145 return unlikely (obj->header.ref_count.is_invalid ());
147 template <typename Type>
148 static inline Type *hb_object_reference (Type *obj)
150 hb_object_trace (obj, HB_FUNC);
151 if (unlikely (!obj || hb_object_is_inert (obj)))
152 return obj;
153 obj->header.ref_count.inc ();
154 return obj;
156 template <typename Type>
157 static inline bool hb_object_destroy (Type *obj)
159 hb_object_trace (obj, HB_FUNC);
160 if (unlikely (!obj || hb_object_is_inert (obj)))
161 return false;
162 if (obj->header.ref_count.dec () != 1)
163 return false;
165 obj->header.ref_count.finish (); /* Do this before user_data */
166 obj->header.user_data.finish ();
167 return true;
169 template <typename Type>
170 static inline bool hb_object_set_user_data (Type *obj,
171 hb_user_data_key_t *key,
172 void * data,
173 hb_destroy_func_t destroy,
174 hb_bool_t replace)
176 if (unlikely (!obj || hb_object_is_inert (obj)))
177 return false;
178 return obj->header.user_data.set (key, data, destroy, replace);
181 template <typename Type>
182 static inline void *hb_object_get_user_data (Type *obj,
183 hb_user_data_key_t *key)
185 if (unlikely (!obj || hb_object_is_inert (obj)))
186 return NULL;
187 return obj->header.user_data.get (key);
191 #endif /* HB_OBJECT_PRIVATE_HH */