v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
zone-list-inl.h
Go to the documentation of this file.
1// Copyright 2017 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_INL_H_
6#define V8_ZONE_ZONE_LIST_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
11#include "src/base/macros.h"
13#include "src/utils/memcopy.h"
14
15namespace v8 {
16namespace internal {
17
18template <typename T>
19void ZoneList<T>::Add(const T& element, Zone* zone) {
20 if (length_ < capacity_) {
21 data_[length_++] = element;
22 } else {
23 ZoneList<T>::ResizeAdd(element, zone);
24 }
25}
26
27template <typename T>
28void ZoneList<T>::AddAll(const ZoneList<T>& other, Zone* zone) {
29 AddAll(other.ToVector(), zone);
30}
31
32template <typename T>
34 int length = other.length();
35 if (length == 0) return;
36
37 int result_length = length_ + length;
38 if (capacity_ < result_length) Resize(result_length, zone);
39 if (std::is_trivially_copyable<T>::value) {
40 memcpy(&data_[length_], other.begin(), sizeof(T) * length);
41 } else {
42 std::copy(other.begin(), other.end(), &data_[length_]);
43 }
44 length_ = result_length;
45}
46
47// Use two layers of inlining so that the non-inlined function can
48// use the same implementation as the inlined version.
49template <typename T>
50void ZoneList<T>::ResizeAdd(const T& element, Zone* zone) {
51 ResizeAddInternal(element, zone);
52}
53
54template <typename T>
55void ZoneList<T>::ResizeAddInternal(const T& element, Zone* zone) {
56 DCHECK(length_ >= capacity_);
57 // Grow the list capacity by 100%, but make sure to let it grow
58 // even when the capacity is zero (possible initial case).
59 int new_capacity = 1 + 2 * capacity_;
60 // Since the element reference could be an element of the list, copy
61 // it out of the old backing storage before resizing.
62 T temp = element;
63 Resize(new_capacity, zone);
64 data_[length_++] = temp;
65}
66
67template <typename T>
68void ZoneList<T>::Resize(int new_capacity, Zone* zone) {
69 DCHECK_LE(length_, new_capacity);
70 T* new_data = zone->AllocateArray<T>(new_capacity);
71 if (length_ > 0) {
72 if (std::is_trivially_copyable<T>::value) {
73 MemCopy(new_data, data_, length_ * sizeof(T));
74 } else {
75 std::copy(&data_[0], &data_[length_], &new_data[0]);
76 }
77 }
78 if (data_) zone->DeleteArray<T>(data_, capacity_);
79 data_ = new_data;
80 capacity_ = new_capacity;
81}
82
83template <typename T>
85 int start = length_;
86 for (int i = 0; i < count; i++) Add(value, zone);
88}
89
90template <typename T>
91void ZoneList<T>::Set(int index, const T& elm) {
92 DCHECK(index >= 0 && index <= length_);
93 data_[index] = elm;
94}
95
96template <typename T>
97void ZoneList<T>::InsertAt(int index, const T& elm, Zone* zone) {
98 DCHECK(index >= 0 && index <= length_);
99 Add(elm, zone);
100 for (int i = length_ - 1; i > index; --i) {
101 data_[i] = data_[i - 1];
102 }
103 data_[index] = elm;
104}
105
106template <typename T>
108 T element = at(i);
109 length_--;
110 while (i < length_) {
111 data_[i] = data_[i + 1];
112 i++;
113 }
114 return element;
115}
117template <typename T>
119 if (data_) zone->DeleteArray<T>(data_, capacity_);
120 DropAndClear();
121}
123template <typename T>
125 DCHECK(0 <= pos && pos <= length_);
126 length_ = pos;
128
129template <typename T>
130template <class Visitor>
131void ZoneList<T>::Iterate(Visitor* visitor) {
132 for (int i = 0; i < length_; i++) visitor->Apply(&data_[i]);
133}
134
135template <typename T>
136template <typename CompareFunction>
137void ZoneList<T>::Sort(CompareFunction cmp) {
138 std::sort(begin(), end(),
139 [cmp](const T& a, const T& b) { return cmp(&a, &b) < 0; });
140#ifdef DEBUG
141 for (int i = 1; i < length_; i++) {
142 DCHECK_LE(cmp(&data_[i - 1], &data_[i]), 0);
143 }
144#endif
146
147template <typename T>
148template <typename CompareFunction>
149void ZoneList<T>::StableSort(CompareFunction cmp, size_t s, size_t l) {
150 std::stable_sort(begin() + s, begin() + s + l,
151 [cmp](const T& a, const T& b) { return cmp(&a, &b) < 0; });
152#ifdef DEBUG
153 for (size_t i = s + 1; i < l; i++) {
154 DCHECK_LE(cmp(&data_[i - 1], &data_[i]), 0);
155 }
156#endif
158
159} // namespace internal
160} // namespace v8
161
162#endif // V8_ZONE_ZONE_LIST_INL_H_
uint8_t data_[MAX_STACK_LENGTH]
SourcePosition pos
V8_INLINE void Clear(Zone *zone)
void AddAll(const ZoneList< T > &other, Zone *zone)
void StableSort(CompareFunction cmp, size_t start, size_t length)
base::Vector< T > AddBlock(T value, int count, Zone *zone)
void Set(int index, const T &element)
void Sort(CompareFunction cmp)
void Iterate(Visitor *visitor)
void InsertAt(int index, const T &element, Zone *zone)
void Resize(int new_capacity, Zone *zone)
V8_INLINE void Rewind(int pos)
void Add(const T &element, Zone *zone)
void ResizeAdd(const T &element, Zone *zone)
void ResizeAddInternal(const T &element, Zone *zone)
T * AllocateArray(size_t length)
Definition zone.h:127
void DeleteArray(T *pointer, size_t length)
Definition zone.h:171
int start
uint32_t count
int end
const int length_
Definition mul-fft.cc:473
void MemCopy(void *dest, const void *src, size_t size)
Definition memcopy.h:124
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK(condition)
Definition logging.h:482