weak-object-worklists.cc 5.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2020 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.

#include "src/heap/weak-object-worklists.h"

#include "src/heap/heap-inl.h"
#include "src/heap/heap.h"
#include "src/objects/hash-table.h"
#include "src/objects/heap-object.h"
#include "src/objects/js-function.h"
#include "src/objects/js-weak-refs-inl.h"
#include "src/objects/js-weak-refs.h"
#include "src/objects/shared-function-info.h"
#include "src/objects/transitions.h"

namespace v8 {

namespace internal {

21 22 23 24 25 26 27 28 29 30 31 32 33 34
WeakObjects::Local::Local(WeakObjects* weak_objects)
    : WeakObjects::UnusedBase()
#define INIT_LOCAL_WORKLIST(_, name, __) , name##_local(&weak_objects->name)
          WEAK_OBJECT_WORKLISTS(INIT_LOCAL_WORKLIST)
#undef INIT_LOCAL_WORKLIST
{
}

void WeakObjects::Local::Publish() {
#define INVOKE_PUBLISH(_, name, __) name##_local.Publish();
  WEAK_OBJECT_WORKLISTS(INVOKE_PUBLISH)
#undef INVOKE_PUBLISH
}

35 36 37 38 39 40
void WeakObjects::UpdateAfterScavenge() {
#define INVOKE_UPDATE(_, name, Name) Update##Name(name);
  WEAK_OBJECT_WORKLISTS(INVOKE_UPDATE)
#undef INVOKE_UPDATE
}

41 42 43 44 45 46
void WeakObjects::Clear() {
#define INVOKE_CLEAR(_, name, __) name.Clear();
  WEAK_OBJECT_WORKLISTS(INVOKE_CLEAR)
#undef INVOKE_CLEAR
}

47
// static
48 49 50 51 52
void WeakObjects::UpdateTransitionArrays(
    WeakObjectWorklist<TransitionArray>& transition_arrays) {
  DCHECK(!ContainsYoungObjects(transition_arrays));
}

53
// static
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
void WeakObjects::UpdateEphemeronHashTables(
    WeakObjectWorklist<EphemeronHashTable>& ephemeron_hash_tables) {
  ephemeron_hash_tables.Update(
      [](EphemeronHashTable slot_in, EphemeronHashTable* slot_out) -> bool {
        EphemeronHashTable forwarded = ForwardingAddress(slot_in);

        if (!forwarded.is_null()) {
          *slot_out = forwarded;
          return true;
        }

        return false;
      });
}

namespace {
bool EphemeronUpdater(Ephemeron slot_in, Ephemeron* slot_out) {
  HeapObject key = slot_in.key;
  HeapObject value = slot_in.value;
  HeapObject forwarded_key = ForwardingAddress(key);
  HeapObject forwarded_value = ForwardingAddress(value);

  if (!forwarded_key.is_null() && !forwarded_value.is_null()) {
    *slot_out = Ephemeron{forwarded_key, forwarded_value};
    return true;
  }

  return false;
}
}  // anonymous namespace

85
// static
86 87 88 89 90
void WeakObjects::UpdateCurrentEphemerons(
    WeakObjectWorklist<Ephemeron>& current_ephemerons) {
  current_ephemerons.Update(EphemeronUpdater);
}

91
// static
92 93 94 95 96
void WeakObjects::UpdateNextEphemerons(
    WeakObjectWorklist<Ephemeron>& next_ephemerons) {
  next_ephemerons.Update(EphemeronUpdater);
}

97
// static
98 99 100 101 102
void WeakObjects::UpdateDiscoveredEphemerons(
    WeakObjectWorklist<Ephemeron>& discovered_ephemerons) {
  discovered_ephemerons.Update(EphemeronUpdater);
}

103
// static
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
void WeakObjects::UpdateWeakReferences(
    WeakObjectWorklist<HeapObjectAndSlot>& weak_references) {
  weak_references.Update(
      [](HeapObjectAndSlot slot_in, HeapObjectAndSlot* slot_out) -> bool {
        HeapObject heap_obj = slot_in.first;
        HeapObject forwarded = ForwardingAddress(heap_obj);

        if (!forwarded.is_null()) {
          ptrdiff_t distance_to_slot =
              slot_in.second.address() - slot_in.first.ptr();
          Address new_slot = forwarded.ptr() + distance_to_slot;
          slot_out->first = forwarded;
          slot_out->second = HeapObjectSlot(new_slot);
          return true;
        }

        return false;
      });
}

124
// static
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
void WeakObjects::UpdateWeakObjectsInCode(
    WeakObjectWorklist<HeapObjectAndCode>& weak_objects_in_code) {
  weak_objects_in_code.Update(
      [](HeapObjectAndCode slot_in, HeapObjectAndCode* slot_out) -> bool {
        HeapObject heap_obj = slot_in.first;
        HeapObject forwarded = ForwardingAddress(heap_obj);

        if (!forwarded.is_null()) {
          slot_out->first = forwarded;
          slot_out->second = slot_in.second;
          return true;
        }

        return false;
      });
}

142
// static
143 144
void WeakObjects::UpdateJSWeakRefs(
    WeakObjectWorklist<JSWeakRef>& js_weak_refs) {
145 146 147 148 149 150 151 152 153 154 155
  js_weak_refs.Update(
      [](JSWeakRef js_weak_ref_in, JSWeakRef* js_weak_ref_out) -> bool {
        JSWeakRef forwarded = ForwardingAddress(js_weak_ref_in);

        if (!forwarded.is_null()) {
          *js_weak_ref_out = forwarded;
          return true;
        }

        return false;
      });
156 157
}

158
// static
159 160 161 162 163
void WeakObjects::UpdateWeakCells(WeakObjectWorklist<WeakCell>& weak_cells) {
  // TODO(syg, marja): Support WeakCells in the young generation.
  DCHECK(!ContainsYoungObjects(weak_cells));
}

164
// static
165 166 167
void WeakObjects::UpdateCodeFlushingCandidates(
    WeakObjectWorklist<SharedFunctionInfo>& code_flushing_candidates) {
  DCHECK(!ContainsYoungObjects(code_flushing_candidates));
168 169
}

170
// static
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void WeakObjects::UpdateFlushedJSFunctions(
    WeakObjectWorklist<JSFunction>& flushed_js_functions) {
  flushed_js_functions.Update(
      [](JSFunction slot_in, JSFunction* slot_out) -> bool {
        JSFunction forwarded = ForwardingAddress(slot_in);

        if (!forwarded.is_null()) {
          *slot_out = forwarded;
          return true;
        }

        return false;
      });
}

186
// static
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
void WeakObjects::UpdateBaselineFlushingCandidates(
    WeakObjectWorklist<JSFunction>& baseline_flush_candidates) {
  baseline_flush_candidates.Update(
      [](JSFunction slot_in, JSFunction* slot_out) -> bool {
        JSFunction forwarded = ForwardingAddress(slot_in);

        if (!forwarded.is_null()) {
          *slot_out = forwarded;
          return true;
        }

        return false;
      });
}

202
#ifdef DEBUG
203
// static
204 205 206 207 208 209 210 211 212 213 214 215 216 217
template <typename Type>
bool WeakObjects::ContainsYoungObjects(WeakObjectWorklist<Type>& worklist) {
  bool result = false;
  worklist.Iterate([&result](Type candidate) {
    if (Heap::InYoungGeneration(candidate)) {
      result = true;
    }
  });
  return result;
}
#endif

}  // namespace internal
}  // namespace v8