scoped-list.h 3.07 KB
Newer Older
1 2 3 4
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_UTILS_SCOPED_LIST_H_
#define V8_UTILS_SCOPED_LIST_H_
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

#include <type_traits>
#include <vector>

#include "src/base/logging.h"

namespace v8 {
namespace internal {

template <typename T>
class Vector;

template <typename T>
class ZoneList;

// ScopedList is a scope-lifetime list with a std::vector backing that can be
// re-used between ScopedLists. Note that a ScopedList in an outer scope cannot
// add any entries if there is a ScopedList with the same backing in an inner
// scope.
template <typename T, typename TBacking = T>
27
class V8_NODISCARD ScopedList final {
28 29
  // The backing can either be the same type as the list type, or, for pointers,
  // we additionally allow a void* backing store.
30 31 32 33
  static_assert((std::is_same<TBacking, T>::value) ||
                    (std::is_same<TBacking, void*>::value &&
                     std::is_pointer<T>::value),
                "Incompatible combination of T and TBacking types");
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

 public:
  explicit ScopedList(std::vector<TBacking>* buffer)
      : buffer_(*buffer), start_(buffer->size()), end_(buffer->size()) {}

  ~ScopedList() { Rewind(); }

  void Rewind() {
    DCHECK_EQ(buffer_.size(), end_);
    buffer_.resize(start_);
    end_ = start_;
  }

  void MergeInto(ScopedList* parent) {
    DCHECK_EQ(parent->end_, start_);
    parent->end_ = end_;
    start_ = end_;
    DCHECK_EQ(0, length());
  }

  int length() const { return static_cast<int>(end_ - start_); }

  const T& at(int i) const {
    size_t index = start_ + i;
    DCHECK_LE(start_, index);
    DCHECK_LT(index, buffer_.size());
    return *reinterpret_cast<T*>(&buffer_[index]);
  }

  T& at(int i) {
    size_t index = start_ + i;
    DCHECK_LE(start_, index);
    DCHECK_LT(index, buffer_.size());
    return *reinterpret_cast<T*>(&buffer_[index]);
  }

70 71 72
  Vector<const T> ToConstVector() const {
    T* data = reinterpret_cast<T*>(buffer_.data() + start_);
    return Vector<const T>(data, length());
73 74 75 76 77 78 79 80
  }

  void Add(const T& value) {
    DCHECK_EQ(buffer_.size(), end_);
    buffer_.push_back(value);
    ++end_;
  }

81
  void AddAll(const Vector<const T>& list) {
82 83 84 85 86 87 88 89 90
    DCHECK_EQ(buffer_.size(), end_);
    buffer_.reserve(buffer_.size() + list.length());
    for (int i = 0; i < list.length(); i++) {
      buffer_.push_back(list.at(i));
    }
    end_ += list.length();
  }

  using iterator = T*;
91 92 93
  using const_iterator = const T*;

  inline iterator begin() {
94 95
    return reinterpret_cast<T*>(buffer_.data() + start_);
  }
96 97 98 99 100 101
  inline const_iterator begin() const {
    return reinterpret_cast<T*>(buffer_.data() + start_);
  }

  inline iterator end() { return reinterpret_cast<T*>(buffer_.data() + end_); }
  inline const_iterator end() const {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    return reinterpret_cast<T*>(buffer_.data() + end_);
  }

 private:
  std::vector<TBacking>& buffer_;
  size_t start_;
  size_t end_;
};

template <typename T>
using ScopedPtrList = ScopedList<T*, void*>;

}  // namespace internal
}  // namespace v8

117
#endif  // V8_UTILS_SCOPED_LIST_H_