// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_UTILS_VECTOR_H_ #define V8_UTILS_VECTOR_H_ #include #include #include #include #include #include "src/common/checks.h" #include "src/common/globals.h" #include "src/utils/allocation.h" namespace v8 { namespace internal { template class Vector { public: using value_type = T; using iterator = T*; using const_iterator = const T*; constexpr Vector() : start_(nullptr), length_(0) {} constexpr Vector(T* data, size_t length) : start_(data), length_(length) { CONSTEXPR_DCHECK(length == 0 || data != nullptr); } static Vector New(size_t length) { return Vector(NewArray(length), length); } // Returns a vector using the same backing storage as this one, // spanning from and including 'from', to but not including 'to'. Vector SubVector(size_t from, size_t to) const { DCHECK_LE(from, to); DCHECK_LE(to, length_); return Vector(begin() + from, to - from); } // Returns the length of the vector. Only use this if you really need an // integer return value. Use {size()} otherwise. int length() const { DCHECK_GE(std::numeric_limits::max(), length_); return static_cast(length_); } // Returns the length of the vector as a size_t. constexpr size_t size() const { return length_; } // Returns whether or not the vector is empty. constexpr bool empty() const { return length_ == 0; } // Access individual vector elements - checks bounds in debug mode. T& operator[](size_t index) const { DCHECK_LT(index, length_); return start_[index]; } const T& at(size_t index) const { return operator[](index); } T& first() { return start_[0]; } T& last() { DCHECK_LT(0, length_); return start_[length_ - 1]; } // Returns a pointer to the start of the data in the vector. constexpr T* begin() const { return start_; } // For consistency with other containers, do also provide a {data} accessor. constexpr T* data() const { return start_; } // Returns a pointer past the end of the data in the vector. constexpr T* end() const { return start_ + length_; } // Returns a clone of this vector with a new backing store. Vector Clone() const { T* result = NewArray(length_); for (size_t i = 0; i < length_; i++) result[i] = start_[i]; return Vector(result, length_); } void Truncate(size_t length) { DCHECK(length <= length_); length_ = length; } // Releases the array underlying this vector. Once disposed the // vector is empty. void Dispose() { DeleteArray(start_); start_ = nullptr; length_ = 0; } Vector operator+(size_t offset) { DCHECK_LE(offset, length_); return Vector(start_ + offset, length_ - offset); } Vector operator+=(size_t offset) { DCHECK_LE(offset, length_); start_ += offset; length_ -= offset; return *this; } // Implicit conversion from Vector to Vector. operator Vector() const { return {start_, length_}; } template static Vector cast(Vector input) { // Casting is potentially dangerous, so be really restrictive here. This // might be lifted once we have use cases for that. STATIC_ASSERT(std::is_pod::value); STATIC_ASSERT(std::is_pod::value); DCHECK_EQ(0, (input.size() * sizeof(S)) % sizeof(T)); DCHECK_EQ(0, reinterpret_cast(input.begin()) % alignof(T)); return Vector(reinterpret_cast(input.begin()), input.size() * sizeof(S) / sizeof(T)); } bool operator==(const Vector other) const { return std::equal(begin(), end(), other.begin(), other.end()); } bool operator!=(const Vector other) const { return !operator==(other); } private: T* start_; size_t length_; }; template class ScopedVector : public Vector { public: explicit ScopedVector(size_t length) : Vector(NewArray(length), length) {} ~ScopedVector() { DeleteArray(this->begin()); } private: DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); }; template class OwnedVector { public: MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(OwnedVector); OwnedVector(std::unique_ptr data, size_t length) : data_(std::move(data)), length_(length) { DCHECK_IMPLIES(length_ > 0, data_ != nullptr); } // Implicit conversion from {OwnedVector} to {OwnedVector}, instantiable // if {std::unique_ptr} can be converted to {std::unique_ptr}. // Can be used to convert {OwnedVector} to {OwnedVector}. template , std::unique_ptr>::value>::type> OwnedVector(OwnedVector&& other) : data_(std::move(other.data_)), length_(other.length_) { STATIC_ASSERT(sizeof(U) == sizeof(T)); other.length_ = 0; } // Returns the length of the vector as a size_t. constexpr size_t size() const { return length_; } // Returns whether or not the vector is empty. constexpr bool empty() const { return length_ == 0; } // Returns the pointer to the start of the data in the vector. T* start() const { DCHECK_IMPLIES(length_ > 0, data_ != nullptr); return data_.get(); } constexpr T* begin() const { return start(); } constexpr T* end() const { return start() + size(); } // Access individual vector elements - checks bounds in debug mode. T& operator[](size_t index) const { DCHECK_LT(index, length_); return data_[index]; } // Returns a {Vector} view of the data in this vector. Vector as_vector() const { return Vector(start(), size()); } // Releases the backing data from this vector and transfers ownership to the // caller. This vector will be empty afterwards. std::unique_ptr ReleaseData() { length_ = 0; return std::move(data_); } // Allocates a new vector of the specified size via the default allocator. // Elements in the new vector are value-initialized. static OwnedVector New(size_t size) { if (size == 0) return {}; return OwnedVector(std::make_unique(size), size); } // Allocates a new vector of the specified size via the default allocator. // Elements in the new vector are default-initialized. static OwnedVector NewForOverwrite(size_t size) { if (size == 0) return {}; // TODO(v8): Use {std::make_unique_for_overwrite} once we allow C++20. return OwnedVector(std::unique_ptr(new T[size]), size); } // Allocates a new vector containing the specified collection of values. // {Iterator} is the common type of {std::begin} and {std::end} called on a // {const U&}. This function is only instantiable if that type exists. template ())), decltype(std::end(std::declval()))>::type> static OwnedVector Of(const U& collection) { Iterator begin = std::begin(collection); Iterator end = std::end(collection); using non_const_t = typename std::remove_const::type; auto vec = OwnedVector::NewForOverwrite(std::distance(begin, end)); std::copy(begin, end, vec.start()); return vec; } bool operator==(std::nullptr_t) const { return data_ == nullptr; } bool operator!=(std::nullptr_t) const { return data_ != nullptr; } private: template friend class OwnedVector; std::unique_ptr data_; size_t length_ = 0; }; // The vectors returned by {StaticCharVector}, {CStrVector}, or {OneByteVector} // do not contain a null-termination byte. If you want the null byte, use // {ArrayVector}. // Known length, constexpr. template constexpr Vector StaticCharVector(const char (&array)[N]) { return {array, N - 1}; } // Unknown length, not constexpr. inline Vector CStrVector(const char* data) { return {data, strlen(data)}; } // OneByteVector is never constexpr because the data pointer is // {reinterpret_cast}ed. inline Vector OneByteVector(const char* data, size_t length) { return {reinterpret_cast(data), length}; } inline Vector OneByteVector(const char* data) { return OneByteVector(data, strlen(data)); } template Vector StaticOneByteVector(const char (&array)[N]) { return OneByteVector(array, N - 1); } // For string literals, ArrayVector("foo") returns a vector ['f', 'o', 'o', \0] // with length 4 and null-termination. // If you want ['f', 'o', 'o'], use CStrVector("foo"). template inline constexpr Vector ArrayVector(T (&arr)[N]) { return {arr, N}; } // Construct a Vector from a start pointer and a size. template inline constexpr Vector VectorOf(T* start, size_t size) { return {start, size}; } // Construct a Vector from anything providing a {data()} and {size()} accessor. template inline constexpr auto VectorOf(Container&& c) -> decltype(VectorOf(c.data(), c.size())) { return VectorOf(c.data(), c.size()); } // Construct a Vector from an initializer list. The vector can obviously only be // used as long as the initializer list is live. Valid uses include direct use // in parameter lists: F(VectorOf({1, 2, 3})); template inline constexpr Vector VectorOf(std::initializer_list list) { return VectorOf(list.begin(), list.size()); } template class EmbeddedVector : public Vector { public: EmbeddedVector() : Vector(buffer_, kSize) {} explicit EmbeddedVector(const T& initial_value) : Vector(buffer_, kSize) { std::fill_n(buffer_, kSize, initial_value); } private: T buffer_[kSize]; DISALLOW_COPY_AND_ASSIGN(EmbeddedVector); }; } // namespace internal } // namespace v8 #endif // V8_UTILS_VECTOR_H_