FinalizationGroup.cpp 3.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Finalization group GC implementation.
 */

#include "builtin/FinalizationGroupObject.h"
#include "gc/GCRuntime.h"
#include "gc/Zone.h"

#include "gc/PrivateIterators-inl.h"

using namespace js;
using namespace js::gc;

bool GCRuntime::registerWithFinalizationGroup(JSContext* cx,
                                              HandleObject target,
                                              HandleObject record) {
  MOZ_ASSERT(!IsCrossCompartmentWrapper(target));
  MOZ_ASSERT(
      UncheckedUnwrapWithoutExpose(record)->is<FinalizationRecordObject>());
  MOZ_ASSERT(target->compartment() == record->compartment());

  auto& map = target->zone()->finalizationRecordMap();
  auto ptr = map.lookupForAdd(target);
  if (!ptr) {
    if (!map.add(ptr, target, FinalizationRecordVector(target->zone()))) {
32
      ReportOutOfMemory(cx);
33
34
35
      return false;
    }
  }
36
37
38
39
40
  if (!ptr->value().append(record)) {
    ReportOutOfMemory(cx);
    return false;
  }
  return true;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
}

void GCRuntime::markFinalizationGroupData(JSTracer* trc) {
  // The finalization groups and holdings for all targets are marked as roots.
  for (GCZonesIter zone(this); !zone.done(); zone.next()) {
    auto& map = zone->finalizationRecordMap();
    for (Zone::FinalizationRecordMap::Enum e(map); !e.empty(); e.popFront()) {
      e.front().value().trace(trc);
    }
  }
}

void GCRuntime::sweepFinalizationGroups(Zone* zone) {
  // Queue holdings for cleanup for any entries whose target is dying and remove
  // them from the map. Sweep remaining unregister tokens.

  auto& map = zone->finalizationRecordMap();
  for (Zone::FinalizationRecordMap::Enum e(map); !e.empty(); e.popFront()) {
    auto& records = e.front().value();
    if (IsAboutToBeFinalized(&e.front().mutableKey())) {
      // Queue holdings for targets that are dying.
      for (JSObject* obj : records) {
        obj = UncheckedUnwrapWithoutExpose(obj);
64
65
66
67
68
69
        if (!obj->is<FinalizationRecordObject>()) {
          MOZ_ASSERT(JS_IsDeadWrapper(obj));
          // CCWs between the compartments have been nuked. The
          // FinalizationGroup's callback doesn't run in this case.
          continue;
        }
70
71
72
        auto record = &obj->as<FinalizationRecordObject>();
        FinalizationGroupObject* group = record->group();
        if (group) {
73
          group->queueRecordToBeCleanedUp(record);
74
75
76
77
78
79
80
81
82
83
          queueFinalizationGroupForCleanup(group);
        }
      }
      e.removeFront();
    } else {
      // Update any pointers moved by the GC.
      records.sweep();
      // Remove records that have been unregistered.
      records.eraseIf([](JSObject* obj) {
        obj = UncheckedUnwrapWithoutExpose(obj);
84
85
86
87
88
89
        if (!obj->is<FinalizationRecordObject>()) {
          MOZ_ASSERT(JS_IsDeadWrapper(obj));
          // CCWs between the compartments have been nuked. The
          // FinalizationGroup's callback doesn't run in this case.
          return true;
        }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
        auto record = &obj->as<FinalizationRecordObject>();
        return !record->group();
      });
    }
  }
}

void GCRuntime::queueFinalizationGroupForCleanup(
    FinalizationGroupObject* group) {
  // Prod the embedding to call us back later to run the finalization callbacks.
  if (!group->isQueuedForCleanup()) {
    callHostCleanupFinalizationGroupCallback(group);
    group->setQueuedForCleanup(true);
  }
}

bool GCRuntime::cleanupQueuedFinalizationGroup(
    JSContext* cx, HandleFinalizationGroupObject group) {
  group->setQueuedForCleanup(false);
109
  bool ok = FinalizationGroupObject::cleanupQueuedRecords(cx, group);
110
111
  return ok;
}