address-map.cc 1.69 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
#include "src/utils/address-map.h"
6
#include "src/execution/isolate.h"
7
#include "src/heap/heap.h"
8
#include "src/objects/heap-object-inl.h"
9
#include "src/objects/objects-inl.h"
10 11 12 13 14 15

namespace v8 {
namespace internal {

RootIndexMap::RootIndexMap(Isolate* isolate) {
  map_ = isolate->root_index_map();
16
  if (map_ != nullptr) return;
17
  map_ = new HeapObjectToIndexHashMap();
Dan Elphick's avatar
Dan Elphick committed
18 19
  for (RootIndex root_index = RootIndex::kFirstStrongOrReadOnlyRoot;
       root_index <= RootIndex::kLastStrongOrReadOnlyRoot; ++root_index) {
20
    Object root = isolate->root(root_index);
21
    if (!root.IsHeapObject()) continue;
22 23
    // Omit root entries that can be written after initialization. They must
    // not be referenced through the root list in the snapshot.
24 25
    // Since we map the raw address of an root item to its root list index, the
    // raw address must be constant, i.e. the object must be immovable.
26
    if (RootsTable::IsImmortalImmovable(root_index)) {
27
      HeapObject heap_object = HeapObject::cast(root);
28
      Maybe<uint32_t> maybe_index = map_->Get(heap_object);
29
      uint32_t index = static_cast<uint32_t>(root_index);
30
      if (maybe_index.IsJust()) {
31
        // Some are initialized to a previous value in the root list.
32
        DCHECK_LT(maybe_index.FromJust(), index);
33
      } else {
34
        map_->Set(heap_object, index);
35 36 37 38 39 40
      }
    }
  }
  isolate->set_root_index_map(map_);
}

41
bool RootIndexMap::Lookup(Address obj, RootIndex* out_root_list) const {
42
  return Lookup(HeapObject::cast(Object(obj)), out_root_list);
43 44
}

45 46
}  // namespace internal
}  // namespace v8