diff options
author | John Hodge <tpg@mutabah.net> | 2016-05-21 20:10:25 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-05-21 20:10:25 +0800 |
commit | ad93bc7fda1988e49b4e3a0d85344d7e3dc7df10 (patch) | |
tree | d4fee563f881b5a4ab90dfbb7b40be3486d01349 /src/include | |
parent | be0892fb5cd1442013ee9e761e60294a374f4566 (diff) | |
download | mrust-ad93bc7fda1988e49b4e3a0d85344d7e3dc7df10.tar.gz |
Parse - Updates for better memory efficiency (hopefully)
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/rc_string.hpp | 104 | ||||
-rw-r--r-- | src/include/serialise.hpp | 10 | ||||
-rw-r--r-- | src/include/span.hpp | 11 | ||||
-rw-r--r-- | src/include/tagged_union.hpp | 1 |
4 files changed, 116 insertions, 10 deletions
diff --git a/src/include/rc_string.hpp b/src/include/rc_string.hpp new file mode 100644 index 00000000..6091d748 --- /dev/null +++ b/src/include/rc_string.hpp @@ -0,0 +1,104 @@ +/* + */ +#pragma once + +#include <cstring> +#include <ostream> + +class RcString +{ + unsigned int* m_ptr; + unsigned int m_len; +public: + RcString(): + 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): + RcString(s, ::std::strlen(s)) + { + } + RcString(const ::std::string& s): + RcString(s.data(), s.size()) + { + } + + RcString(const RcString& x): + m_ptr(x.m_ptr), + m_len(x.m_len) + { + *m_ptr += 1; + } + RcString(RcString&& x): + m_ptr(x.m_ptr), + m_len(x.m_len) + { + x.m_ptr = nullptr; + x.m_len = 0; + } + + ~RcString() + { + if(m_ptr) + { + *m_ptr -= 1; + if( *m_ptr == 0 ) + { + delete[] m_ptr; + m_ptr = nullptr; + } + } + } + + RcString& operator=(const RcString& x) + { + if( &x != this ) + { + this->~RcString(); + m_ptr = x.m_ptr; + m_len = x.m_len; + *m_ptr += 1; + } + return *this; + } + RcString& operator=(RcString&& x) + { + if( &x != this ) + { + this->~RcString(); + m_ptr = x.m_ptr; + m_len = x.m_len; + x.m_ptr = nullptr; + x.m_len = 0; + } + return *this; + } + + + 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; + } + friend ::std::ostream& operator<<(::std::ostream& os, const RcString& x) { + return os << x.c_str(); + } +}; diff --git a/src/include/serialise.hpp b/src/include/serialise.hpp index 5556a510..9af379e7 100644 --- a/src/include/serialise.hpp +++ b/src/include/serialise.hpp @@ -26,13 +26,13 @@ class Deserialiser; class DeserialiseFailure: public ::std::runtime_error { - const char *m_fcn; - const char *m_message; + //const char *m_fcn; + //const char *m_message; public: DeserialiseFailure(const char *fcn, const char *message): - ::std::runtime_error("Deserialise failure"), - m_fcn(fcn), - m_message(message) + ::std::runtime_error("Deserialise failure")//, + //m_fcn(fcn), + //m_message(message) {} }; diff --git a/src/include/span.hpp b/src/include/span.hpp index e5dd8ccc..f57fa8a9 100644 --- a/src/include/span.hpp +++ b/src/include/span.hpp @@ -5,9 +5,10 @@ * include/span.hpp * - Spans and error handling */ - #pragma once +#include <rc_string.hpp> + enum ErrorType { E0000, @@ -21,22 +22,22 @@ class Position; struct ProtoSpan { - ::std::string filename; + RcString filename; unsigned int start_line; unsigned int start_ofs; }; struct Span { - ::std::string filename; + RcString filename; unsigned int start_line; unsigned int start_ofs; unsigned int end_line; unsigned int end_ofs; - Span(::std::string filename, unsigned int start_line, unsigned int start_ofs, unsigned int end_line, unsigned int end_ofs): - filename(filename), + Span(RcString filename, unsigned int start_line, unsigned int start_ofs, unsigned int end_line, unsigned int end_ofs): + filename( ::std::move(filename) ), start_line(start_line), start_ofs(start_ofs), end_line(end_line), diff --git a/src/include/tagged_union.hpp b/src/include/tagged_union.hpp index 9f1becc5..2c8d4f31 100644 --- a/src/include/tagged_union.hpp +++ b/src/include/tagged_union.hpp @@ -9,6 +9,7 @@ #define INCLUDED_TAGGED_UNION_H_ //#include "cpp_unpack.h" +#include <cassert> #define TU_CASE_ITEM(src, mod, var, name) mod auto& name = src.as_##var(); (void)&name; #define TU_CASE_BODY(class,var, ...) case class::var: { __VA_ARGS__ } break; |