diff --git a/docs/coding_style_guide.md b/docs/coding_style_guide.md index b920b317..647ba259 100644 --- a/docs/coding_style_guide.md +++ b/docs/coding_style_guide.md @@ -88,12 +88,12 @@ if(x == 1){ } ``` -## For `shared_ptr` +## For `std::shared_ptr` Use a `_` suffix to indicate a type is a shared pointer. ```cpp -typedef shared_ptr CodeObject_; -CodeObject_ co = make_shared(); +using CodeObject_ = std::shared_ptr; +CodeObject_ co = std::make_shared(); ``` diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 78230ee9..9d1dd11c 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -48,9 +48,9 @@ struct CodeBlock { }; struct CodeObject; -typedef shared_ptr CodeObject_; struct FuncDecl; -using FuncDecl_ = shared_ptr; +using CodeObject_ = std::shared_ptr; +using FuncDecl_ = std::shared_ptr; struct CodeObjectSerializer{ std::string buffer; @@ -92,7 +92,7 @@ struct CodeObjectSerializer{ struct CodeObject { - shared_ptr src; + std::shared_ptr src; Str name; bool is_generator = false; @@ -105,7 +105,7 @@ struct CodeObject { NameDictInt labels; std::vector func_decls; - CodeObject(shared_ptr src, const Str& name); + CodeObject(std::shared_ptr src, const Str& name); void _gc_mark() const; void write(VM* vm, CodeObjectSerializer& ss) const; Str serialize(VM* vm) const; diff --git a/include/pocketpy/lexer.h b/include/pocketpy/lexer.h index ce9b63e5..d35d44e8 100644 --- a/include/pocketpy/lexer.h +++ b/include/pocketpy/lexer.h @@ -103,7 +103,7 @@ enum Precedence { enum StringType { NORMAL_STRING, RAW_STRING, F_STRING, NORMAL_BYTES }; struct Lexer { - shared_ptr src; + std::shared_ptr src; const char* token_start; const char* curr_char; int current_line = 1; @@ -137,7 +137,7 @@ struct Lexer { void SyntaxError(Str msg){ throw_err("SyntaxError", msg); } void SyntaxError(){ throw_err("SyntaxError", "invalid syntax"); } void IndentationError(Str msg){ throw_err("IndentationError", msg); } - Lexer(shared_ptr src); + Lexer(std::shared_ptr src); std::vector run(); }; diff --git a/include/pocketpy/memory.h b/include/pocketpy/memory.h index 54a1a905..53a384f3 100644 --- a/include/pocketpy/memory.h +++ b/include/pocketpy/memory.h @@ -249,68 +249,4 @@ struct MemoryPool{ inline MemoryPool<64> pool64; inline MemoryPool<128> pool128; -template -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 -shared_ptr make_sp(Args&&... args) { - int* p = (int*)pool128.alloc(sizeof(int) + sizeof(T)); - *p = 1; - new(p+1) T(std::forward(args)...); - return shared_ptr(p); -} - }; // namespace pkpy diff --git a/include/pocketpy/namedict.h b/include/pocketpy/namedict.h index 9ddf513f..07c54466 100644 --- a/include/pocketpy/namedict.h +++ b/include/pocketpy/namedict.h @@ -198,7 +198,7 @@ while(!_items[i].first.empty()) { \ }; using NameDict = NameDictImpl; -using NameDict_ = shared_ptr; +using NameDict_ = std::shared_ptr; using NameDictInt = NameDictImpl; } // namespace pkpy \ No newline at end of file diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 65ce9286..d40b8c7d 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -2,7 +2,7 @@ namespace pkpy{ - CodeObject::CodeObject(shared_ptr src, const Str& name): + CodeObject::CodeObject(std::shared_ptr src, const Str& name): src(src), name(name) {} void CodeObject::_gc_mark() const { diff --git a/src/compiler.cpp b/src/compiler.cpp index 7dc30fcd..ce4934c0 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -9,14 +9,14 @@ namespace pkpy{ } CodeObject_ Compiler::push_global_context(){ - CodeObject_ co = make_sp(lexer->src, lexer->src->filename); + CodeObject_ co = std::make_shared(lexer->src, lexer->src->filename); contexts.push(CodeEmitContext(vm, co, contexts.size())); return co; } FuncDecl_ Compiler::push_f_context(Str name){ - FuncDecl_ decl = make_sp(); - decl->code = make_sp(lexer->src, name); + FuncDecl_ decl = std::make_shared(); + decl->code = std::make_shared(lexer->src, name); decl->nested = name_scope() == NAME_LOCAL; contexts.push(CodeEmitContext(vm, decl->code, contexts.size())); return decl; @@ -1008,7 +1008,7 @@ __SUBSCR_END: this->used = false; this->unknown_global_scope = unknown_global_scope; this->lexer = std::make_unique( - make_sp(source, filename, mode) + std::make_shared(source, filename, mode) ); init_pratt_rules(); } diff --git a/src/frame.cpp b/src/frame.cpp index 5df2957c..2fafd34e 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -8,7 +8,7 @@ namespace pkpy{ } NameDict_ FastLocals::to_namedict(){ - NameDict_ dict = make_sp(); + NameDict_ dict = std::make_shared(); varnames_inv->apply([&](StrName name, int index){ PyObject* value = a[index]; if(value != PY_NULL) dict->set(name, value); diff --git a/src/lexer.cpp b/src/lexer.cpp index 6bae2488..712d1198 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -449,7 +449,7 @@ static bool is_unicode_Lo_char(uint32_t c) { throw e; } - Lexer::Lexer(shared_ptr src) { + Lexer::Lexer(std::shared_ptr src) { this->src = src; this->token_start = src->source.c_str(); this->curr_char = src->source.c_str();