OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2007 Chris Wilson |
| 3 * Copyright (C) 2009,2010 Red Hat, Inc. |
| 4 * |
| 5 * This is part of HarfBuzz, a text shaping library. |
| 6 * |
| 7 * Permission is hereby granted, without written agreement and without |
| 8 * license or royalty fees, to use, copy, modify, and distribute this |
| 9 * software and its documentation for any purpose, provided that the |
| 10 * above copyright notice and the following two paragraphs appear in |
| 11 * all copies of this software. |
| 12 * |
| 13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 * DAMAGE. |
| 18 * |
| 19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 * |
| 25 * Contributor(s): |
| 26 * Chris Wilson <chris@chris-wilson.co.uk> |
| 27 * Red Hat Author(s): Behdad Esfahbod |
| 28 */ |
| 29 |
| 30 #ifndef HB_OBJECT_PRIVATE_H |
| 31 #define HB_OBJECT_PRIVATE_H |
| 32 |
| 33 #include "hb-private.h" |
| 34 |
| 35 HB_BEGIN_DECLS |
| 36 |
| 37 |
| 38 /* Encapsulate operations on the object's reference count */ |
| 39 typedef struct { |
| 40 hb_atomic_int_t ref_count; |
| 41 } hb_reference_count_t; |
| 42 |
| 43 #define hb_reference_count_inc(RC) hb_atomic_int_fetch_and_add ((RC).ref_count,
1) |
| 44 #define hb_reference_count_dec(RC) hb_atomic_int_fetch_and_add ((RC).ref_count,
-1) |
| 45 |
| 46 #define HB_REFERENCE_COUNT_INIT(RC, VALUE) ((RC).ref_count = (VALUE)) |
| 47 |
| 48 #define HB_REFERENCE_COUNT_GET_VALUE(RC) hb_atomic_int_get ((RC).ref_count) |
| 49 #define HB_REFERENCE_COUNT_SET_VALUE(RC, VALUE) hb_atomic_int_set ((RC).ref_coun
t, (VALUE)) |
| 50 |
| 51 #define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1) |
| 52 #define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE} |
| 53 |
| 54 #define HB_REFERENCE_COUNT_IS_INVALID(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC) ==
HB_REFERENCE_COUNT_INVALID_VALUE) |
| 55 |
| 56 #define HB_REFERENCE_COUNT_HAS_REFERENCE(RC) (HB_REFERENCE_COUNT_GET_VALUE (RC)
> 0) |
| 57 |
| 58 |
| 59 |
| 60 /* Debug */ |
| 61 |
| 62 #ifndef HB_DEBUG_OBJECT |
| 63 #define HB_DEBUG_OBJECT (HB_DEBUG+0) |
| 64 #endif |
| 65 |
| 66 static inline void |
| 67 _hb_trace_object (const void *obj, |
| 68 hb_reference_count_t *ref_count, |
| 69 const char *function) |
| 70 { |
| 71 (void) (HB_DEBUG_OBJECT && |
| 72 fprintf (stderr, "OBJECT(%p) refcount=%d %s\n", |
| 73 obj, |
| 74 HB_REFERENCE_COUNT_GET_VALUE (*ref_count), |
| 75 function)); |
| 76 } |
| 77 |
| 78 #define TRACE_OBJECT(obj) _hb_trace_object (obj, &obj->ref_count, __FUNCTION__) |
| 79 |
| 80 |
| 81 |
| 82 /* Object allocation and lifecycle manamgement macros */ |
| 83 |
| 84 #define HB_OBJECT_IS_INERT(obj) \ |
| 85 (unlikely (HB_REFERENCE_COUNT_IS_INVALID ((obj)->ref_count))) |
| 86 |
| 87 #define HB_OBJECT_DO_INIT_EXPR(obj) \ |
| 88 HB_REFERENCE_COUNT_INIT (obj->ref_count, 1) |
| 89 |
| 90 #define HB_OBJECT_DO_INIT(obj) \ |
| 91 HB_STMT_START { \ |
| 92 HB_OBJECT_DO_INIT_EXPR (obj); \ |
| 93 } HB_STMT_END |
| 94 |
| 95 #define HB_OBJECT_DO_CREATE(Type, obj) \ |
| 96 likely (( \ |
| 97 (void) ( \ |
| 98 ((obj) = (Type *) calloc (1, sizeof (Type))) && \ |
| 99 ( \ |
| 100 HB_OBJECT_DO_INIT_EXPR (obj), \ |
| 101 TRACE_OBJECT (obj), \ |
| 102 TRUE \ |
| 103 ) \ |
| 104 ), \ |
| 105 (obj) \ |
| 106 )) |
| 107 |
| 108 #define HB_OBJECT_DO_REFERENCE(obj) \ |
| 109 HB_STMT_START { \ |
| 110 int old_count; \ |
| 111 if (unlikely (!(obj) || HB_OBJECT_IS_INERT (obj))) \ |
| 112 return obj; \ |
| 113 TRACE_OBJECT (obj); \ |
| 114 old_count = hb_reference_count_inc (obj->ref_count); \ |
| 115 assert (old_count > 0); \ |
| 116 return obj; \ |
| 117 } HB_STMT_END |
| 118 |
| 119 #define HB_OBJECT_DO_GET_REFERENCE_COUNT(obj) \ |
| 120 HB_STMT_START { \ |
| 121 if (unlikely (!(obj) || HB_OBJECT_IS_INERT (obj))) \ |
| 122 return 0; \ |
| 123 return HB_REFERENCE_COUNT_GET_VALUE (obj->ref_count); \ |
| 124 } HB_STMT_END |
| 125 |
| 126 #define HB_OBJECT_DO_DESTROY(obj) \ |
| 127 HB_STMT_START { \ |
| 128 int old_count; \ |
| 129 if (unlikely (!(obj) || HB_OBJECT_IS_INERT (obj))) \ |
| 130 return; \ |
| 131 TRACE_OBJECT (obj); \ |
| 132 old_count = hb_reference_count_dec (obj->ref_count); \ |
| 133 assert (old_count > 0); \ |
| 134 if (old_count != 1) \ |
| 135 return; \ |
| 136 } HB_STMT_END |
| 137 |
| 138 |
| 139 HB_END_DECLS |
| 140 |
| 141 #endif /* HB_OBJECT_PRIVATE_H */ |
OLD | NEW |