v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
scoped-list.h
Go to the documentation of this file.
1// Copyright 2020 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_UTILS_SCOPED_LIST_H_
6#define V8_UTILS_SCOPED_LIST_H_
7
8#include <type_traits>
9#include <vector>
10
11#include "src/base/logging.h"
12
13namespace v8 {
14
15namespace base {
16template <typename T>
17class Vector;
18} // namespace base
19
20namespace internal {
21
22template <typename T>
23class ZoneList;
24
25// ScopedList is a scope-lifetime list with a std::vector backing that can be
26// reused between ScopedLists. Note that a ScopedList in an outer scope cannot
27// add any entries if there is a ScopedList with the same backing in an inner
28// scope.
29template <typename T, typename TBacking = T>
31 // The backing can either be the same type as the list type, or, for pointers,
32 // we additionally allow a void* backing store.
33 static_assert((std::is_same<TBacking, T>::value) ||
34 (std::is_same<TBacking, void*>::value &&
35 std::is_pointer<T>::value),
36 "Incompatible combination of T and TBacking types");
37
38 public:
39 explicit ScopedList(std::vector<TBacking>* buffer)
40 : buffer_(*buffer), start_(buffer->size()), end_(buffer->size()) {}
41
42 ~ScopedList() { Rewind(); }
43
44 void Rewind() {
45 DCHECK_EQ(buffer_.size(), end_);
46 buffer_.resize(start_);
47 end_ = start_;
48 }
49
50 void MergeInto(ScopedList* parent) {
51 DCHECK_EQ(parent->end_, start_);
52 parent->end_ = end_;
53 start_ = end_;
54 DCHECK_EQ(0, length());
55 }
56
57 int length() const { return static_cast<int>(end_ - start_); }
58
59 const T& at(int i) const {
60 size_t index = start_ + i;
61 DCHECK_LE(start_, index);
62 DCHECK_LT(index, buffer_.size());
63 return *reinterpret_cast<T*>(&buffer_[index]);
64 }
65
66 T& at(int i) {
67 size_t index = start_ + i;
68 DCHECK_LE(start_, index);
69 DCHECK_LT(index, buffer_.size());
70 return *reinterpret_cast<T*>(&buffer_[index]);
71 }
72
74 T* data = reinterpret_cast<T*>(buffer_.data() + start_);
75 return base::Vector<const T>(data, length());
76 }
77
78 void Add(const T& value) {
79 DCHECK_EQ(buffer_.size(), end_);
80 buffer_.push_back(value);
81 ++end_;
82 }
83
85 DCHECK_EQ(buffer_.size(), end_);
86 buffer_.reserve(buffer_.size() + list.length());
87 for (int i = 0; i < list.length(); i++) {
88 buffer_.push_back(list.at(i));
89 }
90 end_ += list.length();
91 }
92
93 using iterator = T*;
94 using const_iterator = const T*;
95
96 inline iterator begin() {
97 return reinterpret_cast<T*>(buffer_.data() + start_);
98 }
99 inline const_iterator begin() const {
100 return reinterpret_cast<T*>(buffer_.data() + start_);
101 }
102
103 inline iterator end() { return reinterpret_cast<T*>(buffer_.data() + end_); }
104 inline const_iterator end() const {
105 return reinterpret_cast<T*>(buffer_.data() + end_);
106 }
107
108 private:
109 std::vector<TBacking>& buffer_;
110 size_t start_;
111 size_t end_;
112};
113
114template <typename T>
116
117} // namespace internal
118} // namespace v8
119
120#endif // V8_UTILS_SCOPED_LIST_H_
int length() const
Definition vector.h:64
const T & at(size_t index) const
Definition vector.h:81
base::Vector< const T > ToConstVector() const
Definition scoped-list.h:73
void AddAll(base::Vector< const T > list)
Definition scoped-list.h:84
std::vector< TBacking > & buffer_
const_iterator end() const
ScopedList(std::vector< TBacking > *buffer)
Definition scoped-list.h:39
void Add(const T &value)
Definition scoped-list.h:78
const T & at(int i) const
Definition scoped-list.h:59
const_iterator begin() const
Definition scoped-list.h:99
void MergeInto(ScopedList *parent)
Definition scoped-list.h:50
base::OwnedVector< uint8_t > buffer_
Definition assembler.cc:111
uint8_t *const start_
Definition assembler.cc:131
const v8::base::TimeTicks end_
Definition sweeper.cc:54
OptionalOpIndex index
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_NODISCARD
Definition v8config.h:693