author Steve Fink <sfink@mozilla.com>
Wed, 21 Nov 2018 12:07:12 -0800
changeset 498695 6fcd1d505c808bd7e14aaefecb9759cd7194bb87
parent 491455 3ba56e06507e0c78c679261c3d0424decb3f7b29
child 505383 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1426574 - Use fallible bitmap ops for AtomizeAndCopyChars, r=jonco a=lizzard

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * 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/. */

#include "gc/AtomMarking.h"

#include "mozilla/Assertions.h"
#include "vm/Realm.h"

#include "gc/Heap-inl.h"

namespace js {
namespace gc {

inline size_t
GetAtomBit(TenuredCell* thing)
    Arena* arena = thing->arena();
    size_t arenaBit = (reinterpret_cast<uintptr_t>(thing) - arena->address()) / CellBytesPerMarkBit;
    return arena->atomBitmapStart() * JS_BITS_PER_WORD + arenaBit;

inline bool
ThingIsPermanent(JSAtom* atom)
    return atom->isPinned();

inline bool
ThingIsPermanent(JS::Symbol* symbol)
    return symbol->isWellKnownSymbol();

template <typename T, bool Fallible>
AtomMarkingRuntime::inlinedMarkAtomInternal(JSContext* cx, T* thing)
    static_assert(mozilla::IsSame<T, JSAtom>::value ||
                  mozilla::IsSame<T, JS::Symbol>::value,
                  "Should only be called with JSAtom* or JS::Symbol* argument");

    js::gc::TenuredCell* cell = &thing->asTenured();

    // The context's zone will be null during initialization of the runtime.
    if (!cx->zone()) {
        return true;

    if (ThingIsPermanent(thing)) {
        return true;

    size_t bit = GetAtomBit(cell);
    MOZ_ASSERT(bit / JS_BITS_PER_WORD < allocatedWords);

    if (Fallible) {
        if (!cx->zone()->markedAtoms().setBitFallible(bit)) {
            return false;
    } else {

    if (!cx->helperThread()) {
        // Trigger a read barrier on the atom, in case there is an incremental
        // GC in progress. This is necessary if the atom is being marked
        // because a reference to it was obtained from another zone which is
        // not being collected by the incremental GC.

    // Children of the thing also need to be marked in the context's zone.
    // We don't have a JSTracer for this so manually handle the cases in which
    // an atom can reference other atoms.
    markChildren(cx, thing);

    return true;

template <typename T>
AtomMarkingRuntime::inlinedMarkAtom(JSContext* cx, T* thing)
    MOZ_ALWAYS_TRUE((inlinedMarkAtomInternal<T, false>(cx, thing)));

template <typename T>
AtomMarkingRuntime::inlinedMarkAtomFallible(JSContext* cx, T* thing)
    return inlinedMarkAtomInternal<T, true>(cx, thing);

} // namespace gc
} // namespace js