1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JITStubRoutineSet.h"
28
29#if ENABLE(JIT)
30
31#include "GCAwareJITStubRoutine.h"
32#include "JSCInlines.h"
33#include "SlotVisitor.h"
34
35namespace JSC {
36
37JITStubRoutineSet::JITStubRoutineSet() { }
38JITStubRoutineSet::~JITStubRoutineSet()
39{
40    for (size_t i = m_listOfRoutines.size(); i--;) {
41        GCAwareJITStubRoutine* routine = m_listOfRoutines[i];
42
43        routine->m_mayBeExecuting = false;
44
45        if (!routine->m_isJettisoned) {
46            // Inform the deref() routine that it should delete this guy as soon
47            // as the ref count reaches zero.
48            routine->m_isJettisoned = true;
49            continue;
50        }
51
52        routine->deleteFromGC();
53    }
54}
55
56void JITStubRoutineSet::add(GCAwareJITStubRoutine* routine)
57{
58    ASSERT(!routine->m_isJettisoned);
59
60    m_listOfRoutines.append(routine);
61
62    uintptr_t start = routine->startAddress();
63    uintptr_t end = routine->endAddress();
64    uintptr_t step = JITStubRoutine::addressStep();
65    for (uintptr_t iter = start; iter < end; iter += step) {
66        ASSERT(m_addressToRoutineMap.find(iter) == m_addressToRoutineMap.end());
67        m_addressToRoutineMap.add(iter, routine);
68    }
69}
70
71void JITStubRoutineSet::clearMarks()
72{
73    for (size_t i = m_listOfRoutines.size(); i--;)
74        m_listOfRoutines[i]->m_mayBeExecuting = false;
75}
76
77void JITStubRoutineSet::markSlow(uintptr_t address)
78{
79    HashMap<uintptr_t, GCAwareJITStubRoutine*>::iterator iter =
80        m_addressToRoutineMap.find(address & ~(JITStubRoutine::addressStep() - 1));
81
82    if (iter == m_addressToRoutineMap.end())
83        return;
84
85    iter->value->m_mayBeExecuting = true;
86}
87
88void JITStubRoutineSet::deleteUnmarkedJettisonedStubRoutines()
89{
90    for (size_t i = 0; i < m_listOfRoutines.size(); i++) {
91        GCAwareJITStubRoutine* routine = m_listOfRoutines[i];
92        if (!routine->m_isJettisoned || routine->m_mayBeExecuting)
93            continue;
94
95        uintptr_t start = routine->startAddress();
96        uintptr_t end = routine->endAddress();
97        uintptr_t step = JITStubRoutine::addressStep();
98        for (uintptr_t iter = start; iter < end; iter += step) {
99            ASSERT(m_addressToRoutineMap.find(iter) != m_addressToRoutineMap.end());
100            ASSERT(m_addressToRoutineMap.find(iter)->value == routine);
101            m_addressToRoutineMap.remove(iter);
102        }
103
104        routine->deleteFromGC();
105
106        m_listOfRoutines[i] = m_listOfRoutines.last();
107        m_listOfRoutines.removeLast();
108        i--;
109    }
110}
111
112void JITStubRoutineSet::traceMarkedStubRoutines(SlotVisitor& visitor)
113{
114    for (size_t i = m_listOfRoutines.size(); i--;) {
115        GCAwareJITStubRoutine* routine = m_listOfRoutines[i];
116        if (!routine->m_mayBeExecuting)
117            continue;
118
119        routine->markRequiredObjects(visitor);
120    }
121}
122
123} // namespace JSC
124
125#endif // ENABLE(JIT)
126
127