2 * Copyright © 2007 Chris Wilson
3 * Copyright © 2009,2010 Red Hat, Inc.
4 * Copyright © 2011 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
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.
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"
42 #ifndef HB_DEBUG_OBJECT
43 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
52 template <typename Type, unsigned int StaticSize>
53 struct hb_static_array_t {
56 unsigned int allocated;
58 Type static_array[StaticSize];
60 void finish (void) { for (unsigned i = 0; i < len; i++) array[i].finish (); }
62 inline Type& operator [] (unsigned int i)
67 inline Type *push (void)
71 allocated = ARRAY_LENGTH (static_array);
73 if (likely (len < allocated))
75 /* Need to reallocate */
76 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
78 if (array == static_array) {
79 new_array = (Type *) calloc (new_allocated, sizeof (Type));
81 memcpy (new_array, array, len * sizeof (Type));
85 bool overflows = new_allocated >= ((unsigned int) -1) / sizeof (Type);
86 if (unlikely (overflows))
89 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
95 if ((len < allocated))
101 inline void pop (void)
104 /* TODO: shrink array if needed */
108 template <typename Type>
109 struct hb_array_t : hb_static_array_t<Type, 2> {};
112 template <typename Key, typename Value>
117 /* unsigned int hash; */
120 void finish (void) { value.finish (); }
123 hb_array_t <item_t> items;
127 inline item_t *find (Key key) {
128 if (unlikely (!key)) return NULL;
129 for (unsigned int i = 0; i < items.len; i++)
130 if (key == items[i].key)
137 inline bool set (Key key,
140 if (unlikely (!key)) return NULL;
146 item = items.push ();
147 if (unlikely (!item)) return false;
153 inline void unset (Key &key)
160 items[items.len - 1] = *item;
164 inline Value *get (Key key)
166 item_t *item = find (key);
167 return item ? &item->value : NULL;
170 void finish (void) { items.finish (); }
178 hb_destroy_func_t destroy;
180 void finish (void) { if (destroy) destroy (data); }
183 struct hb_user_data_array_t {
185 hb_map_t<hb_user_data_key_t *, hb_user_data_t> map;
187 inline bool set (hb_user_data_key_t *key,
189 hb_destroy_func_t destroy)
191 if (!data && !destroy) {
195 hb_user_data_t user_data = {data, destroy};
196 return map.set (key, user_data);
199 inline void *get (hb_user_data_key_t *key) {
200 return map.get (key);
203 void finish (void) { map.finish (); }
208 typedef struct _hb_object_header_t hb_object_header_t;
210 struct _hb_object_header_t {
211 hb_reference_count_t ref_count;
212 hb_user_data_array_t user_data;
214 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
216 static inline void *create (unsigned int size) {
217 hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
225 inline void init (void) {
229 inline bool is_inert (void) const {
230 return unlikely (ref_count.is_invalid ());
233 inline void reference (void) {
234 if (unlikely (!this || this->is_inert ()))
239 inline bool destroy (void) {
240 if (unlikely (!this || this->is_inert ()))
242 if (ref_count.dec () != 1)
250 inline bool set_user_data (hb_user_data_key_t *key,
252 hb_destroy_func_t destroy) {
253 if (unlikely (!this || this->is_inert ()))
256 return user_data.set (key, data, destroy);
259 inline void *get_user_data (hb_user_data_key_t *key) {
260 return user_data.get (key);
263 inline void trace (const char *function) const {
264 (void) (HB_DEBUG_OBJECT &&
265 fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
267 this ? ref_count.get () : 0,
276 template <typename Type>
277 static inline void hb_object_trace (const Type *obj, const char *function)
279 obj->header.trace (function);
281 template <typename Type>
282 static inline Type *hb_object_create ()
284 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
285 hb_object_trace (obj, HB_FUNC);
288 template <typename Type>
289 static inline bool hb_object_is_inert (const Type *obj)
291 return unlikely (obj->header.is_inert());
293 template <typename Type>
294 static inline Type *hb_object_reference (Type *obj)
296 hb_object_trace (obj, HB_FUNC);
297 obj->header.reference ();
300 template <typename Type>
301 static inline bool hb_object_destroy (Type *obj)
303 hb_object_trace (obj, HB_FUNC);
304 return obj->header.destroy ();
313 #endif /* HB_OBJECT_PRIVATE_HH */