use pool128 for Expr_

This commit is contained in:
blueloveTH 2024-02-18 23:09:41 +08:00
parent 458dd32af8
commit 4cfe93ee51
2 changed files with 17 additions and 17 deletions

View File

@ -62,9 +62,9 @@ class Compiler {
Expr_ EXPR_VARS(); // special case for `for loop` and `comp` Expr_ EXPR_VARS(); // special case for `for loop` and `comp`
template <typename T, typename... Args> template <typename T, typename... Args>
unique_ptr_64<T> make_expr(Args&&... args) { unique_ptr_128<T> make_expr(Args&&... args) {
void* p = pool64_alloc(sizeof(T)); void* p = pool128_alloc(sizeof(T));
unique_ptr_64<T> expr(new (p) T(std::forward<Args>(args)...)); unique_ptr_128<T> expr(new (p) T(std::forward<Args>(args)...));
expr->line = prev().line; expr->line = prev().line;
return expr; return expr;
} }
@ -72,7 +72,7 @@ class Compiler {
template<typename T> template<typename T>
void _consume_comp(Expr_ expr){ void _consume_comp(Expr_ expr){
static_assert(std::is_base_of<CompExpr, T>::value); static_assert(std::is_base_of<CompExpr, T>::value);
unique_ptr_64<CompExpr> ce = make_expr<T>(); unique_ptr_128<CompExpr> ce = make_expr<T>();
ce->expr = std::move(expr); ce->expr = std::move(expr);
ce->vars = EXPR_VARS(); ce->vars = EXPR_VARS();
consume(TK("in")); consume(TK("in"));

View File

@ -11,46 +11,46 @@ namespace pkpy{
struct CodeEmitContext; struct CodeEmitContext;
struct Expr; struct Expr;
#define PK_POOL64_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); pool64_dealloc(ptr); ptr = nullptr; } #define PK_POOL128_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); pool128_dealloc(ptr); ptr = nullptr; }
template<typename T> template<typename T>
class unique_ptr_64{ class unique_ptr_128{
T* ptr; T* ptr;
public: public:
unique_ptr_64(): ptr(nullptr) {} unique_ptr_128(): ptr(nullptr) {}
unique_ptr_64(T* ptr): ptr(ptr) {} unique_ptr_128(T* ptr): ptr(ptr) {}
T* operator->() const { return ptr; } T* operator->() const { return ptr; }
T* get() const { return ptr; } T* get() const { return ptr; }
T* release() { T* p = ptr; ptr = nullptr; return p; } T* release() { T* p = ptr; ptr = nullptr; return p; }
unique_ptr_64(const unique_ptr_64&) = delete; unique_ptr_128(const unique_ptr_128&) = delete;
unique_ptr_64& operator=(const unique_ptr_64&) = delete; unique_ptr_128& operator=(const unique_ptr_128&) = delete;
bool operator==(std::nullptr_t) const { return ptr == nullptr; } bool operator==(std::nullptr_t) const { return ptr == nullptr; }
bool operator!=(std::nullptr_t) const { return ptr != nullptr; } bool operator!=(std::nullptr_t) const { return ptr != nullptr; }
~unique_ptr_64(){ PK_POOL64_DELETE(ptr) } ~unique_ptr_128(){ PK_POOL128_DELETE(ptr) }
template<typename U> template<typename U>
unique_ptr_64(unique_ptr_64<U>&& other): ptr(other.release()) {} unique_ptr_128(unique_ptr_128<U>&& other): ptr(other.release()) {}
operator bool() const { return ptr != nullptr; } operator bool() const { return ptr != nullptr; }
template<typename U> template<typename U>
unique_ptr_64& operator=(unique_ptr_64<U>&& other) { unique_ptr_128& operator=(unique_ptr_128<U>&& other) {
PK_POOL64_DELETE(ptr) PK_POOL128_DELETE(ptr)
ptr = other.release(); ptr = other.release();
return *this; return *this;
} }
unique_ptr_64& operator=(std::nullptr_t) { unique_ptr_128& operator=(std::nullptr_t) {
PK_POOL64_DELETE(ptr) PK_POOL128_DELETE(ptr)
ptr = nullptr; ptr = nullptr;
return *this; return *this;
} }
}; };
typedef unique_ptr_64<Expr> Expr_; typedef unique_ptr_128<Expr> Expr_;
typedef small_vector<Expr_, 6> Expr_vector; typedef small_vector<Expr_, 6> Expr_vector;
struct Expr{ struct Expr{