MarkedBlock.cpp 6.26 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
/*
 * Copyright (C) 2011 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.
 */

#include "config.h"
#include "MarkedBlock.h"

29
#include "IncrementalSweeper.h"
30
#include "JSCell.h"
31
#include "JSDestructibleObject.h"
32

33

34
35
namespace JSC {

36
MarkedBlock* MarkedBlock::create(const PageAllocationAligned& allocation, MarkedAllocator* allocator, size_t cellSize, DestructorType destructorType)
37
{
38
    return new (NotNull, allocation.base()) MarkedBlock(allocation, allocator, cellSize, destructorType);
39
40
}

41
MarkedBlock::MarkedBlock(const PageAllocationAligned& allocation, MarkedAllocator* allocator, size_t cellSize, DestructorType destructorType)
42
    : HeapBlock<MarkedBlock>(allocation)
43
    , m_atomsPerCell((cellSize + atomSize - 1) / atomSize)
44
    , m_endAtom(atomsPerBlock - m_atomsPerCell + 1)
45
46
    , m_destructorType(destructorType)
    , m_allocator(allocator)
47
    , m_state(New) // All cells start out unmarked.
48
    , m_weakSet(allocator->heap()->globalData())
49
{
50
    ASSERT(allocator);
51
    HEAP_LOG_BLOCK_STATE_TRANSITION(this);
52
53
}

54
inline void MarkedBlock::callDestructor(JSCell* cell)
55
{
56
57
    // A previous eager sweep may already have run cell's destructor.
    if (cell->isZapped())
58
        return;
59

60
#if ENABLE(SIMPLE_HEAP_PROFILING)
61
    m_heap->m_destroyedTypeCounts.countVPtr(vptr);
62
#endif
63

64
    cell->methodTable()->destroy(cell);
65
    cell->zap();
66
67
}

68
template<MarkedBlock::BlockState blockState, MarkedBlock::SweepMode sweepMode, MarkedBlock::DestructorType dtorType>
69
MarkedBlock::FreeList MarkedBlock::specializedSweep()
70
{
71
    ASSERT(blockState != Allocated && blockState != FreeListed);
72
    ASSERT(!(dtorType == MarkedBlock::None && sweepMode == SweepOnly));
73

74
    // This produces a free list that is ordered in reverse through the block.
75
76
    // This is fine, since the allocation code makes no assumptions about the
    // order of the free list.
77
    FreeCell* head = 0;
78
    size_t count = 0;
79
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
80
81
82
        if (blockState == Marked && m_marks.get(i))
            continue;

83
        JSCell* cell = reinterpret_cast_ptr<JSCell*>(&atoms()[i]);
84

85
        if (dtorType != MarkedBlock::None && blockState != New)
86
            callDestructor(cell);
87
88

        if (sweepMode == SweepToFreeList) {
89
            FreeCell* freeCell = reinterpret_cast<FreeCell*>(cell);
90
91
            freeCell->next = head;
            head = freeCell;
92
            ++count;
93
94
        }
    }
95

96
    m_state = ((sweepMode == SweepToFreeList) ? FreeListed : Marked);
97
    return FreeList(head, count * cellSize());
98
99
}

100
MarkedBlock::FreeList MarkedBlock::sweep(SweepMode sweepMode)
101
{
102
103
    HEAP_LOG_BLOCK_STATE_TRANSITION(this);

104
105
    m_weakSet.sweep();

106
    if (sweepMode == SweepOnly && m_destructorType == MarkedBlock::None)
107
        return FreeList();
108

109
110
111
112
113
    if (m_destructorType == MarkedBlock::ImmortalStructure)
        return sweepHelper<MarkedBlock::ImmortalStructure>(sweepMode);
    if (m_destructorType == MarkedBlock::Normal)
        return sweepHelper<MarkedBlock::Normal>(sweepMode);
    return sweepHelper<MarkedBlock::None>(sweepMode);
114
115
}

116
template<MarkedBlock::DestructorType dtorType>
117
MarkedBlock::FreeList MarkedBlock::sweepHelper(SweepMode sweepMode)
118
{
119
120
121
    switch (m_state) {
    case New:
        ASSERT(sweepMode == SweepToFreeList);
122
        return specializedSweep<New, SweepToFreeList, dtorType>();
123
124
125
    case FreeListed:
        // Happens when a block transitions to fully allocated.
        ASSERT(sweepMode == SweepToFreeList);
126
        return FreeList();
127
128
    case Allocated:
        ASSERT_NOT_REACHED();
129
        return FreeList();
130
131
    case Marked:
        return sweepMode == SweepToFreeList
132
133
            ? specializedSweep<Marked, SweepToFreeList, dtorType>()
            : specializedSweep<Marked, SweepOnly, dtorType>();
134
    }
ggaren@apple.com's avatar
ggaren@apple.com committed
135
136

    ASSERT_NOT_REACHED();
137
    return FreeList();
138
139
}

140
141
142
143
144
145
146
147
148
class SetAllMarksFunctor : public MarkedBlock::VoidFunctor {
public:
    void operator()(JSCell* cell)
    {
        MarkedBlock::blockFor(cell)->setMarked(cell);
    }
};

void MarkedBlock::canonicalizeCellLivenessData(const FreeList& freeList)
149
{
150
    HEAP_LOG_BLOCK_STATE_TRANSITION(this);
151
    FreeCell* head = freeList.head;
152

153
154
155
156
157
158
159
    if (m_state == Marked) {
        // If the block is in the Marked state then we know that:
        // 1) It was not used for allocation during the previous allocation cycle.
        // 2) It may have dead objects, and we only know them to be dead by the
        //    fact that their mark bits are unset.
        // Hence if the block is Marked we need to leave it Marked.
        
160
        ASSERT(!head);
161
162
        return;
    }
163
   
164
165
    ASSERT(m_state == FreeListed);
    
166
167
    // Roll back to a coherent state for Heap introspection. Cells newly
    // allocated from our free list are not currently marked, so we need another
168
    // way to tell what's live vs dead. 
169
    
170
171
172
    SetAllMarksFunctor functor;
    forEachCell(functor);

173
    FreeCell* next;
174
    for (FreeCell* current = head; current; current = next) {
175
176
        next = current->next;
        reinterpret_cast<JSCell*>(current)->zap();
177
        clearMarked(current);
178
    }
179
    
180
    m_state = Marked;
181
182
}

183
} // namespace JSC