This commit is contained in:
blueloveTH 2024-05-13 02:20:11 +08:00
parent 47463290a6
commit bf481b84dc
6 changed files with 12 additions and 10 deletions

View File

@ -42,7 +42,7 @@ struct ManagedHeap{
PyVar gcnew(Type type, Args&&... args){ PyVar gcnew(Type type, Args&&... args){
using __T = Py_<std::decay_t<T>>; using __T = Py_<std::decay_t<T>>;
// https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476 // https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476
PyObject* p = new(pool64_alloc<__T>()) Py_<std::decay_t<T>>(std::forward<Args>(args)...); PyObject* p = new(pool128_alloc<__T>()) Py_<std::decay_t<T>>(std::forward<Args>(args)...);
PyVar obj(type, p); PyVar obj(type, p);
gen.push_back(obj); gen.push_back(obj);
gc_counter++; gc_counter++;
@ -53,7 +53,7 @@ struct ManagedHeap{
PyVar _new(Type type, Args&&... args){ PyVar _new(Type type, Args&&... args){
using __T = Py_<std::decay_t<T>>; using __T = Py_<std::decay_t<T>>;
// https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476 // https://github.com/pocketpy/pocketpy/issues/94#issuecomment-1594784476
PyObject* p = new(pool64_alloc<__T>()) Py_<std::decay_t<T>>(std::forward<Args>(args)...); PyObject* p = new(pool128_alloc<__T>()) Py_<std::decay_t<T>>(std::forward<Args>(args)...);
PyVar obj(type, p); PyVar obj(type, p);
obj->gc_enabled = false; obj->gc_enabled = false;
_no_gc.push_back(obj); _no_gc.push_back(obj);

View File

@ -10,8 +10,10 @@ namespace pkpy {
using List = pod_vector<PyVar, 4>; using List = pod_vector<PyVar, 4>;
struct Tuple { struct Tuple {
static const int INLINED_SIZE = 4;
PyVar* _args; PyVar* _args;
PyVar _inlined[4]; PyVar _inlined[INLINED_SIZE];
int _size; int _size;
Tuple(int n); Tuple(int n);

View File

@ -14,7 +14,7 @@ namespace pkpy{
#endif #endif
if(_gc_on_delete) _gc_on_delete(vm, obj); if(_gc_on_delete) _gc_on_delete(vm, obj);
obj->~PyObject(); obj->~PyObject();
pool64_dealloc(obj.get()); pool128_dealloc(obj.get());
} }
} }
@ -55,8 +55,8 @@ namespace pkpy{
} }
ManagedHeap::~ManagedHeap(){ ManagedHeap::~ManagedHeap(){
for(PyVar obj: _no_gc) { obj->~PyObject(); pool64_dealloc(obj.get()); } for(PyVar obj: _no_gc) { obj->~PyObject(); pool128_dealloc(obj.get()); }
for(PyVar obj: gen) { obj->~PyObject(); pool64_dealloc(obj.get()); } for(PyVar obj: gen) { obj->~PyObject(); pool128_dealloc(obj.get()); }
} }

View File

@ -1530,7 +1530,7 @@ void VM::__post_init_builtin_types(){
bind_property(_t(tp_object), "__class__", PK_LAMBDA(vm->_t(args[0]))); bind_property(_t(tp_object), "__class__", PK_LAMBDA(vm->_t(args[0])));
bind_property(_t(tp_type), "__base__", [](VM* vm, ArgsView args){ bind_property(_t(tp_type), "__base__", [](VM* vm, ArgsView args){
const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])]; const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];
return info.base.index == -1 ? vm->None : vm->_all_types[info.base].obj; return info.base ? vm->_all_types[info.base].obj : vm->None;
}); });
bind_property(_t(tp_type), "__name__", [](VM* vm, ArgsView args){ bind_property(_t(tp_type), "__name__", [](VM* vm, ArgsView args){
const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])]; const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];

View File

@ -3,7 +3,7 @@
namespace pkpy { namespace pkpy {
Tuple::Tuple(int n){ Tuple::Tuple(int n){
if(n <= 3){ if(n <= INLINED_SIZE){
this->_args = _inlined; this->_args = _inlined;
}else{ }else{
this->_args = (PyVar*)pool128_alloc(n * sizeof(void*)); this->_args = (PyVar*)pool128_alloc(n * sizeof(void*));

View File

@ -149,7 +149,7 @@ namespace pkpy{
val = _t(cls)->attr().try_get(name); val = _t(cls)->attr().try_get(name);
if(val != nullptr) return val; if(val != nullptr) return val;
cls = _all_types[cls].base; cls = _all_types[cls].base;
if(cls.index == -1) break; if(!cls) break;
}while(true); }while(true);
return nullptr; return nullptr;
} }
@ -162,7 +162,7 @@ namespace pkpy{
do{ do{
if(cls == base) return true; if(cls == base) return true;
Type next = _all_types[cls].base; Type next = _all_types[cls].base;
if(next.index == -1) break; if(!next) break;
cls = next; cls = next;
}while(true); }while(true);
return false; return false;