From 39ad3f2ef490e2627442b172d78ce6c5084dd0de Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 20 May 2024 00:09:48 +0800 Subject: [PATCH] some fix --- include/pocketpy/gc.h | 15 ++++++--------- include/pocketpy/obj.h | 4 ++-- src/codeobject.cpp | 10 +++------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/include/pocketpy/gc.h b/include/pocketpy/gc.h index 9c468197..21090ce0 100644 --- a/include/pocketpy/gc.h +++ b/include/pocketpy/gc.h @@ -43,24 +43,21 @@ struct ManagedHeap{ using __T = std::decay_t; static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types"); // https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476 - PyObject* p = new(pool128_alloc(py_sizeof<__T>)) PyObject(); + PyObject* p = new(pool128_alloc(py_sizeof<__T>)) PyObject(true); p->placement_new<__T>(std::forward(args)...); - PyVar obj(type, p); - gen.push_back(obj); + gen.emplace_back(type, p); gc_counter++; - return obj; + return gen.back(); } template PyVar _new(Type type, Args&&... args){ using __T = std::decay_t; static_assert(!is_sso_v<__T>); - PyObject* p = new(pool128_alloc<__T>()) PyObject(); + PyObject* p = new(pool128_alloc<__T>()) PyObject(false); p->placement_new<__T>(std::forward(args)...); - PyVar obj(type, p); - obj->gc_enabled = false; - _no_gc.push_back(obj); - return obj; + _no_gc.emplace_back(type, p); + return _no_gc.back(); } void _delete(PyVar); diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index 5e4e18fc..eabaf4b3 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -101,7 +101,7 @@ struct Slice { void _gc_mark(VM*) const; }; -struct PyObject{ +struct PyObject final{ bool gc_enabled; // whether this object is managed by GC bool gc_marked; // whether this object is marked NameDict* _attr; @@ -119,7 +119,7 @@ struct PyObject{ return (*_attr)[name]; } - PyObject() : gc_enabled(true), gc_marked(false), _attr(nullptr) {} + PyObject(bool gc_enabled) : gc_enabled(gc_enabled), gc_marked(false), _attr(nullptr) {} template void placement_new(Args&&... args){ diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 8b2f277d..177ac565 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -7,11 +7,7 @@ namespace pkpy{ blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0)); } - struct PySignalObject: PyObject { - PySignalObject() : PyObject() { gc_enabled = false; } - }; - - PyVar const PY_NULL(Type(), new PySignalObject()); - PyVar const PY_OP_CALL(Type(), new PySignalObject()); - PyVar const PY_OP_YIELD(Type(), new PySignalObject()); + PyVar const PY_NULL(Type(), new PyObject(false)); + PyVar const PY_OP_CALL(Type(), new PyObject(false)); + PyVar const PY_OP_YIELD(Type(), new PyObject(false)); } // namespace pkpy \ No newline at end of file