hb-object-private.hh revision 47e71d9661946a4ffb96026bf1d697d788414ab5
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 94template <typename Type> 95static inline void hb_object_trace (const Type *obj, const char *function) 96{ 97 obj->header.trace (function); 98} 99template <typename Type> 100static inline Type *hb_object_create () 101{ 102 Type *obj = (Type *) hb_object_header_t::create (sizeof (Type)); 103 hb_object_trace (obj, HB_FUNC); 104 return obj; 105} 106template <typename Type> 107static inline bool hb_object_is_inert (const Type *obj) 108{ 109 return unlikely (obj->header.is_inert()); 110} 111template <typename Type> 112static inline Type *hb_object_reference (Type *obj) 113{ 114 hb_object_trace (obj, HB_FUNC); 115 obj->header.reference (); 116 return obj; 117} 118template <typename Type> 119static inline bool hb_object_destroy (Type *obj) 120{ 121 hb_object_trace (obj, HB_FUNC); 122 return obj->header.destroy (); 123} 124 125 126HB_BEGIN_DECLS 127 128 129HB_END_DECLS 130 131#endif /* HB_OBJECT_PRIVATE_HH */ 132