mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
some refactor
This commit is contained in:
parent
4b687d36ff
commit
ec366c15c6
@ -4,6 +4,7 @@
|
|||||||
#include "pocketpy/common/vector.hpp"
|
#include "pocketpy/common/vector.hpp"
|
||||||
#include "pocketpy/common/utils.hpp"
|
#include "pocketpy/common/utils.hpp"
|
||||||
#include "pocketpy/objects/object.hpp"
|
#include "pocketpy/objects/object.hpp"
|
||||||
|
#include "pocketpy/objects/namedict.hpp"
|
||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
struct ManagedHeap {
|
struct ManagedHeap {
|
||||||
@ -34,9 +35,8 @@ struct ManagedHeap {
|
|||||||
ScopeLock gc_scope_lock() { return ScopeLock(this); }
|
ScopeLock gc_scope_lock() { return ScopeLock(this); }
|
||||||
|
|
||||||
/********************/
|
/********************/
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
PyObject* gcnew(Type type, Args&&... args) {
|
PyObject* _basic_new(Type type, Args&&... args) {
|
||||||
using __T = std::decay_t<T>;
|
using __T = std::decay_t<T>;
|
||||||
static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types");
|
static_assert(!is_sso_v<__T>, "gcnew cannot be used with SSO types");
|
||||||
// https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476
|
// https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476
|
||||||
@ -46,7 +46,22 @@ struct ManagedHeap {
|
|||||||
}else{
|
}else{
|
||||||
p = new (std::malloc(py_sizeof<__T>)) PyObject(type, true);
|
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);
|
gen.push_back(p);
|
||||||
gc_counter++;
|
gc_counter++;
|
||||||
return p;
|
return p;
|
||||||
@ -54,15 +69,7 @@ struct ManagedHeap {
|
|||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
PyObject* _new(Type type, Args&&... args) {
|
PyObject* _new(Type type, Args&&... args) {
|
||||||
using __T = std::decay_t<T>;
|
PyObject* p = _basic_new<T>(type, std::forward<Args>(args)...);
|
||||||
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)...);
|
|
||||||
_no_gc.push_back(p);
|
_no_gc.push_back(p);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -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) {}
|
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;
|
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);
|
static_assert(sizeof(PyObject) <= 16);
|
||||||
|
@ -160,8 +160,4 @@ PyVar PyObject::attr(StrName name) const {
|
|||||||
return (*_attr)[name];
|
return (*_attr)[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
NameDict* PyObject::__init_namedict(float lf) {
|
|
||||||
return new NameDict(lf);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
Loading…
x
Reference in New Issue
Block a user