Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / src / hb-object.hh
index c470532..4b5bc32 100644 (file)
@@ -53,7 +53,7 @@ struct hb_lockable_set_t
   item_t *replace_or_insert (T v, lock_t &l, bool replace)
   {
     l.lock ();
-    item_t *item = items.find (v);
+    item_t *item = items.lsearch (v);
     if (item) {
       if (replace) {
        item_t old = *item;
@@ -76,7 +76,7 @@ struct hb_lockable_set_t
   void remove (T v, lock_t &l)
   {
     l.lock ();
-    item_t *item = items.find (v);
+    item_t *item = items.lsearch (v);
     if (item)
     {
       item_t old = *item;
@@ -93,7 +93,7 @@ struct hb_lockable_set_t
   bool find (T v, item_t *i, lock_t &l)
   {
     l.lock ();
-    item_t *item = items.find (v);
+    item_t *item = items.lsearch (v);
     if (item)
       *i = *item;
     l.unlock ();
@@ -140,10 +140,6 @@ struct hb_lockable_set_t
  * Reference-count.
  */
 
-#define HB_REFERENCE_COUNT_INERT_VALUE 0
-#define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD
-#define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_INERT_VALUE)}
-
 struct hb_reference_count_t
 {
   mutable hb_atomic_int_t ref_count;
@@ -152,9 +148,9 @@ struct hb_reference_count_t
   int get_relaxed () const { return ref_count.get_relaxed (); }
   int inc () const { return ref_count.inc (); }
   int dec () const { return ref_count.dec (); }
-  void fini () { ref_count.set_relaxed (HB_REFERENCE_COUNT_POISON_VALUE); }
+  void fini () { ref_count.set_relaxed (-0x0000DEAD); }
 
-  bool is_inert () const { return ref_count.get_relaxed () == HB_REFERENCE_COUNT_INERT_VALUE; }
+  bool is_inert () const { return !ref_count.get_relaxed (); }
   bool is_valid () const { return ref_count.get_relaxed () > 0; }
 };
 
@@ -168,8 +164,8 @@ struct hb_user_data_array_t
     void *data;
     hb_destroy_func_t destroy;
 
-    bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; }
-    bool operator == (hb_user_data_item_t &other) const { return key == other.key; }
+    bool operator == (const hb_user_data_key_t *other_key) const { return key == other_key; }
+    bool operator == (const hb_user_data_item_t &other) const { return key == other.key; }
 
     void fini () { if (destroy) destroy (data); }
   };
@@ -197,15 +193,12 @@ struct hb_user_data_array_t
 struct hb_object_header_t
 {
   hb_reference_count_t ref_count;
-  mutable hb_atomic_int_t writable;
+  mutable hb_atomic_int_t writable = 0;
   hb_atomic_ptr_t<hb_user_data_array_t> user_data;
+
+  bool is_inert () const { return !ref_count.get_relaxed (); }
 };
-#define HB_OBJECT_HEADER_STATIC \
-       { \
-         HB_REFERENCE_COUNT_INIT, \
-         HB_ATOMIC_INT_INIT (false), \
-         HB_ATOMIC_PTR_INIT (nullptr) \
-       }
+#define HB_OBJECT_HEADER_STATIC {}
 
 
 /*
@@ -224,7 +217,7 @@ static inline void hb_object_trace (const Type *obj, const char *function)
 template <typename Type>
 static inline Type *hb_object_create ()
 {
-  Type *obj = (Type *) calloc (1, sizeof (Type));
+  Type *obj = (Type *) hb_calloc (1, sizeof (Type));
 
   if (unlikely (!obj))
     return obj;
@@ -241,11 +234,6 @@ static inline void hb_object_init (Type *obj)
   obj->header.user_data.init ();
 }
 template <typename Type>
-static inline bool hb_object_is_inert (const Type *obj)
-{
-  return unlikely (obj->header.ref_count.is_inert ());
-}
-template <typename Type>
 static inline bool hb_object_is_valid (const Type *obj)
 {
   return likely (obj->header.ref_count.is_valid ());
@@ -264,7 +252,7 @@ template <typename Type>
 static inline Type *hb_object_reference (Type *obj)
 {
   hb_object_trace (obj, HB_FUNC);
-  if (unlikely (!obj || hb_object_is_inert (obj)))
+  if (unlikely (!obj || obj->header.is_inert ()))
     return obj;
   assert (hb_object_is_valid (obj));
   obj->header.ref_count.inc ();
@@ -274,7 +262,7 @@ template <typename Type>
 static inline bool hb_object_destroy (Type *obj)
 {
   hb_object_trace (obj, HB_FUNC);
-  if (unlikely (!obj || hb_object_is_inert (obj)))
+  if (unlikely (!obj || obj->header.is_inert ()))
     return false;
   assert (hb_object_is_valid (obj));
   if (obj->header.ref_count.dec () != 1)
@@ -291,7 +279,7 @@ static inline void hb_object_fini (Type *obj)
   if (user_data)
   {
     user_data->fini ();
-    free (user_data);
+    hb_free (user_data);
     user_data = nullptr;
   }
 }
@@ -302,7 +290,7 @@ static inline bool hb_object_set_user_data (Type               *obj,
                                            hb_destroy_func_t   destroy,
                                            hb_bool_t           replace)
 {
-  if (unlikely (!obj || hb_object_is_inert (obj)))
+  if (unlikely (!obj || obj->header.is_inert ()))
     return false;
   assert (hb_object_is_valid (obj));
 
@@ -310,14 +298,14 @@ retry:
   hb_user_data_array_t *user_data = obj->header.user_data.get ();
   if (unlikely (!user_data))
   {
-    user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1);
+    user_data = (hb_user_data_array_t *) hb_calloc (sizeof (hb_user_data_array_t), 1);
     if (unlikely (!user_data))
       return false;
     user_data->init ();
     if (unlikely (!obj->header.user_data.cmpexch (nullptr, user_data)))
     {
       user_data->fini ();
-      free (user_data);
+      hb_free (user_data);
       goto retry;
     }
   }
@@ -329,7 +317,7 @@ template <typename Type>
 static inline void *hb_object_get_user_data (Type               *obj,
                                             hb_user_data_key_t *key)
 {
-  if (unlikely (!obj || hb_object_is_inert (obj)))
+  if (unlikely (!obj || obj->header.is_inert ()))
     return nullptr;
   assert (hb_object_is_valid (obj));
   hb_user_data_array_t *user_data = obj->header.user_data.get ();