Просмотр исходного кода

Update memory.h

Update common.h
blueloveTH 3 лет назад
Родитель
Сommit
368e963cb5
2 измененных файлов с 16 добавлено и 28 удалено
  1. 1 1
      src/common.h
  2. 15 27
      src/memory.h

+ 1 - 1
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);

+ 15 - 27
src/memory.h

@@ -22,28 +22,9 @@ namespace pkpy{
     class shared_ptr {
         int* counter;
 
-        inline T* _t() const {
-            if constexpr(std::is_same_v<T, PyObject>){
-                if(is_tagged()) UNREACHABLE();
-            }
-            return (T*)(counter + 1);
-        }
-
-        inline void _inc_counter() const {
-            if constexpr(std::is_same_v<T, PyObject>){
-                if(is_tagged()) return;
-            }
-            if(counter) ++(*counter);
-        }
-
-        inline void _dec_counter() const {
-            if constexpr(std::is_same_v<T, PyObject>){
-                if(is_tagged()) return;
-            }
-            if(counter && --(*counter) == 0){
-                SpAllocator<T>::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<T>::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<i64>() & 0b11) != 0b00; }
-        inline bool is_tag_00() const { return (cast<i64>() & 0b11) == 0b00; }
-        inline bool is_tag_01() const { return (cast<i64>() & 0b11) == 0b01; }
-        inline bool is_tag_10() const { return (cast<i64>() & 0b11) == 0b10; }
-        inline bool is_tag_11() const { return (cast<i64>() & 0b11) == 0b11; }
+        inline bool is_tagged() const {
+            if constexpr(!std::is_same_v<T, PyObject>) return false;
+            return (reinterpret_cast<i64>(counter) & 0b11) != 0b00;
+        }
+        inline bool is_tag_00() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b00; }
+        inline bool is_tag_01() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b01; }
+        inline bool is_tag_10() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b10; }
+        inline bool is_tag_11() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b11; }
     };
 
+#undef _t
+#undef _inc_counter
+#undef _dec_counter
+
     template <typename T, typename U, typename... Args>
     shared_ptr<T> make_shared(Args&&... args) {
         static_assert(std::is_base_of_v<T, U>, "U must be derived from T");