V8 Project
vector.h
Go to the documentation of this file.
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_VECTOR_H_
6 #define V8_VECTOR_H_
7 
8 #include <string.h>
9 #include <algorithm>
10 
11 #include "src/allocation.h"
12 #include "src/checks.h"
13 #include "src/globals.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 
19 template <typename T>
20 class Vector {
21  public:
22  Vector() : start_(NULL), length_(0) {}
23  Vector(T* data, int length) : start_(data), length_(length) {
24  DCHECK(length == 0 || (length > 0 && data != NULL));
25  }
26 
27  static Vector<T> New(int length) {
28  return Vector<T>(NewArray<T>(length), length);
29  }
30 
31  // Returns a vector using the same backing storage as this one,
32  // spanning from and including 'from', to but not including 'to'.
33  Vector<T> SubVector(int from, int to) {
35  SLOW_DCHECK(from < to);
36  DCHECK(0 <= from);
37  return Vector<T>(start() + from, to - from);
38  }
39 
40  // Returns the length of the vector.
41  int length() const { return length_; }
42 
43  // Returns whether or not the vector is empty.
44  bool is_empty() const { return length_ == 0; }
45 
46  // Returns the pointer to the start of the data in the vector.
47  T* start() const { return start_; }
48 
49  // Access individual vector elements - checks bounds in debug mode.
50  T& operator[](int index) const {
51  DCHECK(0 <= index && index < length_);
52  return start_[index];
53  }
54 
55  const T& at(int index) const { return operator[](index); }
56 
57  T& first() { return start_[0]; }
58 
59  T& last() { return start_[length_ - 1]; }
60 
61  // Returns a clone of this vector with a new backing store.
62  Vector<T> Clone() const {
63  T* result = NewArray<T>(length_);
64  for (int i = 0; i < length_; i++) result[i] = start_[i];
65  return Vector<T>(result, length_);
66  }
67 
68  void Sort(int (*cmp)(const T*, const T*)) {
69  std::sort(start(), start() + length(), RawComparer(cmp));
70  }
71 
72  void Sort() {
73  std::sort(start(), start() + length());
74  }
75 
76  void Truncate(int length) {
77  DCHECK(length <= length_);
78  length_ = length;
79  }
80 
81  // Releases the array underlying this vector. Once disposed the
82  // vector is empty.
83  void Dispose() {
85  start_ = NULL;
86  length_ = 0;
87  }
88 
89  inline Vector<T> operator+(int offset) {
90  DCHECK(offset < length_);
91  return Vector<T>(start_ + offset, length_ - offset);
92  }
93 
94  // Factory method for creating empty vectors.
95  static Vector<T> empty() { return Vector<T>(NULL, 0); }
96 
97  template<typename S>
98  static Vector<T> cast(Vector<S> input) {
99  return Vector<T>(reinterpret_cast<T*>(input.start()),
100  input.length() * sizeof(S) / sizeof(T));
101  }
102 
103  bool operator==(const Vector<T>& other) const {
104  if (length_ != other.length_) return false;
105  if (start_ == other.start_) return true;
106  for (int i = 0; i < length_; ++i) {
107  if (start_[i] != other.start_[i]) {
108  return false;
109  }
110  }
111  return true;
112  }
113 
114  protected:
115  void set_start(T* start) { start_ = start; }
116 
117  private:
119  int length_;
120 
121  class RawComparer {
122  public:
123  explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {}
124  bool operator()(const T& a, const T& b) {
125  return cmp_(&a, &b) < 0;
126  }
127 
128  private:
129  int (*cmp_)(const T*, const T*);
130  };
131 };
132 
133 
134 template <typename T>
135 class ScopedVector : public Vector<T> {
136  public:
137  explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
139  DeleteArray(this->start());
140  }
141 
142  private:
144 };
145 
146 
147 inline int StrLength(const char* string) {
148  size_t length = strlen(string);
149  DCHECK(length == static_cast<size_t>(static_cast<int>(length)));
150  return static_cast<int>(length);
151 }
152 
153 
154 #define STATIC_CHAR_VECTOR(x) \
155  v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
156  arraysize(x) - 1)
157 
158 inline Vector<const char> CStrVector(const char* data) {
159  return Vector<const char>(data, StrLength(data));
160 }
161 
162 inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
163  return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
164 }
165 
166 inline Vector<const uint8_t> OneByteVector(const char* data) {
167  return OneByteVector(data, StrLength(data));
168 }
169 
170 inline Vector<char> MutableCStrVector(char* data) {
171  return Vector<char>(data, StrLength(data));
172 }
173 
174 inline Vector<char> MutableCStrVector(char* data, int max) {
175  int length = StrLength(data);
176  return Vector<char>(data, (length < max) ? length : max);
177 }
178 
179 
180 } } // namespace v8::internal
181 
182 #endif // V8_VECTOR_H_
#define SLOW_DCHECK(condition)
Definition: checks.h:30
ScopedVector(int length)
Definition: vector.h:137
DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector)
RawComparer(int(*cmp)(const T *, const T *))
Definition: vector.h:123
bool operator()(const T &a, const T &b)
Definition: vector.h:124
int(* cmp_)(const T *, const T *)
Definition: vector.h:129
void Sort(int(*cmp)(const T *, const T *))
Definition: vector.h:68
static Vector< T > cast(Vector< S > input)
Definition: vector.h:98
Vector< T > operator+(int offset)
Definition: vector.h:89
Vector< T > Clone() const
Definition: vector.h:62
Vector< T > SubVector(int from, int to)
Definition: vector.h:33
static Vector< T > empty()
Definition: vector.h:95
T * start() const
Definition: vector.h:47
bool operator==(const Vector< T > &other) const
Definition: vector.h:103
T & operator[](int index) const
Definition: vector.h:50
int length() const
Definition: vector.h:41
void set_start(T *start)
Definition: vector.h:115
Vector(T *data, int length)
Definition: vector.h:23
bool is_empty() const
Definition: vector.h:44
void Truncate(int length)
Definition: vector.h:76
const T & at(int index) const
Definition: vector.h:55
static Vector< T > New(int length)
Definition: vector.h:27
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes to(mksnapshot only)") DEFINE_STRING(raw_context_file
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
Vector< const char > CStrVector(const char *data)
Definition: vector.h:158
void DeleteArray(T *array)
Definition: allocation.h:68
T * NewArray(size_t size)
Definition: allocation.h:60
Vector< char > MutableCStrVector(char *data)
Definition: vector.h:170
Vector< const uint8_t > OneByteVector(const char *data, int length)
Definition: vector.h:162
int StrLength(const char *string)
Definition: vector.h:147
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
#define T(name, string, precedence)
Definition: token.cc:25