summaryrefslogtreecommitdiffstats
path: root/js/src/gc/Zone-inl.h
blob: 8bcb9802cf48a4d71981e4d3342f29e5f245f86a (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -*- 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/. */

#ifndef gc_Zone_inl_h
#define gc_Zone_inl_h

#include "gc/Zone.h"

#include "vm/Runtime.h"

/* static */ inline js::HashNumber JS::Zone::UniqueIdToHash(uint64_t uid) {
  return mozilla::HashGeneric(uid);
}

inline bool JS::Zone::getHashCode(js::gc::Cell* cell, js::HashNumber* hashp) {
  uint64_t uid;
  if (!getOrCreateUniqueId(cell, &uid)) {
    return false;
  }
  *hashp = UniqueIdToHash(uid);
  return true;
}

inline bool JS::Zone::maybeGetUniqueId(js::gc::Cell* cell, uint64_t* uidp) {
  MOZ_ASSERT(uidp);
  MOZ_ASSERT(js::CurrentThreadCanAccessZone(this) ||
             js::CurrentThreadIsPerformingGC());

  // Get an existing uid, if one has been set.
  auto p = uniqueIds().readonlyThreadsafeLookup(cell);
  if (p) {
    *uidp = p->value();
  }

  return p.found();
}

inline bool JS::Zone::getOrCreateUniqueId(js::gc::Cell* cell, uint64_t* uidp) {
  MOZ_ASSERT(uidp);
  MOZ_ASSERT(js::CurrentThreadCanAccessZone(this) ||
             js::CurrentThreadIsPerformingGC());

  // Get an existing uid, if one has been set.
  auto p = uniqueIds().lookupForAdd(cell);
  if (p) {
    *uidp = p->value();
    return true;
  }

  MOZ_ASSERT(js::CurrentThreadCanAccessZone(this));

  // Set a new uid on the cell.
  *uidp = js::gc::NextCellUniqueId(runtimeFromAnyThread());
  if (!uniqueIds().add(p, cell, *uidp)) {
    return false;
  }

  // If the cell was in the nursery, hopefully unlikely, then we need to
  // tell the nursery about it so that it can sweep the uid if the thing
  // does not get tenured.
  if (IsInsideNursery(cell) &&
      !runtimeFromMainThread()->gc.nursery().addedUniqueIdToCell(cell)) {
    uniqueIds().remove(cell);
    return false;
  }

  return true;
}

inline js::HashNumber JS::Zone::getHashCodeInfallible(js::gc::Cell* cell) {
  return UniqueIdToHash(getUniqueIdInfallible(cell));
}

inline uint64_t JS::Zone::getUniqueIdInfallible(js::gc::Cell* cell) {
  uint64_t uid;
  js::AutoEnterOOMUnsafeRegion oomUnsafe;
  if (!getOrCreateUniqueId(cell, &uid)) {
    oomUnsafe.crash("failed to allocate uid");
  }
  return uid;
}

inline bool JS::Zone::hasUniqueId(js::gc::Cell* cell) {
  MOZ_ASSERT(js::CurrentThreadCanAccessZone(this) ||
             js::CurrentThreadIsPerformingGC());
  return uniqueIds().has(cell);
}

inline void JS::Zone::transferUniqueId(js::gc::Cell* tgt, js::gc::Cell* src) {
  MOZ_ASSERT(src != tgt);
  MOZ_ASSERT(!IsInsideNursery(tgt));
  MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtimeFromMainThread()));
  MOZ_ASSERT(js::CurrentThreadCanAccessZone(this));
  MOZ_ASSERT(!uniqueIds().has(tgt));
  uniqueIds().rekeyIfMoved(src, tgt);
}

inline void JS::Zone::removeUniqueId(js::gc::Cell* cell) {
  MOZ_ASSERT(js::CurrentThreadCanAccessZone(this));
  uniqueIds().remove(cell);
}

#endif  // gc_Zone_inl_h