address-map.h 2 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_ADDRESS_MAP_H_
#define V8_ADDRESS_MAP_H_

8
#include "include/v8.h"
9
#include "src/assert-scope.h"
lpy's avatar
lpy committed
10
#include "src/base/hashmap.h"
11 12 13 14 15
#include "src/objects.h"

namespace v8 {
namespace internal {

16 17 18 19 20 21 22
template <typename Type>
class PointerToIndexHashMap
    : public base::TemplateHashMapImpl<uintptr_t, uint32_t,
                                       base::KeyEqualityMatcher<intptr_t>,
                                       base::DefaultAllocationPolicy> {
 public:
  typedef base::TemplateHashMapEntry<uintptr_t, uint32_t> Entry;
23

24 25 26
  inline void Set(Type value, uint32_t index) {
    uintptr_t key = Key(value);
    LookupOrInsert(key, Hash(key))->value = index;
27 28
  }

29 30 31 32 33
  inline Maybe<uint32_t> Get(Type value) const {
    uintptr_t key = Key(value);
    Entry* entry = Lookup(key, Hash(key));
    if (entry == nullptr) return Nothing<uint32_t>();
    return Just(entry->value);
34 35 36
  }

 private:
37
  static inline uintptr_t Key(Type value);
38

39
  static uint32_t Hash(uintptr_t key) { return static_cast<uint32_t>(key); }
40 41
};

42 43 44 45 46 47 48 49 50 51
template <>
inline uintptr_t PointerToIndexHashMap<Address>::Key(Address value) {
  return static_cast<uintptr_t>(value);
}

template <typename Type>
inline uintptr_t PointerToIndexHashMap<Type>::Key(Type value) {
  return reinterpret_cast<uintptr_t>(value);
}

52 53 54 55
class AddressToIndexHashMap : public PointerToIndexHashMap<Address> {};
class HeapObjectToIndexHashMap : public PointerToIndexHashMap<HeapObject*> {};

class RootIndexMap {
56 57 58 59 60 61
 public:
  explicit RootIndexMap(Isolate* isolate);

  static const int kInvalidRootIndex = -1;

  int Lookup(HeapObject* obj) {
62 63
    Maybe<uint32_t> maybe_index = map_->Get(obj);
    return maybe_index.IsJust() ? maybe_index.FromJust() : kInvalidRootIndex;
64 65 66
  }

 private:
67
  HeapObjectToIndexHashMap* map_;
68 69 70 71 72 73 74 75

  DISALLOW_COPY_AND_ASSIGN(RootIndexMap);
};

}  // namespace internal
}  // namespace v8

#endif  // V8_ADDRESS_MAP_H_