#include "src/heap/index-generator.h"
#include <optional>
namespace v8 {
namespace internal {
IndexGenerator::IndexGenerator(size_t size) : first_use_(size > 0) {
if (size == 0) return;
base::MutexGuard guard(&lock_);
ranges_to_split_.emplace(0, size);
}
std::optional<size_t> IndexGenerator::GetNext() {
base::MutexGuard guard(&lock_);
if (first_use_) {
first_use_ = false;
return 0;
}
if (ranges_to_split_.empty()) return std::nullopt;
auto range = ranges_to_split_.front();
ranges_to_split_.pop();
size_t size = range.second - range.first;
size_t mid = range.first + size / 2;
if (mid - range.first > 1) ranges_to_split_.emplace(range.first, mid);
if (range.second - mid > 1) ranges_to_split_.emplace(mid, range.second);
return mid;
}
}
}