Kaynağa Gözat

some refactor

blueloveTH 1 yıl önce
ebeveyn
işleme
ec366c15c6

+ 19 - 12
include/pocketpy/interpreter/gc.hpp

@@ -4,6 +4,7 @@
 #include "pocketpy/common/vector.hpp"
 #include "pocketpy/common/utils.hpp"
 #include "pocketpy/objects/object.hpp"
+#include "pocketpy/objects/namedict.hpp"
 
 namespace pkpy {
 struct ManagedHeap {
@@ -34,9 +35,8 @@ struct ManagedHeap {
     ScopeLock gc_scope_lock() { return ScopeLock(this); }
 
     /********************/
-
     template <typename T, typename... Args>
-    PyObject* gcnew(Type type, Args&&... args) {
+    PyObject* _basic_new(Type type, Args&&... args) {
         using __T = std::decay_t<T>;
         static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types");
         // https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476
@@ -46,7 +46,22 @@ struct ManagedHeap {
         }else{
             p = new (std::malloc(py_sizeof<__T>)) PyObject(type, true);
         }
-        p->placement_new<__T>(std::forward<Args>(args)...);
+        new (p->_value_ptr()) T(std::forward<Args>(args)...);
+
+        // backdoor for important builtin types
+        if constexpr(std::is_same_v<__T, DummyInstance>) {
+            p->_attr = new NameDict(PK_INST_ATTR_LOAD_FACTOR);
+        } else if constexpr(std::is_same_v<__T, Type>) {
+            p->_attr = new NameDict(PK_TYPE_ATTR_LOAD_FACTOR);
+        } else if constexpr(std::is_same_v<__T, DummyModule>) {
+            p->_attr = new NameDict(PK_TYPE_ATTR_LOAD_FACTOR);
+        }
+        return p;
+    }
+
+    template <typename T, typename... Args>
+    PyObject* gcnew(Type type, Args&&... args) {
+        PyObject* p = _basic_new<T>(type, std::forward<Args>(args)...);
         gen.push_back(p);
         gc_counter++;
         return p;
@@ -54,15 +69,7 @@ struct ManagedHeap {
 
     template <typename T, typename... Args>
     PyObject* _new(Type type, Args&&... args) {
-        using __T = std::decay_t<T>;
-        static_assert(!is_sso_v<__T>);
-        PyObject* p;
-        if constexpr(py_sizeof<__T> <= kPoolObjectBlockSize){
-            p = new (PoolObject_alloc()) PyObject(type, false);
-        }else{
-            p = new (std::malloc(py_sizeof<__T>)) PyObject(type, true);
-        }
-        p->placement_new<__T>(std::forward<Args>(args)...);
+        PyObject* p = _basic_new<T>(type, std::forward<Args>(args)...);
         _no_gc.push_back(p);
         return p;
     }

+ 0 - 16
include/pocketpy/objects/object.hpp

@@ -32,22 +32,6 @@ struct PyObject final {
     PyObject(Type type, bool gc_is_large) : type(type), gc_is_large(gc_is_large), gc_marked(false), _attr(nullptr) {}
 
     PyVar attr(StrName name) const;
-    static NameDict* __init_namedict(float lf);
-
-    template <typename T, typename... Args>
-    void placement_new(Args&&... args) {
-        static_assert(std::is_same_v<T, std::decay_t<T>>);
-        new (_value_ptr()) T(std::forward<Args>(args)...);
-
-        // backdoor for important builtin types
-        if constexpr(std::is_same_v<T, DummyInstance>) {
-            _attr = __init_namedict(PK_INST_ATTR_LOAD_FACTOR);
-        } else if constexpr(std::is_same_v<T, Type>) {
-            _attr = __init_namedict(PK_TYPE_ATTR_LOAD_FACTOR);
-        } else if constexpr(std::is_same_v<T, DummyModule>) {
-            _attr = __init_namedict(PK_TYPE_ATTR_LOAD_FACTOR);
-        }
-    }
 };
 
 static_assert(sizeof(PyObject) <= 16);

+ 0 - 4
src/objects/namedict.cpp

@@ -160,8 +160,4 @@ PyVar PyObject::attr(StrName name) const {
     return (*_attr)[name];
 }
 
-NameDict* PyObject::__init_namedict(float lf) {
-    return new NameDict(lf);
-}
-
 }  // namespace pkpy