This commit is contained in:
BLUELOVETH 2023-07-19 16:02:47 +08:00
parent 864345ccc3
commit 6ee0a169d0
9 changed files with 17 additions and 81 deletions

View File

@ -88,12 +88,12 @@ if(x == 1){
} }
``` ```
## For `shared_ptr<T>` ## For `std::shared_ptr<T>`
Use a `_` suffix to indicate a type is a shared pointer. Use a `_` suffix to indicate a type is a shared pointer.
```cpp ```cpp
typedef shared_ptr<CodeObject> CodeObject_; using CodeObject_ = std::shared_ptr<CodeObject>;
CodeObject_ co = make_shared<CodeObject>(); CodeObject_ co = std::make_shared<CodeObject>();
``` ```

View File

@ -48,9 +48,9 @@ struct CodeBlock {
}; };
struct CodeObject; struct CodeObject;
typedef shared_ptr<CodeObject> CodeObject_;
struct FuncDecl; struct FuncDecl;
using FuncDecl_ = shared_ptr<FuncDecl>; using CodeObject_ = std::shared_ptr<CodeObject>;
using FuncDecl_ = std::shared_ptr<FuncDecl>;
struct CodeObjectSerializer{ struct CodeObjectSerializer{
std::string buffer; std::string buffer;
@ -92,7 +92,7 @@ struct CodeObjectSerializer{
struct CodeObject { struct CodeObject {
shared_ptr<SourceData> src; std::shared_ptr<SourceData> src;
Str name; Str name;
bool is_generator = false; bool is_generator = false;
@ -105,7 +105,7 @@ struct CodeObject {
NameDictInt labels; NameDictInt labels;
std::vector<FuncDecl_> func_decls; std::vector<FuncDecl_> func_decls;
CodeObject(shared_ptr<SourceData> src, const Str& name); CodeObject(std::shared_ptr<SourceData> src, const Str& name);
void _gc_mark() const; void _gc_mark() const;
void write(VM* vm, CodeObjectSerializer& ss) const; void write(VM* vm, CodeObjectSerializer& ss) const;
Str serialize(VM* vm) const; Str serialize(VM* vm) const;

View File

@ -103,7 +103,7 @@ enum Precedence {
enum StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES }; enum StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES };
struct Lexer { struct Lexer {
shared_ptr<SourceData> src; std::shared_ptr<SourceData> src;
const char* token_start; const char* token_start;
const char* curr_char; const char* curr_char;
int current_line = 1; int current_line = 1;
@ -137,7 +137,7 @@ struct Lexer {
void SyntaxError(Str msg){ throw_err("SyntaxError", msg); } void SyntaxError(Str msg){ throw_err("SyntaxError", msg); }
void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); } void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); }
void IndentationError(Str msg){ throw_err("IndentationError", msg); } void IndentationError(Str msg){ throw_err("IndentationError", msg); }
Lexer(shared_ptr<SourceData> src); Lexer(std::shared_ptr<SourceData> src);
std::vector<Token> run(); std::vector<Token> run();
}; };

View File

@ -249,68 +249,4 @@ struct MemoryPool{
inline MemoryPool<64> pool64; inline MemoryPool<64> pool64;
inline MemoryPool<128> pool128; inline MemoryPool<128> pool128;
template <typename T>
struct shared_ptr {
int* counter;
T* _t() const noexcept { return (T*)(counter + 1); }
void _inc_counter() { if(counter) ++(*counter); }
void _dec_counter() { if(counter && --(*counter) == 0) {((T*)(counter + 1))->~T(); pool128.dealloc(counter);} }
public:
shared_ptr() : counter(nullptr) {}
shared_ptr(int* counter) : counter(counter) {}
shared_ptr(const shared_ptr& other) : counter(other.counter) {
_inc_counter();
}
shared_ptr(shared_ptr&& other) noexcept : counter(other.counter) {
other.counter = nullptr;
}
~shared_ptr() { _dec_counter(); }
bool operator==(const shared_ptr& other) const { return counter == other.counter; }
bool operator!=(const shared_ptr& other) const { return counter != other.counter; }
bool operator<(const shared_ptr& other) const { return counter < other.counter; }
bool operator>(const shared_ptr& other) const { return counter > other.counter; }
bool operator<=(const shared_ptr& other) const { return counter <= other.counter; }
bool operator>=(const shared_ptr& other) const { return counter >= other.counter; }
bool operator==(std::nullptr_t) const { return counter == nullptr; }
bool operator!=(std::nullptr_t) const { return counter != nullptr; }
shared_ptr& operator=(const shared_ptr& other) {
_dec_counter();
counter = other.counter;
_inc_counter();
return *this;
}
shared_ptr& operator=(shared_ptr&& other) noexcept {
_dec_counter();
counter = other.counter;
other.counter = nullptr;
return *this;
}
T& operator*() const { return *_t(); }
T* operator->() const { return _t(); }
T* get() const { return _t(); }
int use_count() const {
return counter ? *counter : 0;
}
void reset(){
_dec_counter();
counter = nullptr;
}
};
template <typename T, typename... Args>
shared_ptr<T> make_sp(Args&&... args) {
int* p = (int*)pool128.alloc(sizeof(int) + sizeof(T));
*p = 1;
new(p+1) T(std::forward<Args>(args)...);
return shared_ptr<T>(p);
}
}; // namespace pkpy }; // namespace pkpy

View File

@ -198,7 +198,7 @@ while(!_items[i].first.empty()) { \
}; };
using NameDict = NameDictImpl<PyObject*>; using NameDict = NameDictImpl<PyObject*>;
using NameDict_ = shared_ptr<NameDict>; using NameDict_ = std::shared_ptr<NameDict>;
using NameDictInt = NameDictImpl<int>; using NameDictInt = NameDictImpl<int>;
} // namespace pkpy } // namespace pkpy

View File

@ -2,7 +2,7 @@
namespace pkpy{ namespace pkpy{
CodeObject::CodeObject(shared_ptr<SourceData> src, const Str& name): CodeObject::CodeObject(std::shared_ptr<SourceData> src, const Str& name):
src(src), name(name) {} src(src), name(name) {}
void CodeObject::_gc_mark() const { void CodeObject::_gc_mark() const {

View File

@ -9,14 +9,14 @@ namespace pkpy{
} }
CodeObject_ Compiler::push_global_context(){ CodeObject_ Compiler::push_global_context(){
CodeObject_ co = make_sp<CodeObject>(lexer->src, lexer->src->filename); CodeObject_ co = std::make_shared<CodeObject>(lexer->src, lexer->src->filename);
contexts.push(CodeEmitContext(vm, co, contexts.size())); contexts.push(CodeEmitContext(vm, co, contexts.size()));
return co; return co;
} }
FuncDecl_ Compiler::push_f_context(Str name){ FuncDecl_ Compiler::push_f_context(Str name){
FuncDecl_ decl = make_sp<FuncDecl>(); FuncDecl_ decl = std::make_shared<FuncDecl>();
decl->code = make_sp<CodeObject>(lexer->src, name); decl->code = std::make_shared<CodeObject>(lexer->src, name);
decl->nested = name_scope() == NAME_LOCAL; decl->nested = name_scope() == NAME_LOCAL;
contexts.push(CodeEmitContext(vm, decl->code, contexts.size())); contexts.push(CodeEmitContext(vm, decl->code, contexts.size()));
return decl; return decl;
@ -1008,7 +1008,7 @@ __SUBSCR_END:
this->used = false; this->used = false;
this->unknown_global_scope = unknown_global_scope; this->unknown_global_scope = unknown_global_scope;
this->lexer = std::make_unique<Lexer>( this->lexer = std::make_unique<Lexer>(
make_sp<SourceData>(source, filename, mode) std::make_shared<SourceData>(source, filename, mode)
); );
init_pratt_rules(); init_pratt_rules();
} }

View File

@ -8,7 +8,7 @@ namespace pkpy{
} }
NameDict_ FastLocals::to_namedict(){ NameDict_ FastLocals::to_namedict(){
NameDict_ dict = make_sp<NameDict>(); NameDict_ dict = std::make_shared<NameDict>();
varnames_inv->apply([&](StrName name, int index){ varnames_inv->apply([&](StrName name, int index){
PyObject* value = a[index]; PyObject* value = a[index];
if(value != PY_NULL) dict->set(name, value); if(value != PY_NULL) dict->set(name, value);

View File

@ -449,7 +449,7 @@ static bool is_unicode_Lo_char(uint32_t c) {
throw e; throw e;
} }
Lexer::Lexer(shared_ptr<SourceData> src) { Lexer::Lexer(std::shared_ptr<SourceData> src) {
this->src = src; this->src = src;
this->token_start = src->source.c_str(); this->token_start = src->source.c_str();
this->curr_char = src->source.c_str(); this->curr_char = src->source.c_str();