Debian Codegraph @

js/src/irregexp/util/VectorShim.h

raw
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_UTIL_VECTOR_H_
6 #define V8_UTIL_VECTOR_H_
7
8 #include <algorithm>
9 #include <cstring>
10 #include <iterator>
11 #include <memory>
12
13 #include "js/AllocPolicy.h"
14 #include "js/Utility.h"
15 #include "js/Vector.h"
16
17 namespace v8 {
18 namespace internal {
19
20 //////////////////////////////////////////////////
21
22 // Adapted from:
23 // https://github.com/v8/v8/blob/5f69bbc233c2d1baf149faf869a7901603929914/src/utils/allocation.h#L36-L58
24
25 template <typename T>
26 T* NewArray(size_t size) {
27 static_assert(std::is_pod<T>::value, "");
28 js::AutoEnterOOMUnsafeRegion oomUnsafe;
29 T* result = static_cast<T*>(js_malloc(size * sizeof(T)));
30 if (!result) {
31 oomUnsafe.crash("Irregexp NewArray");
32 }
33 return result;
34 }
35
36 template <typename T>
37 void DeleteArray(T* array) {
38 js_free(array);
39 }
40
41 } // namespace internal
42
43 namespace base {
44
45 //////////////////////////////////////////////////
46
47 // A non-resizable vector containing a pointer and a length.
48 // The Vector may or may not own the pointer, depending on context.
49 // Origin:
50 // https://github.com/v8/v8/blob/5f69bbc233c2d1baf149faf869a7901603929914/src/utils/vector.h#L20-L134
51
52 template <typename T>
53 class Vector {
54 public:
55 constexpr Vector() : start_(nullptr), length_(0) {}
56
57 constexpr Vector(T* data, size_t length) : start_(data), length_(length) {
58 MOZ_ASSERT_IF(length != 0, data != nullptr);
59 }
60
61 static Vector<T> New(size_t length) {
62 return Vector<T>(v8::internal::NewArray<T>(length), length);
63 }
64
65 // Returns a vector using the same backing storage as this one,
66 // spanning from and including 'from', to but not including 'to'.
67 Vector<T> SubVector(size_t from, size_t to) const {
68 MOZ_ASSERT(from <= to);
69 MOZ_ASSERT(to <= length_);
70 return Vector<T>(begin() + from, to - from);
71 }
72
73 // Returns the length of the vector. Only use this if you really need an
74 // integer return value. Use {size()} otherwise.
75 int length() const {
76 MOZ_ASSERT(length_ <= static_cast<size_t>(std::numeric_limits<int>::max()));
77 return static_cast<int>(length_);
78 }
79
80 // Returns the length of the vector as a size_t.
81 constexpr size_t size() const { return length_; }
82
83 // Returns whether or not the vector is empty.
84 constexpr bool empty() const { return length_ == 0; }
85
86 // Access individual vector elements - checks bounds in debug mode.
87 T& operator[](size_t index) const {
88 MOZ_ASSERT(index < length_);
89 return start_[index];
90 }
91
92 const T& at(size_t index) const { return operator[](index); }
93
94 T& first() { return start_[0]; }
95
96 T& last() {
97 MOZ_ASSERT(length_ > 0);
98 return start_[length_ - 1];
99 }
100
101 // Returns a pointer to the start of the data in the vector.
102 constexpr T* begin() const { return start_; }
103
104 // Returns a pointer past the end of the data in the vector.
105 constexpr T* end() const { return start_ + length_; }
106
107 // Returns a clone of this vector with a new backing store.
108 Vector<T> Clone() const {
109 T* result = v8::internal::NewArray<T>(length_);
110 for (size_t i = 0; i < length_; i++) result[i] = start_[i];
111 return Vector<T>(result, length_);
112 }
113
114 void Truncate(size_t length) {
115 MOZ_ASSERT(length <= length_);
116 length_ = length;
117 }
118
119 // Releases the array underlying this vector. Once disposed the
120 // vector is empty.
121 void Dispose() {
122 DeleteArray(start_);
123 start_ = nullptr;
124 length_ = 0;
125 }
126
127 Vector<T> operator+(size_t offset) const {
128 MOZ_ASSERT(offset <= length_);
129 return Vector<T>(start_ + offset, length_ - offset);
130 }
131
132 Vector<T> operator+=(size_t offset) {
133 MOZ_ASSERT(offset <= length_);
134 start_ += offset;
135 length_ -= offset;
136 return *this;
137 }
138
139 // Implicit conversion from Vector<T> to Vector<const T>.
140 inline operator Vector<const T>() const {
141 return Vector<const T>::cast(*this);
142 }
143
144 template <typename S>
145 static constexpr Vector<T> cast(Vector<S> input) {
146 return Vector<T>(reinterpret_cast<T*>(input.begin()),
147 input.length() * sizeof(S) / sizeof(T));
148 }
149
150 bool operator==(const Vector<const T> other) const {
151 if (length_ != other.length_) return false;
152 if (start_ == other.start_) return true;
153 for (size_t i = 0; i < length_; ++i) {
154 if (start_[i] != other.start_[i]) {
155 return false;
156 }
157 }
158 return true;
159 }
160
161 private:
162 T* start_;
163 size_t length_;
164 };
165
166 // The resulting vector does not contain a null-termination byte. If you want
167 // the null byte, use ArrayVector("foo").
168 inline Vector<const char> CStrVector(const char* data) {
169 return Vector<const char>(data, strlen(data));
170 }
171
172 // Construct a Vector from a start pointer and a size.
173 template <typename T>
174 inline constexpr Vector<T> VectorOf(T* start, size_t size) {
175 return {start, size};
176 }
177
178 class DefaultAllocator {
179 public:
180 using Policy = js::SystemAllocPolicy;
181 Policy policy() const { return js::SystemAllocPolicy(); }
182 };
183
184 // SmallVector uses inline storage first, and reallocates when full.
185 // It is basically equivalent to js::Vector, and is implemented
186 // as a thin wrapper.
187 // V8's implementation:
188 // https://github.com/v8/v8/blob/main/src/base/small-vector.h
189 template <typename T, size_t kSize, typename Allocator = DefaultAllocator>
190 class SmallVector {
191 public:
192 explicit SmallVector(const Allocator& allocator = DefaultAllocator())
193 : inner_(allocator.policy()) {}
194 SmallVector(size_t size) { resize_no_init(size); }
195
196 inline bool empty() const { return inner_.empty(); }
197 inline const T& back() const { return inner_.back(); }
198 inline void pop_back() { inner_.popBack(); };
199 template <typename... Args>
200 inline void emplace_back(Args&&... args) {
201 js::AutoEnterOOMUnsafeRegion oomUnsafe;
202 if (!inner_.emplaceBack(args...)) {
203 oomUnsafe.crash("Irregexp SmallVector emplace_back");
204 }
205 };
206 inline size_t size() const { return inner_.length(); }
207 inline const T& at(size_t index) const { return inner_[index]; }
208 T* data() { return inner_.begin(); }
209 T* begin() { return inner_.begin(); }
210
211 T& operator[](size_t index) { return inner_[index]; }
212 const T& operator[](size_t index) const { return inner_[index]; }
213
214 inline void clear() { inner_.clear(); }
215
216 void resize_no_init(size_t new_size) {
217 js::AutoEnterOOMUnsafeRegion oomUnsafe;
218 if (!inner_.resizeUninitialized(new_size)) {
219 oomUnsafe.crash("Irregexp SmallVector resize");
220 }
221 }
222
223 private:
224 js::Vector<T, kSize, typename Allocator::Policy> inner_;
225 };
226
227 } // namespace base
228
229 } // namespace v8
230
231 #endif // V8_UTIL_VECTOR_H_