diff --git a/hilti/runtime/include/types/interval.h b/hilti/runtime/include/types/interval.h index aa43ad223..ee19afc0d 100644 --- a/hilti/runtime/include/types/interval.h +++ b/hilti/runtime/include/types/interval.h @@ -31,7 +31,7 @@ class Interval { * * @param nsecs interval in nanoseconds. */ - explicit Interval(hilti::rt::integer::safe nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {} + explicit Interval(const hilti::rt::integer::safe& nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {} /** * Constructs an interval from a double value. @@ -73,9 +73,11 @@ class Interval { Interval operator+(const Interval& other) const { return Interval(_nsecs + other._nsecs, NanosecondTag()); } Interval operator-(const Interval& other) const { return Interval(_nsecs - other._nsecs, NanosecondTag()); } - Interval operator*(hilti::rt::integer::safe i) const { return Interval(_nsecs * i, NanosecondTag()); } + Interval operator*(const hilti::rt::integer::safe& i) const { + return Interval(_nsecs * i, NanosecondTag()); + } - Interval operator*(hilti::rt::integer::safe i) const { + Interval operator*(const hilti::rt::integer::safe& i) const { return Interval(_nsecs * i.Ref(), NanosecondTag()); } diff --git a/hilti/runtime/include/types/stream.h b/hilti/runtime/include/types/stream.h index 662ec256f..d2ceb7583 100644 --- a/hilti/runtime/include/types/stream.h +++ b/hilti/runtime/include/types/stream.h @@ -90,17 +90,18 @@ class Chunk { using Array = std::pair>; using Vector = std::vector; - Chunk(Offset o, std::array d, Size n) : _offset(o), _data(std::make_pair(n, d)) {} - Chunk(Offset o, Vector&& d) : _offset(o), _data(std::move(d)) {} - Chunk(Offset o, const View& d); - Chunk(Offset o, const std::string& s); + Chunk(const Offset& o, std::array d, const Size& n) + : _offset(o), _data(std::make_pair(n, d)) {} + Chunk(const Offset& o, Vector&& d) : _offset(o), _data(std::move(d)) {} + Chunk(const Offset& o, const View& d); + Chunk(const Offset& o, const std::string& s); template Chunk(Offset o, std::array d) : Chunk(_fromArray(o, std::move(d))) {} - Chunk(Offset o, const char* d, Size n) : Chunk(_fromArray(o, d, n)) {} + Chunk(const Offset& o, const char* d, const Size& n) : Chunk(_fromArray(o, d, n)) {} // Constructs a gap chunk which signifies empty data. - Chunk(Offset o, size_t len) : _offset(o), _data(Gap{len}) {} + Chunk(const Offset& o, size_t len) : _offset(o), _data(Gap{len}) {} Chunk(const Chunk& other) : _offset(other._offset), _data(other._data) {} Chunk(Chunk&& other) noexcept @@ -121,7 +122,7 @@ class Chunk { Offset offset() const { return _offset; } Offset endOffset() const { return _offset + size(); } bool isGap() const { return std::holds_alternative(_data); } - bool inRange(Offset offset) const { return offset >= _offset && offset < endOffset(); } + bool inRange(const Offset& offset) const { return offset >= _offset && offset < endOffset(); } const Byte* data() const { if ( auto a = std::get_if(&_data) ) @@ -135,7 +136,7 @@ class Chunk { hilti::rt::cannot_be_reached(); } - const Byte* data(Offset offset) const { + const Byte* data(const Offset& offset) const { assert(inRange(offset)); return data() + (offset - _offset).Ref(); } @@ -187,7 +188,7 @@ class Chunk { // only from chain so that it can track any changes. friend class Chain; - void trim(Offset o); + void trim(const Offset& o); // Update offset for current chunk and all others linked from it. void setOffset(Offset o) { @@ -230,7 +231,7 @@ class Chunk { void clearNext() { _next = nullptr; } private: - inline Chunk _fromArray(Offset o, const char* d, Size n) { + inline Chunk _fromArray(const Offset& o, const char* d, const Size& n) { auto ud = reinterpret_cast(d); if ( n <= Chunk::SmallBufferSize ) { @@ -276,7 +277,7 @@ class Chain : public intrusive_ptr::ManagedObject { Size size() const { return (endOffset() - offset()).Ref(); } bool isFrozen() const { return _state == State::Frozen; } bool isValid() const { return _state != State::Invalid; } - bool inRange(Offset o) const { return o >= offset() && o < endOffset(); } + bool inRange(const Offset& o) const { return o >= offset() && o < endOffset(); } Offset offset() const { return _head_offset; } Offset endOffset() const { return _tail ? _tail->endOffset() : _head_offset; } @@ -286,16 +287,16 @@ class Chain : public intrusive_ptr::ManagedObject { // allowing that to be found more quickly. If given, *hint_prev* must be // pointing to a current chunk of the chain, but it's ok if it's // non-helpful for finding the target (i.e., pointing to a later chunk). - const Chunk* findChunk(Offset offset, const Chunk* hint_prev = nullptr) const; - Chunk* findChunk(Offset offset, Chunk* hint_prev = nullptr); + const Chunk* findChunk(const Offset& offset, const Chunk* hint_prev = nullptr) const; + Chunk* findChunk(const Offset& offset, Chunk* hint_prev = nullptr); // Returns a pointer to the byte at a given offset. Returns null if // offset is out of range. See find() for semantics of *hint_prev*. - const Byte* data(Offset offset, Chunk* hint_prev = nullptr) const; + const Byte* data(const Offset& offset, Chunk* hint_prev = nullptr) const; SafeConstIterator begin() const; SafeConstIterator end() const; - SafeConstIterator at(Offset offset) const; + SafeConstIterator at(const Offset& offset) const; UnsafeConstIterator unsafeBegin() const; UnsafeConstIterator unsafeEnd() const; @@ -306,7 +307,7 @@ class Chain : public intrusive_ptr::ManagedObject { void append(std::unique_ptr chunk); void append(Chain&& other); - void trim(Offset offset); + void trim(const Offset& offset); void trim(const SafeConstIterator& i); void trim(const UnsafeConstIterator& i); @@ -424,7 +425,7 @@ class SafeConstIterator { } /** Advances the iterator by a given number of stream. */ - auto& operator+=(integer::safe i) { + auto& operator+=(const integer::safe& i) { _increment(i); return *this; } @@ -443,7 +444,7 @@ class SafeConstIterator { } /** Moves back the iterator by a given number of stream. */ - auto& operator-=(integer::safe i) { + auto& operator-=(const integer::safe& i) { _decrement(i); return *this; } @@ -452,14 +453,14 @@ class SafeConstIterator { auto operator*() const { return _dereference(); } /** Return a new iterator advanced by a given number of bytes. */ - auto operator+(integer::safe i) const { + auto operator+(const integer::safe& i) const { auto x = *this; x._increment(i); return x; } /** Returns a new iterator moved back by a given number of bytes. */ - auto operator-(integer::safe i) const { + auto operator-(const integer::safe& i) const { auto x = *this; x._decrement(i); return x; @@ -577,7 +578,7 @@ class SafeConstIterator { const Chain* chain() const { return _chain.get(); } private: - SafeConstIterator(ConstChainPtr chain, Offset offset, const Chunk* chunk) + SafeConstIterator(ConstChainPtr chain, const Offset& offset, const Chunk* chunk) : _chain(std::move(chain)), _offset(offset), _chunk(chunk) { assert(! isUnset()); } @@ -602,7 +603,7 @@ class SafeConstIterator { throw InvalidIterator("incompatible iterators"); } - void _increment(integer::safe n) { + void _increment(const integer::safe& n) { if ( ! _chain ) throw InvalidIterator("unbound stream iterator"); @@ -618,7 +619,7 @@ class SafeConstIterator { // chunk will be null if we're pointing beyond the end. } - void _decrement(integer::safe n) { + void _decrement(const integer::safe& n) { if ( ! _chain ) throw InvalidIterator("unbound stream iterator"); @@ -747,7 +748,7 @@ class UnsafeConstIterator { } /** Moves back the iterator by a given number of stream. */ - auto& operator-=(integer::safe i) { + auto& operator-=(const integer::safe& i) { _decrement(i); return *this; } @@ -756,14 +757,14 @@ class UnsafeConstIterator { auto operator*() const { return _dereference(); } /** Return a new iterator advanced by a given number of bytes. */ - auto operator+(integer::safe i) const { + auto operator+(const integer::safe& i) const { auto x = *this; x._increment(i); return x; } /** Return a new iterator moved back by a given number of bytes. */ - auto operator-(integer::safe i) const { + auto operator-(const integer::safe& i) const { auto x = *this; x._decrement(i); return x; @@ -857,17 +858,17 @@ class UnsafeConstIterator { const Chain* chain() const { return _chain; } private: - UnsafeConstIterator(const ConstChainPtr& chain, Offset offset, const Chunk* chunk) + UnsafeConstIterator(const ConstChainPtr& chain, const Offset& offset, const Chunk* chunk) : _chain(chain.get()), _offset(offset), _chunk(chunk) { assert(! isUnset()); } - UnsafeConstIterator(const Chain* chain, Offset offset, const Chunk* chunk) + UnsafeConstIterator(const Chain* chain, const Offset& offset, const Chunk* chunk) : _chain(chain), _offset(offset), _chunk(chunk) { assert(! isUnset()); } - void _increment(integer::safe n) { + void _increment(const integer::safe& n) { _offset += n; if ( _chunk && _offset < _chunk->endOffset() ) @@ -876,7 +877,7 @@ class UnsafeConstIterator { _chunk = _chain->findChunk(_offset, _chunk); } - void _decrement(integer::safe n) { + void _decrement(const integer::safe& n) { _offset -= n; if ( _chunk && _offset > _chunk->offset() ) @@ -955,7 +956,7 @@ inline SafeConstIterator Chain::end() const { return {ConstChainPtr(intrusive_ptr::NewRef(), this), endOffset(), _tail}; } -inline SafeConstIterator Chain::at(Offset offset) const { +inline SafeConstIterator Chain::at(const Offset& offset) const { return {ConstChainPtr(intrusive_ptr::NewRef(), this), offset, findChunk(offset)}; } @@ -1205,7 +1206,7 @@ class View { * * @param i the number of stream to advance. */ - View advance(integer::safe i) const { return View(begin() + i, _end); } + View advance(const integer::safe& i) const { return View(begin() + i, _end); } /** * Advances the view to the next, none gap offset. This always advances at least by one byte. @@ -1241,7 +1242,7 @@ class View { * @param from offset of start of subrange, relative to beginning of view * @param to offset of one beyond end of subrange, relative to beginning of view */ - View sub(Offset from, Offset to) const { return View(begin() + from, begin() + to); } + View sub(const Offset& from, const Offset& to) const { return View(begin() + from, begin() + to); } /** * Extracts subrange of stream from the beginning of the view, returned as @@ -1249,10 +1250,10 @@ class View { * * @param to of one beyond end of subrange, relative to beginning of view */ - View sub(Offset to) const { return View(begin(), begin() + to); } + View sub(const Offset& to) const { return View(begin(), begin() + to); } /** Returns an iterator representing an offset inside the view's data */ - SafeConstIterator at(Offset offset) const { return begin() + (offset - begin().offset()); } + SafeConstIterator at(const Offset& offset) const { return begin() + (offset - begin().offset()); } /** * Returns a new view moves the beginning to a subsequent iterator while @@ -1276,7 +1277,7 @@ class View { * at a specified offset from that beginning. The returned view will not * be able to expand any further. */ - View limit(Offset incr) const { return View(begin(), begin() + incr); } + View limit(const Offset& incr) const { return View(begin(), begin() + incr); } /** * Extracts a fixed number of stream from the view. @@ -1442,7 +1443,7 @@ class Stream { * Creates an instance from an existing memory block. The data * will be copied if set, otherwise a gap will be recorded. */ - Stream(const char* d, Size n); + Stream(const char* d, const Size& n); /** * Creates an instance from an existing stream view. * @param d `View` to create the stream from @@ -1566,7 +1567,7 @@ class Stream { * Returns an iterator representing a specific offset. * @param offset offset to use for the created iterator */ - SafeConstIterator at(Offset offset) const { return _chain->at(offset); } + SafeConstIterator at(const Offset& offset) const { return _chain->at(offset); } /** * Returns a view representing the entire instance. diff --git a/hilti/runtime/include/types/time.h b/hilti/runtime/include/types/time.h index df2d9920d..5c5c5a1f9 100644 --- a/hilti/runtime/include/types/time.h +++ b/hilti/runtime/include/types/time.h @@ -30,7 +30,7 @@ class Time { * * @param nsecs interval in nanoseconds. */ - explicit Time(hilti::rt::integer::safe nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {} + explicit Time(const hilti::rt::integer::safe& nsecs, NanosecondTag /*unused*/) : _nsecs(nsecs) {} /** * Constructs a time from a double value. diff --git a/hilti/runtime/src/types/stream.cc b/hilti/runtime/src/types/stream.cc index a3228cf84..4c32b4496 100644 --- a/hilti/runtime/src/types/stream.cc +++ b/hilti/runtime/src/types/stream.cc @@ -9,7 +9,7 @@ using namespace hilti::rt; using namespace hilti::rt::stream; using namespace hilti::rt::stream::detail; -Chunk::Chunk(Offset offset, const View& d) : _offset(offset) { +Chunk::Chunk(const Offset& offset, const View& d) : _offset(offset) { if ( d.size() <= SmallBufferSize ) { std::array a{}; d.copyRaw(a.data()); @@ -23,7 +23,7 @@ Chunk::Chunk(Offset offset, const View& d) : _offset(offset) { } } -Chunk::Chunk(Offset offset, const std::string& s) : _offset(offset) { +Chunk::Chunk(const Offset& offset, const std::string& s) : _offset(offset) { if ( s.size() <= SmallBufferSize ) { std::array a{}; memcpy(a.data(), s.data(), s.size()); @@ -37,7 +37,7 @@ Chunk::Chunk(Offset offset, const std::string& s) : _offset(offset) { } } -void Chunk::trim(Offset o) { +void Chunk::trim(const Offset& o) { assert(o >= _offset && o < _offset + size()); if ( auto a = std::get_if(&_data) ) { auto begin = a->second.data() + (o - _offset).Ref(); @@ -54,7 +54,7 @@ void Chunk::trim(Offset o) { _offset = o; } -const Chunk* Chain::findChunk(Offset offset, const Chunk* hint_prev) const { +const Chunk* Chain::findChunk(const Offset& offset, const Chunk* hint_prev) const { _ensureValid(); const Chunk* c = _head.get(); @@ -71,7 +71,7 @@ const Chunk* Chain::findChunk(Offset offset, const Chunk* hint_prev) const { return c; } -Chunk* Chain::findChunk(Offset offset, Chunk* hint_prev) { +Chunk* Chain::findChunk(const Offset& offset, Chunk* hint_prev) { _ensureValid(); Chunk* c = _head.get(); @@ -88,7 +88,7 @@ Chunk* Chain::findChunk(Offset offset, Chunk* hint_prev) { return c; } -const Byte* Chain::data(Offset offset, Chunk* hint_prev) const { +const Byte* Chain::data(const Offset& offset, Chunk* hint_prev) const { auto c = findChunk(offset, hint_prev); if ( ! c ) throw InvalidIterator("stream iterator outside of valid range"); @@ -126,7 +126,7 @@ void Chain::append(Chain&& other) { other.reset(); } -void Chain::trim(Offset offset) { +void Chain::trim(const Offset& offset) { _ensureValid(); // We search the first chunk that's containing the desired position, @@ -425,7 +425,7 @@ std::optional View::nextBlock(std::optional current) const { Stream::Stream(const Bytes& d) : Stream(Chunk(0, d.str())) {} -Stream::Stream(const char* d, Size n) : Stream() { append(d, n); } +Stream::Stream(const char* d, const Size& n) : Stream() { append(d, n); } void Stream::append(Bytes&& data) { if ( data.isEmpty() )