From b836a6be8445fcd8d18f8c6f68eb30fd67ebb189 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 12 Feb 2023 17:32:10 +0800 Subject: [PATCH] Update obj.h --- src/obj.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/obj.h b/src/obj.h index d52ee69d..6b83d0a7 100644 --- a/src/obj.h +++ b/src/obj.h @@ -77,7 +77,9 @@ public: struct PyObject { Type type; pkpy::NameDict* _attr; - void* _tid; + // void* _tid; + const int _size; + inline bool is_attr_valid() const noexcept { return _attr != nullptr; } inline pkpy::NameDict& attr() noexcept { return *_attr; } inline PyVar& attr(const Str& name) noexcept { return (*_attr)[name]; } @@ -85,7 +87,7 @@ struct PyObject { inline bool is_type(Type type) const noexcept{ return this->type == type; } virtual void* value() = 0; - PyObject(Type type, void* _tid) : type(type), _tid(_tid) {} + PyObject(Type type, const int size) : type(type), _size(size) {} virtual ~PyObject() { delete _attr; } }; @@ -93,7 +95,7 @@ template struct Py_ : PyObject { T _value; - Py_(Type type, T val) : PyObject(type, tid()), _value(val) { + Py_(Type type, T val) : PyObject(type, sizeof(Py_)), _value(val) { if constexpr (std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v) { _attr = new pkpy::NameDict(); @@ -146,15 +148,15 @@ namespace pkpy { } }; - static_assert(sizeof(i64) == sizeof(f64)); - static thread_local MemBlock)> _mem_i64(512); + constexpr int kMemObjSize = sizeof(int) + sizeof(Py_); + static thread_local MemBlock _mem_pool(512); template<> struct SpAllocator { template inline static int* alloc(){ - if constexpr (std::is_same_v> || std::is_same_v>) { - return (int*)_mem_i64.alloc(); + if constexpr (sizeof(int) + sizeof(U) == kMemObjSize) { + return (int*)_mem_pool.alloc(); } return (int*)malloc(sizeof(int) + sizeof(U)); } @@ -162,8 +164,8 @@ namespace pkpy { inline static void dealloc(int* counter){ PyObject* obj = (PyObject*)(counter + 1); obj->~PyObject(); - if(obj->_tid == tid() || obj->_tid == tid()){ - _mem_i64.dealloc(counter); + if(obj->_size == kMemObjSize - sizeof(int)){ + _mem_pool.dealloc(counter); }else{ free(counter); }