/*
 * Copyright (C) 2012 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef SlotVisitorInlineMethods_h
#define SlotVisitorInlineMethods_h

#include "SlotVisitor.h"
#include "JSGlobalData.h"
#include "Weak.h"

namespace JSC {

template<typename T>
ALWAYS_INLINE void SlotVisitor::appendUnbarrieredPointer(T** slot)
{
    ASSERT(slot);
    append(*slot);
}

ALWAYS_INLINE void SlotVisitor::appendUnbarrieredValue(JSValue* slot)
{
    ASSERT(slot);
    append(*slot);
}

template<typename T>
ALWAYS_INLINE void SlotVisitor::appendUnbarrieredWeak(Weak<T>* weak)
{
    ASSERT(weak);
    append(weak->get());
}

template<typename T>
ALWAYS_INLINE void SlotVisitor::append(WriteBarrierBase<T>* slot)
{
    append(slot->get());
}

template<typename T>
ALWAYS_INLINE void SlotVisitor::append(WriteBarrierBase<T>& slot)
{
    append(slot.get());
}

template<typename T>
ALWAYS_INLINE void SlotVisitor::appendUnbarrieredReadOnlyPointer(T* cell)
{
    append(cell);
}

inline void SlotVisitor::appendValues(WriteBarrierBase<Unknown>* barriers, size_t count)
{
    for (size_t i = 0; i < count; ++i)
        append(&barriers[i]);
}

inline void SlotVisitor::addWeakReferenceHarvester(WeakReferenceHarvester* weakReferenceHarvester)
{
    m_shared.m_weakReferenceHarvesters.addThreadSafe(weakReferenceHarvester);
}

inline void SlotVisitor::addUnconditionalFinalizer(UnconditionalFinalizer* unconditionalFinalizer)
{
    m_shared.m_unconditionalFinalizers.addThreadSafe(unconditionalFinalizer);
}

ALWAYS_INLINE void SlotVisitor::reportExtraMemoryVisited(JSCell* owner, size_t size)
{
    heap()->reportExtraMemoryVisited(owner, size);
}

ALWAYS_INLINE Heap* SlotVisitor::heap() const
{
    return &sharedData().m_vm->heap;
}

ALWAYS_INLINE JSGlobalData& SlotVisitor::vm()
{
    return *sharedData().m_vm;
}

ALWAYS_INLINE const JSGlobalData& SlotVisitor::vm() const
{
    return *sharedData().m_vm;
}

ALWAYS_INLINE void SlotVisitor::append(JSValue value)
{
    if (value.isCell())
    	setMarkedAndAppendToMarkStack(value.asCell());
}

ALWAYS_INLINE void SlotVisitor::append(JSCell* cell)
{
    setMarkedAndAppendToMarkStack(cell);
}

ALWAYS_INLINE void SlotVisitor::appendNotNull(JSCell* cell)
{
	setMarkedAndAppendToMarkStackNotNull(cell);
}

ALWAYS_INLINE void SlotVisitor::setMarkedAndAppendToMarkStack(JSCell* cell)
{
    ASSERT(!m_isCheckingForDefaultMarkViolation);
    if (cell)
    	setMarkedAndAppendToMarkStackNotNull(cell);
}

ALWAYS_INLINE void SlotVisitor::setMarkedAndAppendToMarkStackNotNull(JSCell* cell)
{
    ASSERT(!m_isCheckingForDefaultMarkViolation);

#if ENABLE(GC_VALIDATION)
    validate(cell);
#endif

    if (Heap::testAndSetMarked(cell))
        return;

    //cell->setMarked();
    appendToMarkStack(cell);
}

} // namespace JSC

#endif // SlotVisitorInlineMethods_h

