mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-28 00:59:16 +01:00
Fix invalid reinterpret_cast (#5558)
* Fix invalid reinterpret_cast reinterpret_cast from non-atomic object to atomic object is not valid. https://eel.is/c++draft/atomics.types.generic#general-3 >[Note 2: The representation of an atomic specialization >need not have the same size and alignment requirement as >its corresponding argument type. — end note] https://eel.is/c++draft/basic.compound#5 >5 Two objects a and b are pointer-interconvertible if >(5.1) they are the same object, or >(5.2) one is a union object and the other is a non-static >data member of that object ([class.union]), or >(5.3) one is a standard-layout class object and the other >is the first non-static data member of that object or any >base class subobject of that object ([class.mem]), or >(5.4) there exists an object c such that a and c are >pointer-interconvertible, and c and b are pointer-interconvertible. >If two objects are pointer-interconvertible, then they have >the same address, and it is possible to obtain a pointer >to one from a pointer to the other via a reinterpret_cast >([expr.reinterpret.cast]). objects of types `int` and `std::atomic<int>` are not pointer-interconvertible (an object of type `int` is not first non-static data member of `std::atomic<int>`) https://eel.is/c++draft/basic.lval#11 >11 An object of dynamic type T-obj is type-accessible through >a glvalue of type T-ref if T-ref is similar ([conv.qual]) to: >(11.1) T-obj, >(11.2) a type that is the signed or unsigned type corresponding >to T-obj, or >(11.3) a char, unsigned char, or std::byte type. >If a program attempts to access ([defns.access]) the stored >value of an object through a glvalue through which it is not >type-accessible, the behavior is undefined The original code resulted in undefined behavior. * revert code formatting * fix const qualifier in type for atomic operation. * implement copy and move ctors (assignment-operators) and swap functions. * swap-functions replaced as frendly members, common header includes returned. * add HB_ prefix to defined macro. * some formatting.
This commit is contained in:
1 parent
43284d1f4d
commit
9d0045443d
1 file changed
+80
-20
+80
-20
@@ -40,7 +40,6 @@
|
||||
* Atomic integers and pointers.
|
||||
*/
|
||||
|
||||
|
||||
/* We need external help for these */
|
||||
|
||||
#if defined(hb_atomic_int_impl_add) \
|
||||
@@ -80,27 +79,11 @@ _hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#define HB_STL_ATOMIC_IMPL
|
||||
|
||||
#define _hb_memory_r_barrier() std::atomic_thread_fence(std::memory_order_acquire)
|
||||
#define _hb_memory_w_barrier() std::atomic_thread_fence(std::memory_order_release)
|
||||
|
||||
#define hb_atomic_int_impl_add(AI, V) (reinterpret_cast<std::atomic<typename std::decay<decltype (*(AI))>::type> *> (AI)->fetch_add ((V), std::memory_order_acq_rel))
|
||||
#define hb_atomic_int_impl_set_relaxed(AI, V) (reinterpret_cast<std::atomic<typename std::decay<decltype (*(AI))>::type> *> (AI)->store ((V), std::memory_order_relaxed))
|
||||
#define hb_atomic_int_impl_set(AI, V) (reinterpret_cast<std::atomic<typename std::decay<decltype (*(AI))>::type> *> (AI)->store ((V), std::memory_order_release))
|
||||
#define hb_atomic_int_impl_get_relaxed(AI) (reinterpret_cast<std::atomic<typename std::decay<decltype (*(AI))>::type> const *> (AI)->load (std::memory_order_relaxed))
|
||||
#define hb_atomic_int_impl_get(AI) (reinterpret_cast<std::atomic<typename std::decay<decltype (*(AI))>::type> const *> (AI)->load (std::memory_order_acquire))
|
||||
|
||||
#define hb_atomic_ptr_impl_set_relaxed(P, V) (reinterpret_cast<std::atomic<void*> *> (P)->store ((V), std::memory_order_relaxed))
|
||||
#define hb_atomic_ptr_impl_get_relaxed(P) (reinterpret_cast<std::atomic<void*> const *> (P)->load (std::memory_order_relaxed))
|
||||
#define hb_atomic_ptr_impl_get(P) (reinterpret_cast<std::atomic<void*> *> (P)->load (std::memory_order_acquire))
|
||||
static inline bool
|
||||
_hb_atomic_ptr_impl_cmplexch (const void **P, const void *O_, const void *N)
|
||||
{
|
||||
const void *O = O_; // Need lvalue
|
||||
return reinterpret_cast<std::atomic<const void*> *> (P)->compare_exchange_weak (O, N, std::memory_order_acq_rel, std::memory_order_relaxed);
|
||||
}
|
||||
#define hb_atomic_ptr_impl_cmpexch(P,O,N) _hb_atomic_ptr_impl_cmplexch ((const void **) (P), (O), (N))
|
||||
|
||||
|
||||
#else /* defined(HB_NO_MT) */
|
||||
|
||||
#define hb_atomic_int_impl_add(AI, V) ((*(AI) += (V)) - (V))
|
||||
@@ -159,6 +142,81 @@ inline T hb_atomic_int_impl_get (const T *AI) { T v = *AI; _hb_memory_r_barrier
|
||||
inline void *hb_atomic_ptr_impl_get (void ** const P) { void *v = *P; _hb_memory_r_barrier (); return v; }
|
||||
#endif
|
||||
|
||||
#ifdef HB_STL_ATOMIC_IMPL
|
||||
template <typename T>
|
||||
struct hb_atomic_t
|
||||
{
|
||||
hb_atomic_t () = default;
|
||||
constexpr hb_atomic_t (T v) : v (v) {}
|
||||
constexpr hb_atomic_t (const hb_atomic_t& o) : v (o.get_relaxed ()) {}
|
||||
constexpr hb_atomic_t (hb_atomic_t&& o) : v (o.get_relaxed ()) { o.set_relaxed ({}); }
|
||||
|
||||
hb_atomic_t &operator= (const hb_atomic_t& o) { set_relaxed (o.get_relaxed ()); return *this; }
|
||||
hb_atomic_t &operator= (hb_atomic_t&& o){ set_relaxed (o.get_relaxed ()); o.set_relaxed ({}); return *this; }
|
||||
hb_atomic_t &operator= (T v_)
|
||||
{
|
||||
set_relaxed (v_);
|
||||
return *this;
|
||||
}
|
||||
operator T () const { return get_relaxed (); }
|
||||
|
||||
void set_relaxed (T v_) { v.store (v_, std::memory_order_relaxed); }
|
||||
void set_release (T v_) { v.store (v_, std::memory_order_release); }
|
||||
T get_relaxed () const { return v.load (std::memory_order_relaxed); }
|
||||
T get_acquire () const { return v.load (std::memory_order_acquire); }
|
||||
T inc () { return v.fetch_add (1, std::memory_order_acq_rel); }
|
||||
T dec () { return v.fetch_add (-1, std::memory_order_acq_rel); }
|
||||
|
||||
int operator++ (int) { return inc (); }
|
||||
int operator-- (int) { return dec (); }
|
||||
long operator|= (long v_)
|
||||
{
|
||||
set_relaxed (get_relaxed () | v_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend void swap (hb_atomic_t &a, hb_atomic_t &b) noexcept
|
||||
{
|
||||
T v = a.get_acquire ();
|
||||
a.set_relaxed (b.get_acquire ());
|
||||
b.set_relaxed (v);
|
||||
}
|
||||
|
||||
std::atomic<T> v = 0;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct hb_atomic_t<T *>
|
||||
{
|
||||
hb_atomic_t () = default;
|
||||
constexpr hb_atomic_t (T *v) : v (v) {}
|
||||
hb_atomic_t (const hb_atomic_t &other) = delete;
|
||||
|
||||
void init (T *v_ = nullptr) { set_relaxed (v_); }
|
||||
void set_relaxed (T *v_) { v.store (v_, std::memory_order_relaxed); }
|
||||
T *get_relaxed () const { return v.load (std::memory_order_relaxed); }
|
||||
T *get_acquire () const { return v.load (std::memory_order_acquire); }
|
||||
bool cmpexch (T *old, T *new_) { return v.compare_exchange_weak (old, new_, std::memory_order_acq_rel, std::memory_order_relaxed); }
|
||||
|
||||
operator bool () const { return get_acquire () != nullptr; }
|
||||
T *operator->() const { return get_acquire (); }
|
||||
template <typename C>
|
||||
operator C * () const
|
||||
{
|
||||
return get_acquire ();
|
||||
}
|
||||
|
||||
friend void swap (hb_atomic_t &a, hb_atomic_t &b) noexcept
|
||||
{
|
||||
T *p = a.get_acquire ();
|
||||
a.set_relaxed (b.get_acquire ());
|
||||
b.set_relaxed (p);
|
||||
}
|
||||
|
||||
std::atomic<T *> v = nullptr;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
template <typename T>
|
||||
struct hb_atomic_t
|
||||
@@ -194,7 +252,7 @@ struct hb_atomic_t<T*>
|
||||
void set_relaxed (T* v_) { hb_atomic_ptr_impl_set_relaxed (&v, v_); }
|
||||
T *get_relaxed () const { return (T *) hb_atomic_ptr_impl_get_relaxed (&v); }
|
||||
T *get_acquire () const { return (T *) hb_atomic_ptr_impl_get ((void **) &v); }
|
||||
bool cmpexch (const T *old, T *new_) { return hb_atomic_ptr_impl_cmpexch ((void **) &v, (void *) old, (void *) new_); }
|
||||
bool cmpexch (T *old, T *new_) { return hb_atomic_ptr_impl_cmpexch ((void **) &v, (void *) old, (void *) new_); }
|
||||
|
||||
operator bool () const { return get_acquire () != nullptr; }
|
||||
T * operator -> () const { return get_acquire (); }
|
||||
@@ -203,6 +261,8 @@ struct hb_atomic_t<T*>
|
||||
T *v = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
static inline bool hb_barrier ()
|
||||
{
|
||||
_hb_compiler_memory_r_barrier ();
|
||||
|
||||
Reference in new issue
Block a user