< prev index next >

src/java.desktop/share/native/libfontmanager/harfbuzz/hb-object.hh

Print this page




  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 #include "hb-debug.hh"


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



  41 
  42 /* reference_count */
































































































  43 
  44 #define HB_REFERENCE_COUNT_INERT_VALUE 0
  45 #define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD
  46 #define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_INERT_VALUE)}
  47 
  48 struct hb_reference_count_t
  49 {
  50   hb_atomic_int_t ref_count;
  51 
  52   inline void init (int v) { ref_count.set_unsafe (v); }
  53   inline int get_unsafe (void) const { return ref_count.get_unsafe (); }
  54   inline int inc (void) { return ref_count.inc (); }
  55   inline int dec (void) { return ref_count.dec (); }
  56   inline void fini (void) { ref_count.set_unsafe (HB_REFERENCE_COUNT_POISON_VALUE); }
  57 
  58   inline bool is_inert (void) const { return ref_count.get_unsafe () == HB_REFERENCE_COUNT_INERT_VALUE; }
  59   inline bool is_valid (void) const { return ref_count.get_unsafe () > 0; }
  60 };
  61 
  62 
  63 /* user_data */
  64 
  65 struct hb_user_data_array_t
  66 {
  67   struct hb_user_data_item_t {
  68     hb_user_data_key_t *key;
  69     void *data;
  70     hb_destroy_func_t destroy;
  71 
  72     inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
  73     inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
  74 
  75     void fini (void) { if (destroy) destroy (data); }
  76   };
  77 
  78   hb_mutex_t lock;
  79   hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
  80 
  81   inline void init (void) { lock.init (); items.init (); }
  82 
  83   HB_INTERNAL bool set (hb_user_data_key_t *key,
  84                         void *              data,
  85                         hb_destroy_func_t   destroy,
  86                         hb_bool_t           replace);
  87 
  88   HB_INTERNAL void *get (hb_user_data_key_t *key);
  89 
  90   inline void fini (void) { items.fini (lock); lock.fini (); }
  91 };
  92 
  93 
  94 /* object_header */


  95 
  96 struct hb_object_header_t
  97 {
  98   hb_reference_count_t ref_count;
  99   hb_user_data_array_t *user_data;
 100 
 101 #define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INIT, nullptr}
 102 
 103   private:
 104   ASSERT_POD ();
 105 };






 106 
 107 
 108 /* object */


 109 
 110 template <typename Type>
 111 static inline void hb_object_trace (const Type *obj, const char *function)
 112 {
 113   DEBUG_MSG (OBJECT, (void *) obj,
 114              "%s refcount=%d",
 115              function,
 116              obj ? obj->header.ref_count.get_unsafe () : 0);
 117 }
 118 
 119 template <typename Type>
 120 static inline Type *hb_object_create (void)
 121 {
 122   Type *obj = (Type *) calloc (1, sizeof (Type));
 123 
 124   if (unlikely (!obj))
 125     return obj;
 126 
 127   hb_object_init (obj);
 128   hb_object_trace (obj, HB_FUNC);
 129   return obj;
 130 }
 131 template <typename Type>
 132 static inline void hb_object_init (Type *obj)
 133 {
 134   obj->header.ref_count.init (1);
 135   obj->header.user_data = nullptr;

 136 }
 137 template <typename Type>
 138 static inline bool hb_object_is_inert (const Type *obj)
 139 {
 140   return unlikely (obj->header.ref_count.is_inert ());
 141 }
 142 template <typename Type>
 143 static inline bool hb_object_is_valid (const Type *obj)
 144 {
 145   return likely (obj->header.ref_count.is_valid ());
 146 }
 147 template <typename Type>










 148 static inline Type *hb_object_reference (Type *obj)
 149 {
 150   hb_object_trace (obj, HB_FUNC);
 151   if (unlikely (!obj || hb_object_is_inert (obj)))
 152     return obj;
 153   assert (hb_object_is_valid (obj));
 154   obj->header.ref_count.inc ();
 155   return obj;
 156 }
 157 template <typename Type>
 158 static inline bool hb_object_destroy (Type *obj)
 159 {
 160   hb_object_trace (obj, HB_FUNC);
 161   if (unlikely (!obj || hb_object_is_inert (obj)))
 162     return false;
 163   assert (hb_object_is_valid (obj));
 164   if (obj->header.ref_count.dec () != 1)
 165     return false;
 166 
 167   hb_object_fini (obj);
 168   return true;
 169 }
 170 template <typename Type>
 171 static inline void hb_object_fini (Type *obj)
 172 {
 173   obj->header.ref_count.fini (); /* Do this before user_data */
 174   if (obj->header.user_data)

 175   {
 176     obj->header.user_data->fini ();
 177     free (obj->header.user_data);

 178   }
 179 }
 180 template <typename Type>
 181 static inline bool hb_object_set_user_data (Type               *obj,
 182                                             hb_user_data_key_t *key,
 183                                             void *              data,
 184                                             hb_destroy_func_t   destroy,
 185                                             hb_bool_t           replace)
 186 {
 187   if (unlikely (!obj || hb_object_is_inert (obj)))
 188     return false;
 189   assert (hb_object_is_valid (obj));
 190 
 191 retry:
 192   hb_user_data_array_t *user_data = (hb_user_data_array_t *) hb_atomic_ptr_get (&obj->header.user_data);
 193   if (unlikely (!user_data))
 194   {
 195     user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1);
 196     if (unlikely (!user_data))
 197       return false;
 198     user_data->init ();
 199     if (unlikely (!hb_atomic_ptr_cmpexch (&obj->header.user_data, nullptr, user_data)))
 200     {
 201       user_data->fini ();
 202       free (user_data);
 203       goto retry;
 204     }
 205   }
 206 
 207   return user_data->set (key, data, destroy, replace);
 208 }
 209 
 210 template <typename Type>
 211 static inline void *hb_object_get_user_data (Type               *obj,
 212                                              hb_user_data_key_t *key)
 213 {
 214   if (unlikely (!obj || hb_object_is_inert (obj) || !obj->header.user_data))
 215     return nullptr;
 216   assert (hb_object_is_valid (obj));
 217   return obj->header.user_data->get (key);



 218 }
 219 
 220 
 221 #endif /* HB_OBJECT_PRIVATE_HH */


  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_HH
  33 #define HB_OBJECT_HH
  34 
  35 #include "hb.hh"
  36 #include "hb-atomic.hh"
  37 #include "hb-mutex.hh"
  38 #include "hb-vector.hh"
  39 


  40 
  41 /*
  42  * Lockable set
  43  */
  44 
  45 template <typename item_t, typename lock_t>
  46 struct hb_lockable_set_t
  47 {
  48   hb_vector_t<item_t> items;
  49 
  50   void init () { items.init (); }
  51 
  52   template <typename T>
  53   item_t *replace_or_insert (T v, lock_t &l, bool replace)
  54   {
  55     l.lock ();
  56     item_t *item = items.find (v);
  57     if (item) {
  58       if (replace) {
  59         item_t old = *item;
  60         *item = v;
  61         l.unlock ();
  62         old.fini ();
  63       }
  64       else {
  65         item = nullptr;
  66         l.unlock ();
  67       }
  68     } else {
  69       item = items.push (v);
  70       l.unlock ();
  71     }
  72     return item;
  73   }
  74 
  75   template <typename T>
  76   void remove (T v, lock_t &l)
  77   {
  78     l.lock ();
  79     item_t *item = items.find (v);
  80     if (item)
  81     {
  82       item_t old = *item;
  83       *item = items[items.length - 1];
  84       items.pop ();
  85       l.unlock ();
  86       old.fini ();
  87     } else {
  88       l.unlock ();
  89     }
  90   }
  91 
  92   template <typename T>
  93   bool find (T v, item_t *i, lock_t &l)
  94   {
  95     l.lock ();
  96     item_t *item = items.find (v);
  97     if (item)
  98       *i = *item;
  99     l.unlock ();
 100     return !!item;
 101   }
 102 
 103   template <typename T>
 104   item_t *find_or_insert (T v, lock_t &l)
 105   {
 106     l.lock ();
 107     item_t *item = items.find (v);
 108     if (!item) {
 109       item = items.push (v);
 110     }
 111     l.unlock ();
 112     return item;
 113   }
 114 
 115   void fini (lock_t &l)
 116   {
 117     if (!items.length)
 118     {
 119       /* No need to lock. */
 120       items.fini ();
 121       return;
 122     }
 123     l.lock ();
 124     while (items.length)
 125     {
 126       item_t old = items[items.length - 1];
 127       items.pop ();
 128       l.unlock ();
 129       old.fini ();
 130       l.lock ();
 131     }
 132     items.fini ();
 133     l.unlock ();
 134   }
 135 
 136 };
 137 
 138 
 139 /*
 140  * Reference-count.
 141  */
 142 
 143 #define HB_REFERENCE_COUNT_INERT_VALUE 0
 144 #define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD
 145 #define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_INERT_VALUE)}
 146 
 147 struct hb_reference_count_t
 148 {
 149   mutable hb_atomic_int_t ref_count;
 150 
 151   void init (int v = 1) { ref_count.set_relaxed (v); }
 152   int get_relaxed () const { return ref_count.get_relaxed (); }
 153   int inc () const { return ref_count.inc (); }
 154   int dec () const { return ref_count.dec (); }
 155   void fini () { ref_count.set_relaxed (HB_REFERENCE_COUNT_POISON_VALUE); }
 156 
 157   bool is_inert () const { return ref_count.get_relaxed () == HB_REFERENCE_COUNT_INERT_VALUE; }
 158   bool is_valid () const { return ref_count.get_relaxed () > 0; }
 159 };
 160 
 161 
 162 /* user_data */
 163 
 164 struct hb_user_data_array_t
 165 {
 166   struct hb_user_data_item_t {
 167     hb_user_data_key_t *key;
 168     void *data;
 169     hb_destroy_func_t destroy;
 170 
 171     bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
 172     bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
 173 
 174     void fini () { if (destroy) destroy (data); }
 175   };
 176 
 177   hb_mutex_t lock;
 178   hb_lockable_set_t<hb_user_data_item_t, hb_mutex_t> items;
 179 
 180   void init () { lock.init (); items.init (); }
 181 
 182   HB_INTERNAL bool set (hb_user_data_key_t *key,
 183                         void *              data,
 184                         hb_destroy_func_t   destroy,
 185                         hb_bool_t           replace);
 186 
 187   HB_INTERNAL void *get (hb_user_data_key_t *key);
 188 
 189   void fini () { items.fini (lock); lock.fini (); }
 190 };
 191 
 192 
 193 /*
 194  * Object header
 195  */
 196 
 197 struct hb_object_header_t
 198 {
 199   hb_reference_count_t ref_count;
 200   mutable hb_atomic_int_t writable;
 201   hb_atomic_ptr_t<hb_user_data_array_t> user_data;




 202 };
 203 #define HB_OBJECT_HEADER_STATIC \
 204         { \
 205           HB_REFERENCE_COUNT_INIT, \
 206           HB_ATOMIC_INT_INIT (false), \
 207           HB_ATOMIC_PTR_INIT (nullptr) \
 208         }
 209 
 210 
 211 /*
 212  * Object
 213  */
 214 
 215 template <typename Type>
 216 static inline void hb_object_trace (const Type *obj, const char *function)
 217 {
 218   DEBUG_MSG (OBJECT, (void *) obj,
 219              "%s refcount=%d",
 220              function,
 221              obj ? obj->header.ref_count.get_relaxed () : 0);
 222 }
 223 
 224 template <typename Type>
 225 static inline Type *hb_object_create ()
 226 {
 227   Type *obj = (Type *) calloc (1, sizeof (Type));
 228 
 229   if (unlikely (!obj))
 230     return obj;
 231 
 232   hb_object_init (obj);
 233   hb_object_trace (obj, HB_FUNC);
 234   return obj;
 235 }
 236 template <typename Type>
 237 static inline void hb_object_init (Type *obj)
 238 {
 239   obj->header.ref_count.init ();
 240   obj->header.writable.set_relaxed (true);
 241   obj->header.user_data.init ();
 242 }
 243 template <typename Type>
 244 static inline bool hb_object_is_inert (const Type *obj)
 245 {
 246   return unlikely (obj->header.ref_count.is_inert ());
 247 }
 248 template <typename Type>
 249 static inline bool hb_object_is_valid (const Type *obj)
 250 {
 251   return likely (obj->header.ref_count.is_valid ());
 252 }
 253 template <typename Type>
 254 static inline bool hb_object_is_immutable (const Type *obj)
 255 {
 256   return !obj->header.writable.get_relaxed ();
 257 }
 258 template <typename Type>
 259 static inline void hb_object_make_immutable (const Type *obj)
 260 {
 261   obj->header.writable.set_relaxed (false);
 262 }
 263 template <typename Type>
 264 static inline Type *hb_object_reference (Type *obj)
 265 {
 266   hb_object_trace (obj, HB_FUNC);
 267   if (unlikely (!obj || hb_object_is_inert (obj)))
 268     return obj;
 269   assert (hb_object_is_valid (obj));
 270   obj->header.ref_count.inc ();
 271   return obj;
 272 }
 273 template <typename Type>
 274 static inline bool hb_object_destroy (Type *obj)
 275 {
 276   hb_object_trace (obj, HB_FUNC);
 277   if (unlikely (!obj || hb_object_is_inert (obj)))
 278     return false;
 279   assert (hb_object_is_valid (obj));
 280   if (obj->header.ref_count.dec () != 1)
 281     return false;
 282 
 283   hb_object_fini (obj);
 284   return true;
 285 }
 286 template <typename Type>
 287 static inline void hb_object_fini (Type *obj)
 288 {
 289   obj->header.ref_count.fini (); /* Do this before user_data */
 290   hb_user_data_array_t *user_data = obj->header.user_data.get ();
 291   if (user_data)
 292   {
 293     user_data->fini ();
 294     free (user_data);
 295     user_data = nullptr;
 296   }
 297 }
 298 template <typename Type>
 299 static inline bool hb_object_set_user_data (Type               *obj,
 300                                             hb_user_data_key_t *key,
 301                                             void *              data,
 302                                             hb_destroy_func_t   destroy,
 303                                             hb_bool_t           replace)
 304 {
 305   if (unlikely (!obj || hb_object_is_inert (obj)))
 306     return false;
 307   assert (hb_object_is_valid (obj));
 308 
 309 retry:
 310   hb_user_data_array_t *user_data = obj->header.user_data.get ();
 311   if (unlikely (!user_data))
 312   {
 313     user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1);
 314     if (unlikely (!user_data))
 315       return false;
 316     user_data->init ();
 317     if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data)))
 318     {
 319       user_data->fini ();
 320       free (user_data);
 321       goto retry;
 322     }
 323   }
 324 
 325   return user_data->set (key, data, destroy, replace);
 326 }
 327 
 328 template <typename Type>
 329 static inline void *hb_object_get_user_data (Type               *obj,
 330                                              hb_user_data_key_t *key)
 331 {
 332   if (unlikely (!obj || hb_object_is_inert (obj)))
 333     return nullptr;
 334   assert (hb_object_is_valid (obj));
 335   hb_user_data_array_t *user_data = obj->header.user_data.get ();
 336   if (!user_data)
 337     return nullptr;
 338   return user_data->get (key);
 339 }
 340 
 341 
 342 #endif /* HB_OBJECT_HH */
< prev index next >