From b4a836873edf9a4dbae2475a83a4e346c9959463 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 2 Jun 2024 21:33:03 +0800 Subject: [PATCH] some fix --- build_g.sh | 2 +- include/pocketpy/common/config.h | 4 -- include/pocketpy/common/memorypool.hpp | 11 +++++ include/pocketpy/common/namedict.hpp | 12 +++--- include/pocketpy/compiler/compiler.hpp | 3 +- include/pocketpy/compiler/expr.hpp | 2 +- include/pocketpy/interpreter/frame.hpp | 3 +- include/pocketpy/interpreter/iter.hpp | 2 +- src/common/memorypool.cpp | 56 +++++++++++++++++++++++++- src/interpreter/frame.cpp | 2 +- 10 files changed, 78 insertions(+), 19 deletions(-) diff --git a/build_g.sh b/build_g.sh index bc8d17be..c677933b 100644 --- a/build_g.sh +++ b/build_g.sh @@ -2,6 +2,6 @@ python prebuild.py SRC=$(find src/ -name "*.cpp") -FLAGS="-std=c++17 -Og -stdlib=libc++ -Iinclude -frtti -Wfatal-errors -g" +FLAGS="-std=c++17 -Og -stdlib=libc++ -Iinclude -frtti -Wfatal-errors -g -DDEBUG" clang++ $FLAGS -o main src2/main.cpp $SRC diff --git a/include/pocketpy/common/config.h b/include/pocketpy/common/config.h index 1d9c5f51..07182d9a 100644 --- a/include/pocketpy/common/config.h +++ b/include/pocketpy/common/config.h @@ -31,10 +31,6 @@ #define PK_ENABLE_STD_FUNCTION 0 /*************** debug settings ***************/ - -// Enable this may help you find bugs -#define PK_DEBUG_EXTRA_CHECK 0 - // Do not edit the following settings unless you know what you are doing #define PK_DEBUG_CEVAL_STEP 0 #define PK_DEBUG_MEMORY_POOL 0 diff --git a/include/pocketpy/common/memorypool.hpp b/include/pocketpy/common/memorypool.hpp index 3b2f77c2..1414cbe7 100644 --- a/include/pocketpy/common/memorypool.hpp +++ b/include/pocketpy/common/memorypool.hpp @@ -1,6 +1,9 @@ #pragma once +#include "pocketpy/common/gil.hpp" + #include +#include #include namespace pkpy{ @@ -13,4 +16,12 @@ void pools_shrink_to_fit() noexcept; std::string pool64_info() noexcept; std::string pool128_info() noexcept; +inline const int kPoolExprBlockSize = 128; +inline const int kPoolFrameBlockSize = 80; + +void* PoolExpr_alloc() noexcept; +void PoolExpr_dealloc(void*) noexcept; +void* PoolFrame_alloc() noexcept; +void PoolFrame_dealloc(void*) noexcept; + }; // namespace pkpy diff --git a/include/pocketpy/common/namedict.hpp b/include/pocketpy/common/namedict.hpp index 7ce11673..359b62c2 100644 --- a/include/pocketpy/common/namedict.hpp +++ b/include/pocketpy/common/namedict.hpp @@ -94,7 +94,7 @@ while(!_items[i].first.empty()) { \ return _items[i].second; } - T* try_get_2(StrName key) { + T* try_get_2(StrName key) const{ bool ok; uint16_t i; HASH_PROBE_0(key, ok, i); if(!ok) return nullptr; @@ -109,7 +109,7 @@ while(!_items[i].first.empty()) { \ return try_get(key); } - T* try_get_2_likely_found(StrName key) { + T* try_get_2_likely_found(StrName key) const{ uint16_t i = key.index & _mask; if(_items[i].first == key) return &_items[i].second; i = (i + 1) & _mask; @@ -152,13 +152,11 @@ while(!_items[i].first.empty()) { \ } T operator[](StrName key) const { - T val = try_get_likely_found(key); -#if PK_DEBUG_EXTRA_CHECK - if(val == default_invalid_value()){ + T* val = try_get_2_likely_found(key); + if(val == nullptr){ throw std::runtime_error(_S("NameDict key not found: ", key.escape()).str()); } -#endif - return val; + return *val; } array keys() const { diff --git a/include/pocketpy/compiler/compiler.hpp b/include/pocketpy/compiler/compiler.hpp index ab1f7eac..16f75a3b 100644 --- a/include/pocketpy/compiler/compiler.hpp +++ b/include/pocketpy/compiler/compiler.hpp @@ -59,7 +59,8 @@ class Compiler { template unique_ptr_128 make_expr(Args&&... args) { - void* p = pool128_alloc(sizeof(T)); + static_assert(sizeof(T) <= kPoolExprBlockSize); + void* p = PoolExpr_alloc(); unique_ptr_128 expr(new (p) T(std::forward(args)...)); expr->line = prev().line; return expr; diff --git a/include/pocketpy/compiler/expr.hpp b/include/pocketpy/compiler/expr.hpp index c6134e61..93485d0f 100644 --- a/include/pocketpy/compiler/expr.hpp +++ b/include/pocketpy/compiler/expr.hpp @@ -8,7 +8,7 @@ namespace pkpy{ struct CodeEmitContext; struct Expr; -#define PK_POOL128_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); pool128_dealloc(ptr); ptr = nullptr; } +#define PK_POOL128_DELETE(ptr) if(ptr != nullptr) { ptr->~T(); PoolExpr_dealloc(ptr); ptr = nullptr; } template class unique_ptr_128{ diff --git a/include/pocketpy/interpreter/frame.hpp b/include/pocketpy/interpreter/frame.hpp index 363e8d06..d6a5adf2 100644 --- a/include/pocketpy/interpreter/frame.hpp +++ b/include/pocketpy/interpreter/frame.hpp @@ -147,7 +147,8 @@ struct CallStack{ template void emplace(Args&&... args){ - _tail = new(pool128_alloc(sizeof(LinkedFrame))) LinkedFrame(_tail, std::forward(args)...); + static_assert(sizeof(LinkedFrame) <= kPoolFrameBlockSize); + _tail = new(PoolFrame_alloc()) LinkedFrame(_tail, std::forward(args)...); ++_size; } diff --git a/include/pocketpy/interpreter/iter.hpp b/include/pocketpy/interpreter/iter.hpp index fa673a78..ff916fde 100644 --- a/include/pocketpy/interpreter/iter.hpp +++ b/include/pocketpy/interpreter/iter.hpp @@ -61,7 +61,7 @@ struct Generator{ ~Generator(){ if(lf){ lf->~LinkedFrame(); - pool128_dealloc(lf); + PoolFrame_dealloc(lf); } } }; diff --git a/src/common/memorypool.cpp b/src/common/memorypool.cpp index c5cd37c8..68004397 100644 --- a/src/common/memorypool.cpp +++ b/src/common/memorypool.cpp @@ -1,5 +1,4 @@ #include "pocketpy/common/memorypool.hpp" -#include "pocketpy/common/gil.hpp" #include "pocketpy/common/config.h" #include @@ -273,4 +272,57 @@ void pools_shrink_to_fit() noexcept { std::string pool64_info() noexcept { return "unavailable"; } std::string pool128_info() noexcept { return pool128.info(); } -} \ No newline at end of file +template +struct FixedMemoryPool{ + struct Block{ + char data[BlockSize]; + }; + + static_assert(BlockSize % 4 == 0); + static_assert(sizeof(Block) == BlockSize); + + Block _blocks[BlockCount]; + Block* _free_list[BlockCount]; + Block** _free_list_end; + + FixedMemoryPool() { + _free_list_end = _free_list + BlockCount; + for(int i = 0; i < BlockCount; ++i){ + _free_list[i] = _blocks + i; + } + } + + bool is_valid(void* p){ + return p >= _blocks && p < _blocks + BlockCount; + } + + void* alloc(){ + PK_GLOBAL_SCOPE_LOCK() + if(_free_list_end != _free_list){ + --_free_list_end; + return *_free_list_end; + }else{ + return std::malloc(BlockSize); + } + } + + void dealloc(void* p){ + PK_GLOBAL_SCOPE_LOCK() + if(is_valid(p)){ + *_free_list_end = static_cast(p); + ++_free_list_end; + }else{ + std::free(p); + } + } +}; + +static FixedMemoryPool PoolExpr; +static FixedMemoryPool PoolFrame; +void* PoolExpr_alloc() noexcept { return PoolExpr.alloc(); } +void PoolExpr_dealloc(void* p) noexcept { PoolExpr.dealloc(p); } +void* PoolFrame_alloc() noexcept { return PoolFrame.alloc(); } +void PoolFrame_dealloc(void* p) noexcept { PoolFrame.dealloc(p); } + + +} // namespace pkpy \ No newline at end of file diff --git a/src/interpreter/frame.cpp b/src/interpreter/frame.cpp index 8e22cf4a..a4b90059 100644 --- a/src/interpreter/frame.cpp +++ b/src/interpreter/frame.cpp @@ -107,7 +107,7 @@ namespace pkpy{ LinkedFrame* p = _tail; _tail = p->f_back; p->~LinkedFrame(); - pool128_dealloc(p); + PoolFrame_dealloc(p); --_size; }