v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
index-generator.cc
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
6
7#include <optional>
8
9namespace v8 {
10namespace internal {
11
12IndexGenerator::IndexGenerator(size_t size) : first_use_(size > 0) {
13 if (size == 0) return;
14 base::MutexGuard guard(&lock_);
15 ranges_to_split_.emplace(0, size);
16}
17
18std::optional<size_t> IndexGenerator::GetNext() {
19 base::MutexGuard guard(&lock_);
20 if (first_use_) {
21 first_use_ = false;
22 return 0;
23 }
24 if (ranges_to_split_.empty()) return std::nullopt;
25
26 // Split the oldest running range in 2 and return the middle index as
27 // starting point.
28 auto range = ranges_to_split_.front();
29 ranges_to_split_.pop();
30 size_t size = range.second - range.first;
31 size_t mid = range.first + size / 2;
32 // Both sides of the range are added to |ranges_to_split_| so they may be
33 // further split if possible.
34 if (mid - range.first > 1) ranges_to_split_.emplace(range.first, mid);
35 if (range.second - mid > 1) ranges_to_split_.emplace(mid, range.second);
36 return mid;
37}
38
39} // namespace internal
40} // namespace v8
std::queue< std::pair< size_t, size_t > > ranges_to_split_
std::optional< size_t > GetNext()