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,
95 template <typename Type>
96 static inline Type *hb_object_create () { return (Type *) hb_object_header_t::create (sizeof (Type)); }
98 template <typename Type>
99 static inline bool hb_object_is_inert (const Type *obj) { return unlikely (obj->header.is_inert()); }
101 template <typename Type>
102 static inline Type *hb_object_reference (Type *obj) { obj->header.reference (); return obj; }
104 template <typename Type>
105 static inline bool hb_object_destroy (Type *obj) { return obj->header.destroy (); }
107 template <typename Type>
108 static inline void hb_object_trace (const Type *obj) { obj->header.trace (__FUNCTION__); }
114 /* Object allocation and lifecycle manamgement macros */
116 /* XXX Trace objects. Got removed in refactoring */
117 #define HB_TRACE_OBJECT(obj) hb_object_trace (obj)
118 #define HB_OBJECT_DO_CREATE(Type, obj) likely (obj = hb_object_create<Type> ())
119 #define HB_OBJECT_IS_INERT(obj) hb_object_is_inert (obj)
120 #define HB_OBJECT_DO_REFERENCE(obj) return hb_object_reference (obj)
121 #define HB_OBJECT_DO_DESTROY(obj) if (!hb_object_destroy (obj)) return
126 #endif /* HB_OBJECT_PRIVATE_HH */