zone-handle-set.h 7.19 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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.

#ifndef V8_ZONE_ZONE_HANDLE_SET_H_
#define V8_ZONE_ZONE_HANDLE_SET_H_

8
#include "src/handles/handles.h"
9
#include "src/zone/zone-containers.h"
10 11 12 13 14 15 16 17 18 19
#include "src/zone/zone.h"

namespace v8 {
namespace internal {

template <typename T>
class ZoneHandleSet final {
 public:
  ZoneHandleSet() : data_(kEmptyTag) {}
  explicit ZoneHandleSet(Handle<T> handle)
20 21
      : data_(handle.address() | kSingletonTag) {
    DCHECK(IsAligned(handle.address(), kPointerAlignment));
22 23 24 25 26 27 28
  }

  bool is_empty() const { return data_ == kEmptyTag; }

  size_t size() const {
    if ((data_ & kTagMask) == kEmptyTag) return 0;
    if ((data_ & kTagMask) == kSingletonTag) return 1;
29
    return list()->size();
30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }

  Handle<T> at(size_t i) const {
    DCHECK_NE(kEmptyTag, data_ & kTagMask);
    if ((data_ & kTagMask) == kSingletonTag) {
      DCHECK_EQ(0u, i);
      return Handle<T>(singleton());
    }
    return Handle<T>(list()->at(static_cast<int>(i)));
  }

  Handle<T> operator[](size_t i) const { return at(i); }

  void insert(Handle<T> handle, Zone* zone) {
44 45
    Address* const value = reinterpret_cast<Address*>(handle.address());
    DCHECK(IsAligned(reinterpret_cast<Address>(value), kPointerAlignment));
46
    if ((data_ & kTagMask) == kEmptyTag) {
47
      data_ = reinterpret_cast<Address>(value) | kSingletonTag;
48 49
    } else if ((data_ & kTagMask) == kSingletonTag) {
      if (singleton() == value) return;
50
      List* list = zone->New<List>(zone);
51
      if (singleton() < value) {
52 53
        list->push_back(singleton());
        list->push_back(value);
54
      } else {
55 56
        list->push_back(value);
        list->push_back(singleton());
57
      }
58 59
      DCHECK(IsAligned(reinterpret_cast<Address>(list), kPointerAlignment));
      data_ = reinterpret_cast<Address>(list) | kListTag;
60 61 62
    } else {
      DCHECK_EQ(kListTag, data_ & kTagMask);
      List const* const old_list = list();
63
      for (size_t i = 0; i < old_list->size(); ++i) {
64 65 66
        if (old_list->at(i) == value) return;
        if (old_list->at(i) > value) break;
      }
67
      List* new_list = zone->New<List>(zone);
68 69 70
      new_list->reserve(old_list->size() + 1);
      size_t i = 0;
      for (; i < old_list->size(); ++i) {
71
        if (old_list->at(i) > value) break;
72
        new_list->push_back(old_list->at(i));
73
      }
74 75 76
      new_list->push_back(value);
      for (; i < old_list->size(); ++i) {
        new_list->push_back(old_list->at(i));
77
      }
78
      DCHECK_EQ(old_list->size() + 1, new_list->size());
79 80
      DCHECK(IsAligned(reinterpret_cast<Address>(new_list), kPointerAlignment));
      data_ = reinterpret_cast<Address>(new_list) | kListTag;
81 82 83 84 85 86 87 88 89
    }
  }

  bool contains(ZoneHandleSet<T> const& other) const {
    if (data_ == other.data_) return true;
    if (data_ == kEmptyTag) return false;
    if (other.data_ == kEmptyTag) return true;
    if ((data_ & kTagMask) == kSingletonTag) return false;
    DCHECK_EQ(kListTag, data_ & kTagMask);
90
    List const* cached_list = list();
91
    if ((other.data_ & kTagMask) == kSingletonTag) {
92 93
      return std::find(cached_list->begin(), cached_list->end(),
                       other.singleton()) != cached_list->end();
94 95 96
    }
    DCHECK_EQ(kListTag, other.data_ & kTagMask);
    // TODO(bmeurer): Optimize this case.
97 98 99 100 101
    for (size_t i = 0; i < other.list()->size(); ++i) {
      if (std::find(cached_list->begin(), cached_list->end(),
                    other.list()->at(i)) == cached_list->end()) {
        return false;
      }
102 103 104 105
    }
    return true;
  }

106 107
  bool contains(Handle<T> other) const {
    if (data_ == kEmptyTag) return false;
108
    Address* other_address = reinterpret_cast<Address*>(other.address());
109
    if ((data_ & kTagMask) == kSingletonTag) {
110
      return singleton() == other_address;
111 112
    }
    DCHECK_EQ(kListTag, data_ & kTagMask);
113 114
    return std::find(list()->begin(), list()->end(), other_address) !=
           list()->end();
115 116
  }

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  void remove(Handle<T> handle, Zone* zone) {
    // TODO(bmeurer): Optimize this case.
    ZoneHandleSet<T> that;
    for (size_t i = 0; i < size(); ++i) {
      Handle<T> value = at(i);
      if (value.address() != handle.address()) {
        that.insert(value, zone);
      }
    }
    std::swap(*this, that);
  }

  friend bool operator==(ZoneHandleSet<T> const& lhs,
                         ZoneHandleSet<T> const& rhs) {
    if (lhs.data_ == rhs.data_) return true;
    if ((lhs.data_ & kTagMask) == kListTag &&
        (rhs.data_ & kTagMask) == kListTag) {
      List const* const lhs_list = lhs.list();
      List const* const rhs_list = rhs.list();
136 137
      if (lhs_list->size() == rhs_list->size()) {
        for (size_t i = 0; i < lhs_list->size(); ++i) {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
          if (lhs_list->at(i) != rhs_list->at(i)) return false;
        }
        return true;
      }
    }
    return false;
  }

  friend bool operator!=(ZoneHandleSet<T> const& lhs,
                         ZoneHandleSet<T> const& rhs) {
    return !(lhs == rhs);
  }

  friend size_t hash_value(ZoneHandleSet<T> const& set) {
    return static_cast<size_t>(set.data_);
  }

155 156 157 158
  class const_iterator;
  inline const_iterator begin() const;
  inline const_iterator end() const;

159
 private:
160
  using List = ZoneVector<Address*>;
161 162 163

  List const* list() const {
    DCHECK_EQ(kListTag, data_ & kTagMask);
164
    return reinterpret_cast<List const*>(data_ - kListTag);
165 166
  }

167
  Address* singleton() const {
168
    DCHECK_EQ(kSingletonTag, data_ & kTagMask);
169
    return reinterpret_cast<Address*>(data_ - kSingletonTag);
170 171
  }

172
  enum Tag : Address {
173 174 175 176 177 178 179 180
    kSingletonTag = 0,
    kEmptyTag = 1,
    kListTag = 2,
    kTagMask = 3
  };

  STATIC_ASSERT(kTagMask < kPointerAlignment);

181
  Address data_;
182 183
};

184 185 186 187 188 189 190 191 192
template <typename T>
std::ostream& operator<<(std::ostream& os, ZoneHandleSet<T> set) {
  for (size_t i = 0; i < set.size(); ++i) {
    if (i > 0) os << ", ";
    os << set.at(i);
  }
  return os;
}

193 194 195
template <typename T>
class ZoneHandleSet<T>::const_iterator {
 public:
196 197 198 199 200
  using iterator_category = std::forward_iterator_tag;
  using difference_type = std::ptrdiff_t;
  using value_type = Handle<T>;
  using reference = value_type;
  using pointer = value_type*;
201 202 203 204

  const_iterator(const const_iterator& other)
      : set_(other.set_), current_(other.current_) {}

205
  reference operator*() const { return (*set_)[current_]; }
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  bool operator==(const const_iterator& other) const {
    return set_ == other.set_ && current_ == other.current_;
  }
  bool operator!=(const const_iterator& other) const {
    return !(*this == other);
  }
  const_iterator& operator++() {
    DCHECK(current_ < set_->size());
    current_ += 1;
    return *this;
  }
  const_iterator operator++(int);

 private:
  friend class ZoneHandleSet<T>;

  explicit const_iterator(const ZoneHandleSet<T>* set, size_t current)
      : set_(set), current_(current) {}

  const ZoneHandleSet<T>* set_;
  size_t current_;
};

template <typename T>
typename ZoneHandleSet<T>::const_iterator ZoneHandleSet<T>::begin() const {
  return ZoneHandleSet<T>::const_iterator(this, 0);
}

template <typename T>
typename ZoneHandleSet<T>::const_iterator ZoneHandleSet<T>::end() const {
  return ZoneHandleSet<T>::const_iterator(this, size());
}

239 240 241 242
}  // namespace internal
}  // namespace v8

#endif  // V8_ZONE_ZONE_HANDLE_SET_H_