diff --git a/src/common.h b/src/common.h index ed2d6749..2b07652d 100644 --- a/src/common.h +++ b/src/common.h @@ -36,7 +36,7 @@ #define PK_VERSION "0.8.9" -#if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(PK_32_BIT) +#if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__) typedef int32_t i64; typedef float f64; const i64 kMinSafeInt = -((i64)1 << 30); diff --git a/src/memory.h b/src/memory.h index 8f807b79..980031ca 100644 --- a/src/memory.h +++ b/src/memory.h @@ -22,28 +22,9 @@ namespace pkpy{ class shared_ptr { int* counter; - inline T* _t() const { - if constexpr(std::is_same_v){ - if(is_tagged()) UNREACHABLE(); - } - return (T*)(counter + 1); - } - - inline void _inc_counter() const { - if constexpr(std::is_same_v){ - if(is_tagged()) return; - } - if(counter) ++(*counter); - } - - inline void _dec_counter() const { - if constexpr(std::is_same_v){ - if(is_tagged()) return; - } - if(counter && --(*counter) == 0){ - SpAllocator::dealloc(counter); - } - } +#define _t() (T*)(counter + 1) +#define _inc_counter() if(!is_tagged() && counter) ++(*counter) +#define _dec_counter() if(!is_tagged() && counter && --(*counter) == 0) SpAllocator::dealloc(counter) public: shared_ptr() : counter(nullptr) {} @@ -99,13 +80,20 @@ namespace pkpy{ return reinterpret_cast<__VAL>(counter); } - inline bool is_tagged() const { return (cast() & 0b11) != 0b00; } - inline bool is_tag_00() const { return (cast() & 0b11) == 0b00; } - inline bool is_tag_01() const { return (cast() & 0b11) == 0b01; } - inline bool is_tag_10() const { return (cast() & 0b11) == 0b10; } - inline bool is_tag_11() const { return (cast() & 0b11) == 0b11; } + inline bool is_tagged() const { + if constexpr(!std::is_same_v) return false; + return (reinterpret_cast(counter) & 0b11) != 0b00; + } + inline bool is_tag_00() const { return (reinterpret_cast(counter) & 0b11) == 0b00; } + inline bool is_tag_01() const { return (reinterpret_cast(counter) & 0b11) == 0b01; } + inline bool is_tag_10() const { return (reinterpret_cast(counter) & 0b11) == 0b10; } + inline bool is_tag_11() const { return (reinterpret_cast(counter) & 0b11) == 0b11; } }; +#undef _t +#undef _inc_counter +#undef _dec_counter + template shared_ptr make_shared(Args&&... args) { static_assert(std::is_base_of_v, "U must be derived from T");