identity-map.h 5.63 KB
Newer Older
1 2 3 4
// Copyright 2015 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_IDENTITY_MAP_H_
#define V8_IDENTITY_MAP_H_
7

8
#include "src/base/functional.h"
9
#include "src/handles.h"
10
#include "src/objects/heap-object.h"
11 12 13 14

namespace v8 {
namespace internal {

15
// Forward declarations.
16 17 18 19
class Heap;

// Base class of identity maps contains shared code for all template
// instantions.
20
class V8_EXPORT_PRIVATE IdentityMapBase {
21 22 23 24 25 26
 public:
  bool empty() const { return size_ == 0; }
  int size() const { return size_; }
  int capacity() const { return capacity_; }
  bool is_iterable() const { return is_iterable_; }

27 28 29 30 31 32 33
 protected:
  // Allow Tester to access internals, including changing the address of objects
  // within the {keys_} array in order to simulate a moving GC.
  friend class IdentityMapTester;

  typedef void** RawEntry;

34
  explicit IdentityMapBase(Heap* heap)
35 36 37
      : heap_(heap),
        gc_counter_(-1),
        size_(0),
38
        capacity_(0),
39 40
        mask_(0),
        keys_(nullptr),
41 42 43
        values_(nullptr),
        is_iterable_(false) {}
  virtual ~IdentityMapBase();
44

45 46 47
  RawEntry GetEntry(Address key);
  RawEntry FindEntry(Address key) const;
  bool DeleteEntry(Address key, void** deleted_value);
48
  void Clear();
49

50
  Address KeyAtIndex(int index) const;
51

52 53
  RawEntry EntryAtIndex(int index) const;
  int NextIndex(int index) const;
54 55 56 57 58 59 60

  void EnableIteration();
  void DisableIteration();

  virtual void** NewPointerArray(size_t length) = 0;
  virtual void DeleteArray(void* array) = 0;

61 62
 private:
  // Internal implementation should not be called directly by subclasses.
63 64 65 66
  int ScanKeysFor(Address address) const;
  int InsertKey(Address address);
  int Lookup(Address key) const;
  int LookupOrInsert(Address key);
67
  bool DeleteIndex(int index, void** deleted_value);
68
  void Rehash();
69
  void Resize(int new_capacity);
70
  int Hash(Address address) const;
71

72
  base::hash<uintptr_t> hasher_;
73 74 75
  Heap* heap_;
  int gc_counter_;
  int size_;
76
  int capacity_;
77
  int mask_;
78
  Address* keys_;
79
  void** values_;
80 81 82
  bool is_iterable_;

  DISALLOW_COPY_AND_ASSIGN(IdentityMapBase);
83 84 85 86 87 88 89 90
};

// Implements an identity map from object addresses to a given value type {V}.
// The map is robust w.r.t. garbage collection by synchronization with the
// supplied {heap}.
//  * Keys are treated as strong roots.
//  * The value type {V} must be reinterpret_cast'able to {void*}
//  * The value type {V} must not be a heap type.
91
template <typename V, class AllocationPolicy>
92 93
class IdentityMap : public IdentityMapBase {
 public:
94 95 96
  explicit IdentityMap(Heap* heap,
                       AllocationPolicy allocator = AllocationPolicy())
      : IdentityMapBase(heap), allocator_(allocator) {}
97
  ~IdentityMap() override { Clear(); }
98 99 100 101 102

  // Searches this map for the given key using the object's address
  // as the identity, returning:
  //    found => a pointer to the storage location for the value
  //    not found => a pointer to a new storage location for the value
yangguo's avatar
yangguo committed
103
  V* Get(Handle<Object> key) { return Get(*key); }
104
  V* Get(Object key) { return reinterpret_cast<V*>(GetEntry(key.ptr())); }
105 106 107 108 109

  // Searches this map for the given key using the object's address
  // as the identity, returning:
  //    found => a pointer to the storage location for the value
  //    not found => {nullptr}
110
  V* Find(Handle<Object> key) const { return Find(*key); }
111
  V* Find(Object key) const {
112 113
    return reinterpret_cast<V*>(FindEntry(key.ptr()));
  }
114 115

  // Set the value for the given key.
yangguo's avatar
yangguo committed
116
  void Set(Handle<Object> key, V v) { Set(*key, v); }
117
  void Set(Object key, V v) {
118 119
    *(reinterpret_cast<V*>(GetEntry(key.ptr()))) = v;
  }
120

121 122 123
  bool Delete(Handle<Object> key, V* deleted_value) {
    return Delete(*key, deleted_value);
  }
124
  bool Delete(Object key, V* deleted_value) {
125 126 127 128 129 130 131
    void* v = nullptr;
    bool deleted_something = DeleteEntry(key.ptr(), &v);
    if (deleted_value != nullptr && deleted_something) {
      *deleted_value = *reinterpret_cast<V*>(&v);
    }
    return deleted_something;
  }
132

133 134
  // Removes all elements from the map.
  void Clear() { IdentityMapBase::Clear(); }
135 136 137 138 139 140 141 142 143 144

  // Iterator over IdentityMap. The IteratableScope used to create this Iterator
  // must be live for the duration of the iteration.
  class Iterator {
   public:
    Iterator& operator++() {
      index_ = map_->NextIndex(index_);
      return *this;
    }

145
    Object key() const { return Object(map_->KeyAtIndex(index_)); }
146 147 148 149 150 151
    V* entry() const {
      return reinterpret_cast<V*>(map_->EntryAtIndex(index_));
    }

    V* operator*() { return entry(); }
    V* operator->() { return entry(); }
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    bool operator!=(const Iterator& other) { return index_ != other.index_; }

   private:
    Iterator(IdentityMap* map, int index) : map_(map), index_(index) {}

    IdentityMap* map_;
    int index_;

    friend class IdentityMap;
  };

  class IteratableScope {
   public:
    explicit IteratableScope(IdentityMap* map) : map_(map) {
      CHECK(!map_->is_iterable());
      map_->EnableIteration();
    }
    ~IteratableScope() {
      CHECK(map_->is_iterable());
      map_->DisableIteration();
    }

    Iterator begin() { return Iterator(map_, map_->NextIndex(-1)); }
    Iterator end() { return Iterator(map_, map_->capacity()); }

   private:
    IdentityMap* map_;
    DISALLOW_COPY_AND_ASSIGN(IteratableScope);
  };

 protected:
  void** NewPointerArray(size_t length) override {
    return static_cast<void**>(allocator_.New(sizeof(void*) * length));
  }
  void DeleteArray(void* array) override { allocator_.Delete(array); }

 private:
  AllocationPolicy allocator_;
  DISALLOW_COPY_AND_ASSIGN(IdentityMap);
191
};
192

193 194
}  // namespace internal
}  // namespace v8
195

196
#endif  // V8_IDENTITY_MAP_H_