v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
zone-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_ZONE_ZONE_LIST_H_
6#define V8_ZONE_ZONE_LIST_H_
7
8#include "src/base/logging.h"
9#include "src/zone/zone.h"
10
11namespace v8 {
12
13namespace base {
14template <typename T>
15class Vector;
16} // namespace base
17
18namespace internal {
19
20// ZoneLists are growable lists with constant-time access to the elements.
21// The list itself and all its elements are supposed to be allocated in zone
22// memory. Unlike ZoneVector container, the ZoneList instance has minimal
23// possible size which makes it a good candidate for embedding into other
24// often-allocated zone objects.
25//
26// Note, ZoneLists' elements cannot be deleted individually and the destructor
27// intentionally does not free the backing store. Because of the latter, the
28// ZoneList must not be used outsize of zone memory. Consider using ZoneVector
29// or other containers instead.
30template <typename T>
31class ZoneList final : public ZoneObject {
32 public:
33 // Construct a new ZoneList with the given capacity; the length is
34 // always zero. The capacity must be non-negative.
35 ZoneList(int capacity, Zone* zone) : capacity_(capacity) {
36 DCHECK_GE(capacity, 0);
37 if (capacity > 0) {
38 DCHECK_NOT_NULL(zone);
39 data_ = zone->AllocateArray<T>(capacity);
40 } else {
41 data_ = nullptr;
42 }
43 }
44
45 // Construct a new ZoneList by copying the elements of the given ZoneList.
46 ZoneList(const ZoneList<T>& other, Zone* zone)
47 : ZoneList(other.length(), zone) {
48 AddAll(other, zone);
49 }
50
51 // Construct a new ZoneList by copying the elements of the given vector.
53 : ZoneList(other.length(), zone) {
54 AddAll(other, zone);
55 }
56
57 ZoneList(ZoneList<T>&& other) V8_NOEXCEPT { *this = std::move(other); }
58
59 ZoneList(const ZoneList&) = delete;
60 ZoneList& operator=(const ZoneList&) = delete;
61
62 // The ZoneList objects are usually allocated as a fields in other
63 // zone-allocated objects for which destructors are not called anyway, so
64 // we are not going to clear the memory here as well.
65 ~ZoneList() = default;
66
68 // We don't have a Zone object, so we'll have to drop the data_ array.
69 // If this assert ever fails, consider calling Clear(Zone*) or
70 // DropAndClear() before the move assignment to make it explicit what's
71 // happenning with the lvalue.
73 data_ = other.data_;
74 capacity_ = other.capacity_;
75 length_ = other.length_;
76 other.DropAndClear();
77 return *this;
78 }
79
80 // Returns a reference to the element at index i. This reference is not safe
81 // to use after operations that can change the list's backing store
82 // (e.g. Add).
83 inline T& operator[](int i) const {
84 DCHECK_LE(0, i);
85 DCHECK_GT(static_cast<unsigned>(length_), static_cast<unsigned>(i));
86 return data_[i];
87 }
88 inline T& at(int i) const { return operator[](i); }
89 inline T& last() const { return at(length_ - 1); }
90 inline T& first() const { return at(0); }
91
92 using iterator = T*;
93 inline iterator begin() { return &data_[0]; }
94 inline iterator end() { return &data_[length_]; }
95
96 using const_iterator = const T*;
97 inline const_iterator begin() const { return &data_[0]; }
98 inline const_iterator end() const { return &data_[length_]; }
99
100 V8_INLINE bool is_empty() const { return length_ == 0; }
101 V8_INLINE int length() const { return length_; }
102 V8_INLINE int capacity() const { return capacity_; }
103
105 base::Vector<T> ToVector(int start, int length) const {
107 return base::Vector<T>(&data_[start], std::min(length_ - start, length));
108 }
109
113
114 // Adds a copy of the given 'element' to the end of the list,
115 // expanding the list if necessary.
116 void Add(const T& element, Zone* zone);
117 // Add all the elements from the argument list to this list.
118 void AddAll(const ZoneList<T>& other, Zone* zone);
119 // Add all the elements from the vector to this list.
120 void AddAll(base::Vector<const T> other, Zone* zone);
121 // Inserts the element at the specific index.
122 void InsertAt(int index, const T& element, Zone* zone);
123
124 // Added 'count' elements with the value 'value' and returns a
125 // vector that allows access to the elements. The vector is valid
126 // until the next change is made to this list.
127 base::Vector<T> AddBlock(T value, int count, Zone* zone);
128
129 // Overwrites the element at the specific index.
130 void Set(int index, const T& element);
131
132 // Removes the i'th element without deleting it even if T is a
133 // pointer type; moves all elements above i "down". Returns the
134 // removed element. This function's complexity is linear in the
135 // size of the list.
136 T Remove(int i);
137
138 // Removes the last element without deleting it even if T is a
139 // pointer type. Returns the removed element.
140 V8_INLINE T RemoveLast() { return Remove(length_ - 1); }
141
142 // Clears the list by freeing the storage memory. If you want to keep the
143 // memory, use Rewind(0) instead. Be aware, that even if T is a
144 // pointer type, clearing the list doesn't delete the entries.
145 V8_INLINE void Clear(Zone* zone);
146
147 // Clears the list but unlike Clear(), it doesn't free the storage memory.
148 // It's useful when the whole zone containing the backing store will be
149 // released but the list will be used further.
151 data_ = nullptr;
152 capacity_ = 0;
153 length_ = 0;
154 }
155
156 // Drops all but the first 'pos' elements from the list.
157 V8_INLINE void Rewind(int pos);
158
159 inline bool Contains(const T& elm) const {
160 for (int i = 0; i < length_; i++) {
161 if (data_[i] == elm) return true;
162 }
163 return false;
164 }
165
166 // Iterate through all list entries, starting at index 0.
167 template <class Visitor>
168 void Iterate(Visitor* visitor);
169
170 // Sort all list entries (using QuickSort)
171 template <typename CompareFunction>
172 void Sort(CompareFunction cmp);
173 template <typename CompareFunction>
174 void StableSort(CompareFunction cmp, size_t start, size_t length);
175
176 private:
177 T* data_ = nullptr;
178 int capacity_ = 0;
179 int length_ = 0;
180
181 // Increase the capacity of a full list, and add an element.
182 // List must be full already.
183 void ResizeAdd(const T& element, Zone* zone);
184
185 // Inlined implementation of ResizeAdd, shared by inlined and
186 // non-inlined versions of ResizeAdd.
187 void ResizeAddInternal(const T& element, Zone* zone);
188
189 // Resize the list.
190 void Resize(int new_capacity, Zone* zone);
191};
192
193} // namespace internal
194} // namespace v8
195
196#endif // V8_ZONE_ZONE_LIST_H_
SourcePosition pos
V8_INLINE void Clear(Zone *zone)
V8_INLINE void DropAndClear()
Definition zone-list.h:150
V8_INLINE int capacity() const
Definition zone-list.h:102
void AddAll(const ZoneList< T > &other, Zone *zone)
T & operator[](int i) const
Definition zone-list.h:83
base::Vector< T > ToVector(int start, int length) const
Definition zone-list.h:105
void StableSort(CompareFunction cmp, size_t start, size_t length)
const_iterator end() const
Definition zone-list.h:98
base::Vector< T > AddBlock(T value, int count, Zone *zone)
ZoneList & operator=(const ZoneList &)=delete
void Set(int index, const T &element)
void Sort(CompareFunction cmp)
ZoneList(ZoneList< T > &&other) V8_NOEXCEPT
Definition zone-list.h:57
V8_INLINE int length() const
Definition zone-list.h:101
void Iterate(Visitor *visitor)
V8_INLINE T RemoveLast()
Definition zone-list.h:140
void InsertAt(int index, const T &element, Zone *zone)
void Resize(int new_capacity, Zone *zone)
T & at(int i) const
Definition zone-list.h:88
ZoneList & operator=(ZoneList &&other) V8_NOEXCEPT
Definition zone-list.h:67
V8_INLINE void Rewind(int pos)
V8_INLINE bool is_empty() const
Definition zone-list.h:100
const_iterator begin() const
Definition zone-list.h:97
bool Contains(const T &elm) const
Definition zone-list.h:159
ZoneList(const ZoneList< T > &other, Zone *zone)
Definition zone-list.h:46
ZoneList(base::Vector< const T > other, Zone *zone)
Definition zone-list.h:52
base::Vector< const T > ToConstVector() const
Definition zone-list.h:110
void Add(const T &element, Zone *zone)
void ResizeAdd(const T &element, Zone *zone)
ZoneList(int capacity, Zone *zone)
Definition zone-list.h:35
void ResizeAddInternal(const T &element, Zone *zone)
ZoneList(const ZoneList &)=delete
base::Vector< T > ToVector() const
Definition zone-list.h:104
T * AllocateArray(size_t length)
Definition zone.h:127
int start
#define V8_NOEXCEPT
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NULL(val)
Definition logging.h:491
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK_GT(v1, v2)
Definition logging.h:487
#define V8_INLINE
Definition v8config.h:500