diff --git a/src/frame.h b/src/frame.h index 65296a3d..ddf3f97c 100644 --- a/src/frame.h +++ b/src/frame.h @@ -112,46 +112,60 @@ template<> inline void gc_mark(Function& t){ t._closure._gc_mark(); } -struct ValueStack { - PyObject** _begin; - PyObject** _sp; - - ValueStack(int n=16): _begin((PyObject**)pool128.alloc(n * sizeof(void*))), _sp(_begin) { } - - PyObject*& top(){ return _sp[-1]; } - PyObject* top() const { return _sp[-1]; } - PyObject*& second(){ return _sp[-2]; } - PyObject* second() const { return _sp[-2]; } - PyObject*& peek(int n){ return _sp[-n]; } - PyObject* peek(int n) const { return _sp[-n]; } - void push(PyObject* v){ *_sp++ = v; } - void pop(){ --_sp; } - PyObject* popx(){ return *--_sp; } - ArgsView view(int n){ return ArgsView(_sp-n, _sp); } - void shrink(int n){ _sp -= n; } - int size() const { return _sp - _begin; } - bool empty() const { return _sp == _begin; } - PyObject** begin() const { return _begin; } - PyObject** end() const { return _sp; } - void resize(int n) { _sp = _begin + n; } - - ValueStack(ValueStack&& other) noexcept{ - _begin = other._begin; - _sp = other._sp; - other._begin = nullptr; - } - - ValueStack& operator=(ValueStack&& other) noexcept{ - if(_begin != nullptr) pool128.dealloc(_begin); - _begin = other._begin; - _sp = other._sp; - other._begin = nullptr; - return *this; - } - - ~ValueStack(){ if(_begin!=nullptr) pool128.dealloc(_begin); } +struct ValueStack: pod_vector { + PyObject*& top(){ return back(); } + PyObject* top() const { return back(); } + PyObject*& second(){ return (*this)[size()-2]; } + PyObject* second() const { return (*this)[size()-2]; } + PyObject*& peek(int n){ return (*this)[size()-n]; } + PyObject* peek(int n) const { return (*this)[size()-n]; } + void push(PyObject* v){ push_back(v); } + void pop(){ pop_back(); } + PyObject* popx(){ return popx_back(); } + ArgsView view(int n){ return ArgsView(end()-n, end()); } + void shrink(int n){ resize(size() - n); } }; +// struct ValueStack { +// PyObject** _begin; +// PyObject** _sp; + +// ValueStack(int n=16): _begin((PyObject**)pool128.alloc(n * sizeof(void*))), _sp(_begin) { } + +// PyObject*& top(){ return _sp[-1]; } +// PyObject* top() const { return _sp[-1]; } +// PyObject*& second(){ return _sp[-2]; } +// PyObject* second() const { return _sp[-2]; } +// PyObject*& peek(int n){ return _sp[-n]; } +// PyObject* peek(int n) const { return _sp[-n]; } +// void push(PyObject* v){ *_sp++ = v; } +// void pop(){ --_sp; } +// PyObject* popx(){ return *--_sp; } +// ArgsView view(int n){ return ArgsView(_sp-n, _sp); } +// void shrink(int n){ _sp -= n; } +// int size() const { return _sp - _begin; } +// bool empty() const { return _sp == _begin; } +// PyObject** begin() const { return _begin; } +// PyObject** end() const { return _sp; } +// void resize(int n) { _sp = _begin + n; } + +// ValueStack(ValueStack&& other) noexcept{ +// _begin = other._begin; +// _sp = other._sp; +// other._begin = nullptr; +// } + +// ValueStack& operator=(ValueStack&& other) noexcept{ +// if(_begin != nullptr) pool128.dealloc(_begin); +// _begin = other._begin; +// _sp = other._sp; +// other._begin = nullptr; +// return *this; +// } + +// ~ValueStack(){ if(_begin!=nullptr) pool128.dealloc(_begin); } +// }; + struct Frame { int _ip = -1; int _next_ip = 0; diff --git a/src/gc.h b/src/gc.h index bfa9320d..1a883210 100644 --- a/src/gc.h +++ b/src/gc.h @@ -56,15 +56,6 @@ struct ManagedHeap{ inline static std::map deleted; #endif - ~ManagedHeap(){ - for(PyObject* obj: _no_gc) obj->~PyObject(), pool64.dealloc(obj); -#if DEBUG_GC_STATS - for(auto& [type, count]: deleted){ - std::cout << "GC: " << obj_type_name(vm, type) << "=" << count << std::endl; - } -#endif - } - int sweep(){ std::vector alive; for(PyObject* obj: gen){ @@ -108,6 +99,16 @@ struct ManagedHeap{ } void mark(); + + ~ManagedHeap(){ + for(PyObject* obj: _no_gc) obj->~PyObject(), pool64.dealloc(obj); + for(PyObject* obj: gen) obj->~PyObject(), pool64.dealloc(obj); +#if DEBUG_GC_STATS + for(auto& [type, count]: deleted){ + std::cout << "GC: " << obj_type_name(vm, type) << "=" << count << std::endl; + } +#endif + } }; inline void FuncDecl::_gc_mark() const{ @@ -116,11 +117,15 @@ inline void FuncDecl::_gc_mark() const{ } template<> inline void gc_mark(List& t){ - for(PyObject* obj: t) OBJ_MARK(obj); + for(PyObject* obj: t){ + OBJ_MARK(obj); + } } template<> inline void gc_mark(Tuple& t){ - for(int i=0; i inline void gc_mark(NameDict& t){ diff --git a/src/namedict.h b/src/namedict.h index 6406a809..cd2a9361 100644 --- a/src/namedict.h +++ b/src/namedict.h @@ -192,6 +192,14 @@ while(!_items[i].first.empty()) { \ } return v; } + + void clear(){ + for(uint16_t i=0; i<_capacity; i++){ + _items[i].first = StrName(); + _items[i].second = nullptr; + } + _size = 0; + } #undef HASH_PROBE #undef _hash }; diff --git a/src/vector.h b/src/vector.h index aebcd354..6c67c19a 100644 --- a/src/vector.h +++ b/src/vector.h @@ -71,6 +71,7 @@ struct pod_vector{ } void pop_back() { _size--; } + T popx_back() { T t = std::move(_data[_size-1]); _size--; return t; } void extend(const pod_vector& other){ for(int i=0; i diff --git a/src/vm.h b/src/vm.h index 083dc420..37980ab8 100644 --- a/src/vm.h +++ b/src/vm.h @@ -326,7 +326,12 @@ public: return _all_types[OBJ_GET(Type, _t(obj->type)).index].obj; } - ~VM() { heap.collect(); } + ~VM() { + callstack.clear(); + _all_types.clear(); + _modules.clear(); + _lazy_modules.clear(); + } CodeObject_ compile(Str source, Str filename, CompileMode mode); PyObject* num_negated(PyObject* obj);