diff options
| author | John Hodge <tpg@mutabah.net> | 2016-05-21 23:44:51 +0800 | 
|---|---|---|
| committer | John Hodge <tpg@mutabah.net> | 2016-05-21 23:44:51 +0800 | 
| commit | 1b744f3c07e7e34dad26f33d9988c3d34871e062 (patch) | |
| tree | e7747ded50796553d35f27289c67a004867aceef /src | |
| parent | ad93bc7fda1988e49b4e3a0d85344d7e3dc7df10 (diff) | |
| download | mrust-1b744f3c07e7e34dad26f33d9988c3d34871e062.tar.gz | |
All - Cleanup of memory leaks and similar
Diffstat (limited to 'src')
| -rw-r--r-- | src/hir/crate_ptr.hpp | 14 | ||||
| -rw-r--r-- | src/include/rc_string.hpp | 48 | ||||
| -rw-r--r-- | src/macros.cpp | 8 | ||||
| -rw-r--r-- | src/main.cpp | 20 | ||||
| -rw-r--r-- | src/parse/lex.cpp | 4 | ||||
| -rw-r--r-- | src/parse/lex.hpp | 4 | ||||
| -rw-r--r-- | src/parse/token.hpp | 2 | ||||
| -rw-r--r-- | src/rc_string.cpp | 44 | ||||
| -rw-r--r-- | src/span.cpp | 2 | ||||
| -rw-r--r-- | src/types.hpp | 3 | 
10 files changed, 86 insertions, 63 deletions
| diff --git a/src/hir/crate_ptr.hpp b/src/hir/crate_ptr.hpp index ca95074d..df60329e 100644 --- a/src/hir/crate_ptr.hpp +++ b/src/hir/crate_ptr.hpp @@ -14,8 +14,18 @@ class CratePtr  public:      CratePtr();      CratePtr(Crate c); -    CratePtr(CratePtr&&) = default; -    CratePtr& operator=(CratePtr&&) = default; +    CratePtr(CratePtr&& x): +        m_ptr( x.m_ptr ) +    { +        x.m_ptr = nullptr; +    } +    CratePtr& operator=(CratePtr&& x) +    { +        this->~CratePtr(); +        m_ptr = x.m_ptr; +        x.m_ptr = nullptr; +        return *this; +    }      ~CratePtr();  }; diff --git a/src/include/rc_string.hpp b/src/include/rc_string.hpp index 6091d748..4649b36e 100644 --- a/src/include/rc_string.hpp +++ b/src/include/rc_string.hpp @@ -14,16 +14,7 @@ public:          m_ptr(nullptr),          m_len(0)      {} -    RcString(const char* s, unsigned int len): -        m_ptr( new unsigned int[1 + (len+1 + sizeof(unsigned int)-1) / sizeof(unsigned int)] ), -        m_len(len) -    { -        *m_ptr = 1; -        char* data_mut = reinterpret_cast<char*>(m_ptr + 1); -        for(unsigned int j = 0; j < len; j ++ ) -            data_mut[j] = s[j]; -        data_mut[len] = '\0'; -    } +    RcString(const char* s, unsigned int len);      RcString(const char* s):          RcString(s, ::std::strlen(s))      { @@ -37,7 +28,7 @@ public:          m_ptr(x.m_ptr),          m_len(x.m_len)      { -        *m_ptr += 1; +        if( m_ptr ) *m_ptr += 1;      }      RcString(RcString&& x):          m_ptr(x.m_ptr), @@ -47,18 +38,7 @@ public:          x.m_len = 0;      } -    ~RcString() -    { -        if(m_ptr) -        { -            *m_ptr -= 1; -            if( *m_ptr == 0 ) -            { -                delete[] m_ptr; -                m_ptr = nullptr; -            } -        } -    } +    ~RcString();      RcString& operator=(const RcString& x)      { @@ -67,7 +47,7 @@ public:              this->~RcString();              m_ptr = x.m_ptr;              m_len = x.m_len; -            *m_ptr += 1; +            if( m_ptr ) *m_ptr += 1;          }          return *this;      } @@ -86,18 +66,16 @@ public:      const char* c_str() const { -        return reinterpret_cast<const char*>(m_ptr + 1); -    } -    bool operator==(const char* s) const { -        if( m_len == 0 ) -            return *s == '\0'; -        auto m = this->c_str(); -        do { -            if( *m != *s ) -                return false; -        } while( *m++ != '\0' && *s++ != '\0' ); -        return true; +        if( m_len > 0 ) +        { +            return reinterpret_cast<const char*>(m_ptr + 1); +        } +        else +        { +            return ""; +        }      } +    bool operator==(const char* s) const;      friend ::std::ostream& operator<<(::std::ostream& os, const RcString& x) {          return os << x.c_str();      } diff --git a/src/macros.cpp b/src/macros.cpp index 5f4216fd..e0113f85 100644 --- a/src/macros.cpp +++ b/src/macros.cpp @@ -114,7 +114,7 @@ class MacroExpander:  public:
  private:
 -    const ::std::string m_macro_name;
 +    const RcString  m_macro_filename;
      const ::std::string m_crate_name;
      const ::std::vector<MacroRuleEnt>&  m_root_contents;
 @@ -148,8 +148,8 @@ public:      //{
      //    prep_counts();
      //}
 -    MacroExpander(::std::string macro_name, const ::std::vector<MacroRuleEnt>& contents, ParameterMappings mappings, ::std::string crate_name):
 -        m_macro_name( mv$(macro_name) ),
 +    MacroExpander(const ::std::string& macro_name, const ::std::vector<MacroRuleEnt>& contents, ParameterMappings mappings, ::std::string crate_name):
 +        m_macro_filename( FMT("Macro:" << macro_name) ),
          m_crate_name( mv$(crate_name) ),
          m_root_contents(contents),
          m_mappings( mv$(mappings) ),
 @@ -414,7 +414,7 @@ bool Macro_HandlePattern(TTStream& lex, const MacroPatEnt& pat, unsigned int lay  Position MacroExpander::getPosition() const
  {
 -    return Position(FMT("Macro:" << m_macro_name << ":"), 0, m_offsets[0].read_pos);
 +    return Position(m_macro_filename, 0, m_offsets[0].read_pos);
  }
  Token MacroExpander::realGetToken()
  {
 diff --git a/src/main.cpp b/src/main.cpp index b223c534..b6dd9254 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -140,10 +140,11 @@ int main(int argc, char *argv[])              return 0;
          }
 -        ::HIR::CratePtr hir_crate;
 -        CompilePhaseV("HIR Lower", [&]() {
 -            hir_crate = LowerHIR_FromAST(mv$( crate ));
 +        ::HIR::CratePtr hir_crate = CompilePhase< ::HIR::CratePtr>("HIR Lower", [&]() {
 +            return LowerHIR_FromAST(mv$( crate ));
              });
 +        // Deallocate the original crate
 +        crate = ::AST::Crate();
          // Perform type checking on items
          // - Replace type aliases (`type`) into the actual type
 @@ -177,18 +178,7 @@ int main(int argc, char *argv[])          CompilePhaseV("Lower MIR", [&]() {
              //ConvertHIR_MIR(hir_crate);
              });
 -
 -        CompilePhaseV("Output", [&]() {
 -            Dump_Rust( FMT(params.outfile << ".rs").c_str(), crate );
 -            });
 -    
 -        if( params.emit_flags == ProgramParams::EMIT_AST )
 -        {
 -            ::std::ofstream os(params.outfile);
 -            Serialiser_TextTree os_tt(os);
 -            ((Serialiser&)os_tt) << crate;
 -            return 0;
 -        }
 +        
          // Flatten modules into "mangled" set
          //g_cur_phase = "Flatten";
          //AST::Flat flat_crate = Convert_Flatten(crate);
 diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp index dfa8b489..fd6044f4 100644 --- a/src/parse/lex.cpp +++ b/src/parse/lex.cpp @@ -22,8 +22,8 @@  const bool DEBUG_PRINT_TOKENS = false; -Lexer::Lexer(::std::string filename): -    m_path(filename), +Lexer::Lexer(const ::std::string& filename): +    m_path(filename.c_str()),      m_line(1),      m_line_ofs(0),      m_istream(filename.c_str()), diff --git a/src/parse/lex.hpp b/src/parse/lex.hpp index ccd0ab8a..a626a374 100644 --- a/src/parse/lex.hpp +++ b/src/parse/lex.hpp @@ -107,7 +107,7 @@ extern ::std::string& operator+=(::std::string& s, const Codepoint& cp);  class Lexer:      public TokenStream  { -    ::std::string   m_path; +    RcString    m_path;      unsigned int m_line;      unsigned int m_line_ofs; @@ -116,7 +116,7 @@ class Lexer:      char    m_last_char;      Token   m_next_token;   // Used when lexing generated two tokens  public: -    Lexer(::std::string filename); +    Lexer(const ::std::string& filename);      virtual Position getPosition() const override;      virtual Token realGetToken() override; diff --git a/src/parse/token.hpp b/src/parse/token.hpp index e2e1f16f..0a72cec1 100644 --- a/src/parse/token.hpp +++ b/src/parse/token.hpp @@ -26,7 +26,7 @@ public:          line(0),          ofs(0)      {} -    Position(::std::string filename, unsigned int line, unsigned int ofs): +    Position(RcString filename, unsigned int line, unsigned int ofs):          filename(filename),          line(line),          ofs(ofs) diff --git a/src/rc_string.cpp b/src/rc_string.cpp new file mode 100644 index 00000000..59534d28 --- /dev/null +++ b/src/rc_string.cpp @@ -0,0 +1,44 @@ +/* + */ +#include <rc_string.hpp> +#include <cstring> +#include <iostream> + +RcString::RcString(const char* s, unsigned int len): +    m_ptr(nullptr), +    m_len(len) +{ +    if( len > 0 ) +    { +        m_ptr = new unsigned int[1 + (len+1 + sizeof(unsigned int)-1) / sizeof(unsigned int)]; +        *m_ptr = 1; +        char* data_mut = reinterpret_cast<char*>(m_ptr + 1); +        for(unsigned int j = 0; j < len; j ++ ) +            data_mut[j] = s[j]; +        data_mut[len] = '\0'; +    } +} +RcString::~RcString() +{ +    if(m_ptr) +    { +        *m_ptr -= 1; +        //::std::cout << "RcString(\"" << *this << "\") - " << *m_ptr << " refs left" << ::std::endl; +        if( *m_ptr == 0 ) +        { +            delete[] m_ptr; +            m_ptr = nullptr; +        } +    } +} +bool RcString::operator==(const char* s) const +{ +    if( m_len == 0 ) +        return *s == '\0'; +    auto m = this->c_str(); +    do { +        if( *m != *s ) +            return false; +    } while( *m++ != '\0' && *s++ != '\0' ); +    return true; +} diff --git a/src/span.cpp b/src/span.cpp index 8a60b07a..e74fb0d4 100644 --- a/src/span.cpp +++ b/src/span.cpp @@ -20,7 +20,7 @@ Span::Span(const Span& x):  {  }  Span::Span(const Position& pos): -    filename(pos.filename.c_str()), +    filename(pos.filename),      start_line(pos.line),      start_ofs(pos.ofs),      end_line(pos.line), diff --git a/src/types.hpp b/src/types.hpp index 6db45937..72080723 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -46,10 +46,11 @@ struct Type_Function:      Type_Function() {}
      Type_Function(bool is_unsafe, ::std::string abi, ::std::unique_ptr<TypeRef> ret, ::std::vector<TypeRef> args):
          is_unsafe(is_unsafe),
 -        m_abi(abi),
 +        m_abi(mv$(abi)),
          m_rettype(mv$(ret)),
          m_arg_types(mv$(args))
      {}
 +    Type_Function(Type_Function&& other) = default;
      Type_Function(const Type_Function& other);
      Ordering ord(const Type_Function& x) const;
 | 
