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)
47 typedef struct _hb_object_header_t hb_object_header_t;
49 struct _hb_object_header_t {
50 hb_reference_count_t ref_count;
52 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
54 static inline void *create (unsigned int size) {
55 hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
63 inline void init (void) {
67 inline bool is_inert (void) const { return unlikely (ref_count.is_invalid ()); }
69 inline void reference (void) {
70 if (unlikely (!this || this->is_inert ()))
75 inline bool destroy (void) {
76 if (unlikely (!this || this->is_inert ()))
78 return ref_count.dec () == 1;
81 inline void trace (const char *function) const {
82 (void) (HB_DEBUG_OBJECT &&
83 fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
85 this ? ref_count.get () : 0,
94 template <typename Type>
95 static inline void hb_object_trace (const Type *obj, const char *function)
97 obj->header.trace (function);
99 template <typename Type>
100 static inline Type *hb_object_create ()
102 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
103 hb_object_trace (obj, HB_FUNC);
106 template <typename Type>
107 static inline bool hb_object_is_inert (const Type *obj)
109 return unlikely (obj->header.is_inert());
111 template <typename Type>
112 static inline Type *hb_object_reference (Type *obj)
114 hb_object_trace (obj, HB_FUNC);
115 obj->header.reference ();
118 template <typename Type>
119 static inline bool hb_object_destroy (Type *obj)
121 hb_object_trace (obj, HB_FUNC);
122 return obj->header.destroy ();
131 #endif /* HB_OBJECT_PRIVATE_HH */