hb-object-private.hh 6.1 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2 3
 * Copyright © 2007  Chris Wilson
 * Copyright © 2009,2010  Red Hat, Inc.
4
 * Copyright © 2011,2012  Google, Inc.
B
Behdad Esfahbod 已提交
5
 *
B
Behdad Esfahbod 已提交
6
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Contributor(s):
 *	Chris Wilson <chris@chris-wilson.co.uk>
28
 * Red Hat Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
29
 * Google Author(s): Behdad Esfahbod
B
Behdad Esfahbod 已提交
30 31
 */

32 33
#ifndef HB_OBJECT_PRIVATE_HH
#define HB_OBJECT_PRIVATE_HH
B
Behdad Esfahbod 已提交
34

35
#include "hb-private.hh"
36

37
#include "hb-atomic-private.hh"
38 39
#include "hb-mutex-private.hh"

40

41 42 43
/* Debug */

#ifndef HB_DEBUG_OBJECT
44
#define HB_DEBUG_OBJECT (HB_DEBUG+0)
45 46 47
#endif


B
Behdad Esfahbod 已提交
48
/* reference_count */
49

B
Behdad Esfahbod 已提交
50 51
#define HB_REFERENCE_COUNT_INVALID_VALUE ((hb_atomic_int_t) -1)
#define HB_REFERENCE_COUNT_INVALID {HB_REFERENCE_COUNT_INVALID_VALUE}
B
Minor  
Behdad Esfahbod 已提交
52 53
struct hb_reference_count_t
{
54
  hb_atomic_int_t ref_count;
55

B
Behdad Esfahbod 已提交
56
  inline void init (int v) { ref_count = v; }
B
Behdad Esfahbod 已提交
57 58
  inline int inc (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count),  1); }
  inline int dec (void) { return hb_atomic_int_add (const_cast<hb_atomic_int_t &> (ref_count), -1); }
B
Behdad Esfahbod 已提交
59
  inline void finish (void) { ref_count = HB_REFERENCE_COUNT_INVALID_VALUE; }
60

61
  inline bool is_invalid (void) const { return ref_count == HB_REFERENCE_COUNT_INVALID_VALUE; }
62

B
Minor  
Behdad Esfahbod 已提交
63
};
64 65


B
Behdad Esfahbod 已提交
66
/* user_data */
67

B
Behdad Esfahbod 已提交
68
#define HB_USER_DATA_ARRAY_INIT {HB_LOCKABLE_SET_INIT}
B
Minor  
Behdad Esfahbod 已提交
69 70
struct hb_user_data_array_t
{
71 72
  /* TODO Add tracing. */

B
Behdad Esfahbod 已提交
73 74 75 76
  struct hb_user_data_item_t {
    hb_user_data_key_t *key;
    void *data;
    hb_destroy_func_t destroy;
77

B
Behdad Esfahbod 已提交
78 79
    inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
    inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
80

B
Behdad Esfahbod 已提交
81 82
    void finish (void) { if (destroy) destroy (data); }
  };
83

B
Behdad Esfahbod 已提交
84
  hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
85

86 87
  inline void init (void) { items.init (); }

B
Behdad Esfahbod 已提交
88 89
  HB_INTERNAL bool set (hb_user_data_key_t *key,
			void *              data,
90
			hb_destroy_func_t   destroy,
B
Behdad Esfahbod 已提交
91 92
			hb_bool_t           replace,
			hb_mutex_t         &lock);
93

B
Behdad Esfahbod 已提交
94 95
  HB_INTERNAL void *get (hb_user_data_key_t *key,
			hb_mutex_t          &lock);
96

B
Behdad Esfahbod 已提交
97
  HB_INTERNAL void finish (hb_mutex_t &lock);
98 99 100
};


B
Behdad Esfahbod 已提交
101
/* object_header */
102

B
Minor  
Behdad Esfahbod 已提交
103 104
struct hb_object_header_t
{
105
  hb_reference_count_t ref_count;
B
Behdad Esfahbod 已提交
106
  hb_mutex_t lock;
107
  hb_user_data_array_t user_data;
108

B
Behdad Esfahbod 已提交
109
#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INVALID, HB_MUTEX_INIT, HB_USER_DATA_ARRAY_INIT}
110

111 112
  static inline void *create (unsigned int size) {
    hb_object_header_t *obj = (hb_object_header_t *) calloc (1, size);
113

114 115
    if (likely (obj))
      obj->init ();
116

117 118
    return obj;
  }
B
Behdad Esfahbod 已提交
119

120 121
  inline void init (void) {
    ref_count.init (1);
B
Behdad Esfahbod 已提交
122
    lock.init ();
123
    user_data.init ();
124 125
  }

126 127 128
  inline bool is_inert (void) const {
    return unlikely (ref_count.is_invalid ());
  }
129 130 131 132 133 134 135 136 137 138

  inline void reference (void) {
    if (unlikely (!this || this->is_inert ()))
      return;
    ref_count.inc ();
  }

  inline bool destroy (void) {
    if (unlikely (!this || this->is_inert ()))
      return false;
139 140 141
    if (ref_count.dec () != 1)
      return false;

B
Behdad Esfahbod 已提交
142 143 144
    ref_count.finish (); /* Do this before user_data */
    user_data.finish (lock);
    lock.finish ();
145 146 147 148 149 150

    return true;
  }

  inline bool set_user_data (hb_user_data_key_t *key,
			     void *              data,
151 152
			     hb_destroy_func_t   destroy_func,
			     hb_bool_t           replace) {
153 154 155
    if (unlikely (!this || this->is_inert ()))
      return false;

B
Behdad Esfahbod 已提交
156
    return user_data.set (key, data, destroy_func, replace, lock);
157 158 159
  }

  inline void *get_user_data (hb_user_data_key_t *key) {
B
Behdad Esfahbod 已提交
160 161 162 163
    if (unlikely (!this || this->is_inert ()))
      return NULL;

    return user_data.get (key, lock);
164 165 166
  }

  inline void trace (const char *function) const {
B
Behdad Esfahbod 已提交
167
    if (unlikely (!this)) return;
B
Minor  
Behdad Esfahbod 已提交
168
    /* XXX We cannot use DEBUG_MSG_FUNC here since that one currecntly only
169
     * prints the class name and throws away the template info. */
170
    DEBUG_MSG (OBJECT, (void *) this,
B
Minor  
Behdad Esfahbod 已提交
171 172
	       "%s refcount=%d",
	       function,
B
Behdad Esfahbod 已提交
173
	       this ? ref_count.ref_count : 0);
174 175
  }

176 177
  private:
  ASSERT_POD ();
178 179 180
};


B
Behdad Esfahbod 已提交
181 182
/* object */

183
template <typename Type>
184 185 186 187
static inline void hb_object_trace (const Type *obj, const char *function)
{
  obj->header.trace (function);
}
188
template <typename Type>
189
static inline Type *hb_object_create (void)
190 191 192 193 194
{
  Type *obj = (Type *) hb_object_header_t::create (sizeof (Type));
  hb_object_trace (obj, HB_FUNC);
  return obj;
}
195
template <typename Type>
196 197
static inline bool hb_object_is_inert (const Type *obj)
{
198
  return unlikely (obj->header.is_inert ());
199
}
200
template <typename Type>
201 202 203 204 205 206
static inline Type *hb_object_reference (Type *obj)
{
  hb_object_trace (obj, HB_FUNC);
  obj->header.reference ();
  return obj;
}
207
template <typename Type>
208 209 210 211 212
static inline bool hb_object_destroy (Type *obj)
{
  hb_object_trace (obj, HB_FUNC);
  return obj->header.destroy ();
}
B
Behdad Esfahbod 已提交
213 214 215 216
template <typename Type>
static inline bool hb_object_set_user_data (Type               *obj,
					    hb_user_data_key_t *key,
					    void *              data,
217 218
					    hb_destroy_func_t   destroy,
					    hb_bool_t           replace)
B
Behdad Esfahbod 已提交
219
{
220
  return obj->header.set_user_data (key, data, destroy, replace);
B
Behdad Esfahbod 已提交
221 222 223 224 225 226 227 228
}

template <typename Type>
static inline void *hb_object_get_user_data (Type               *obj,
					     hb_user_data_key_t *key)
{
  return obj->header.get_user_data (key);
}
229

230

231
#endif /* HB_OBJECT_PRIVATE_HH */