identity-map.h 5.2 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 10 11 12 13
#include "src/handles.h"

namespace v8 {
namespace internal {

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

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

26 27 28 29 30 31 32
 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;

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

yangguo's avatar
yangguo committed
44
  RawEntry GetEntry(Object* key);
45 46
  RawEntry FindEntry(Object* key) const;
  void* DeleteEntry(Object* key);
47
  void Clear();
48

49 50
  V8_EXPORT_PRIVATE RawEntry EntryAtIndex(int index) const;
  V8_EXPORT_PRIVATE int NextIndex(int index) const;
51 52 53 54 55 56 57

  void EnableIteration();
  void DisableIteration();

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

58 59
 private:
  // Internal implementation should not be called directly by subclasses.
60 61 62 63
  int ScanKeysFor(Object* address) const;
  int InsertKey(Object* address);
  int Lookup(Object* key) const;
  int LookupOrInsert(Object* key);
64
  void* DeleteIndex(int index);
65
  void Rehash();
66 67
  void Resize(int new_capacity);
  int Hash(Object* address) const;
68

69
  base::hash<uintptr_t> hasher_;
70 71 72
  Heap* heap_;
  int gc_counter_;
  int size_;
73
  int capacity_;
74 75 76
  int mask_;
  Object** keys_;
  void** values_;
77 78 79
  bool is_iterable_;

  DISALLOW_COPY_AND_ASSIGN(IdentityMapBase);
80 81 82 83 84 85 86 87
};

// 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.
88
template <typename V, class AllocationPolicy>
89 90
class IdentityMap : public IdentityMapBase {
 public:
91 92 93 94
  explicit IdentityMap(Heap* heap,
                       AllocationPolicy allocator = AllocationPolicy())
      : IdentityMapBase(heap), allocator_(allocator) {}
  ~IdentityMap() override { Clear(); };
95 96 97 98 99

  // 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
100 101
  V* Get(Handle<Object> key) { return Get(*key); }
  V* Get(Object* key) { return reinterpret_cast<V*>(GetEntry(key)); }
102 103 104 105 106

  // 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}
107 108
  V* Find(Handle<Object> key) const { return Find(*key); }
  V* Find(Object* key) const { return reinterpret_cast<V*>(FindEntry(key)); }
109 110

  // Set the value for the given key.
yangguo's avatar
yangguo committed
111 112
  void Set(Handle<Object> key, V v) { Set(*key, v); }
  void Set(Object* key, V v) { *(reinterpret_cast<V*>(GetEntry(key))) = v; }
113

114
  V Delete(Handle<Object> key) { return Delete(*key); }
115
  V Delete(Object* key) { return reinterpret_cast<V>(DeleteEntry(key)); }
116

117 118
  // Removes all elements from the map.
  void Clear() { IdentityMapBase::Clear(); }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  // 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;
    }

    V* operator*() { return reinterpret_cast<V*>(map_->EntryAtIndex(index_)); }
    V* operator->() { return reinterpret_cast<V*>(map_->EntryAtIndex(index_)); }
    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);
170
};
171

172 173
}  // namespace internal
}  // namespace v8
174

175
#endif  // V8_IDENTITY_MAP_H_