Commit 9cdb251c authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

Switch more {int} to {size_t} in Vector

The {Vector} class stores the size in a {size_t} since a while, but
many accessors and constructors still accept an {int}. This CL removes
all {int} uses except for the explicit {length()} accessor. It also
adds a comment to avoid this accessor if possible.
The {StrLength} function still has several users outside of vector.h,
which I plan to remove in a follow-up CL.

R=mstarzinger@chromium.org

Bug: v8:8834
Change-Id: I33c5b0e8b8b2cb3531716c1d99e4516a13d6ba1f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1578480Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60950}
parent 0820ee1a
......@@ -22,14 +22,16 @@ class Vector {
public:
constexpr Vector() : start_(nullptr), length_(0) {}
Vector(T* data, size_t length) : start_(data), length_(length) {
constexpr Vector(T* data, size_t length) : start_(data), length_(length) {
#ifdef V8_CAN_HAVE_DCHECK_IN_CONSTEXPR
DCHECK(length == 0 || data != nullptr);
#endif
}
template <int N>
template <size_t N>
explicit constexpr Vector(T (&arr)[N]) : start_(arr), length_(N) {}
static Vector<T> New(int length) {
static Vector<T> New(size_t length) {
return Vector<T>(NewArray<T>(length), length);
}
......@@ -41,9 +43,10 @@ class Vector {
return Vector<T>(start() + from, to - from);
}
// Returns the length of the vector.
// Returns the length of the vector. Only use this if you really need an
// integer return value. Use {size()} otherwise.
int length() const {
DCHECK(length_ <= static_cast<size_t>(std::numeric_limits<int>::max()));
DCHECK_GE(std::numeric_limits<int>::max(), length_);
return static_cast<int>(length_);
}
......@@ -178,7 +181,8 @@ class Vector {
template <typename T>
class ScopedVector : public Vector<T> {
public:
explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
explicit ScopedVector(size_t length)
: Vector<T>(NewArray<T>(length), length) {}
~ScopedVector() {
DeleteArray(this->start());
}
......@@ -263,6 +267,7 @@ class OwnedVector {
size_t length_ = 0;
};
// TODO(clemensh): Remove this; replace all uses by {strlen}.
inline int StrLength(const char* string) {
size_t length = strlen(string);
DCHECK(length == static_cast<size_t>(static_cast<int>(length)));
......@@ -275,29 +280,28 @@ constexpr Vector<const uint8_t> StaticCharVector(const char (&array)[N]) {
}
inline Vector<const char> CStrVector(const char* data) {
return Vector<const char>(data, StrLength(data));
return Vector<const char>(data, strlen(data));
}
inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
inline Vector<const uint8_t> OneByteVector(const char* data, size_t length) {
return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
}
inline Vector<const uint8_t> OneByteVector(const char* data) {
return OneByteVector(data, StrLength(data));
return OneByteVector(data, strlen(data));
}
inline Vector<char> MutableCStrVector(char* data) {
return Vector<char>(data, StrLength(data));
return Vector<char>(data, strlen(data));
}
inline Vector<char> MutableCStrVector(char* data, int max) {
int length = StrLength(data);
return Vector<char>(data, (length < max) ? length : max);
inline Vector<char> MutableCStrVector(char* data, size_t max) {
return Vector<char>(data, strnlen(data, max));
}
template <typename T, int N>
template <typename T, size_t N>
inline constexpr Vector<T> ArrayVector(T (&arr)[N]) {
return Vector<T>(arr);
return Vector<T>{arr, N};
}
// Construct a Vector from a start pointer and a size.
......@@ -313,7 +317,7 @@ inline constexpr auto VectorOf(Container&& c)
return VectorOf(c.data(), c.size());
}
template <typename T, int kSize>
template <typename T, size_t kSize>
class EmbeddedVector : public Vector<T> {
public:
EmbeddedVector() : Vector<T>(buffer_, kSize) {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment