hb-object-private.hh revision ec6f9c2fd03a49d1e91cbaefa5bdbbfb35dff92e
1/*
2 * Copyright © 2007  Chris Wilson
3 * Copyright © 2009,2010  Red Hat, Inc.
4 * Copyright © 2011  Google, Inc.
5 *
6 *  This is part of HarfBuzz, a text shaping library.
7 *
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.
13 *
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.
19 *
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.
25 *
26 * Contributor(s):
27 *	Chris Wilson <chris@chris-wilson.co.uk>
28 * Red Hat Author(s): Behdad Esfahbod
29 * Google Author(s): Behdad Esfahbod
30 */
31
32#ifndef HB_OBJECT_PRIVATE_HH
33#define HB_OBJECT_PRIVATE_HH
34
35#include "hb-private.hh"
36
37HB_BEGIN_DECLS
38
39
40/* Debug */
41
42#ifndef HB_DEBUG_OBJECT
43#define HB_DEBUG_OBJECT (HB_DEBUG+0)
44#endif
45
46
47typedef struct _hb_object_header_t hb_object_header_t;
48
49struct _hb_object_header_t {
50  hb_reference_count_t ref_count;
51
52#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID}
53
54  static inline void *create (unsigned int size) {
55    hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
56
57    if (likely (obj))
58      obj->init ();
59
60    return obj;
61  }
62
63  inline void init (void) {
64    ref_count.init (1);
65  }
66
67  inline bool is_inert (void) const { return unlikely (ref_count.is_invalid ()); }
68
69  inline void reference (void) {
70    if (unlikely (!this || this->is_inert ()))
71      return;
72    ref_count.inc ();
73  }
74
75  inline bool destroy (void) {
76    if (unlikely (!this || this->is_inert ()))
77      return false;
78    return ref_count.dec () == 1;
79  }
80
81  inline void trace (const char *function) const {
82    (void) (HB_DEBUG_OBJECT &&
83	    fprintf (stderr, "OBJECT(%p) refcount=%d %s\n",
84		     this,
85		     this ? ref_count.get () : 0,
86		     function));
87  }
88
89};
90
91
92HB_END_DECLS
93
94
95template <typename Type>
96static inline Type *hb_object_create () { return (Type *) hb_object_header_t::create (sizeof (Type)); }
97
98template <typename Type>
99static inline bool hb_object_is_inert (const Type *obj) { return unlikely (obj->header.is_inert()); }
100
101template <typename Type>
102static inline Type *hb_object_reference (Type *obj) { obj->header.reference (); return obj; }
103
104template <typename Type>
105static inline bool hb_object_destroy (Type *obj) { return obj->header.destroy (); }
106
107template <typename Type>
108static inline void hb_object_trace (const Type *obj) { obj->header.trace (__FUNCTION__); }
109
110
111HB_BEGIN_DECLS
112
113
114/* Object allocation and lifecycle manamgement macros */
115
116#define HB_TRACE_OBJECT(obj) hb_object_trace (obj)
117#define HB_OBJECT_DO_CREATE(Type, obj) likely (obj = hb_object_create<Type> ())
118#define HB_OBJECT_IS_INERT(obj) hb_object_is_inert (obj)
119#define HB_OBJECT_DO_REFERENCE(obj) return hb_object_reference (obj)
120#define HB_OBJECT_DO_DESTROY(obj) if (!hb_object_destroy (obj)) return
121
122
123HB_END_DECLS
124
125#endif /* HB_OBJECT_PRIVATE_HH */
126