MarkedBlock.cpp 4.91 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 "JSCell.h"
30
#include "JSObject.h"
31
#include "ScopeChain.h"
32

33
34
namespace JSC {

35
MarkedBlock* MarkedBlock::create(Heap* heap, size_t cellSize)
36
{
37
    PageAllocationAligned allocation = PageAllocationAligned::allocate(blockSize, blockSize, OSAllocator::JSGCHeapPages);
mitz@apple.com's avatar
mitz@apple.com committed
38
    if (!static_cast<bool>(allocation))
39
        CRASH();
40
    return new (allocation.base()) MarkedBlock(allocation, heap, cellSize);
41
42
43
44
}

void MarkedBlock::destroy(MarkedBlock* block)
{
45
46
    for (size_t i = block->firstAtom(); i < block->m_endAtom; i += block->m_atomsPerCell)
        reinterpret_cast<JSCell*>(&block->atoms()[i])->~JSCell();
47
48
49
    block->m_allocation.deallocate();
}

50
MarkedBlock::MarkedBlock(const PageAllocationAligned& allocation, Heap* heap, size_t cellSize)
51
    : m_inNewSpace(false)
52
    , m_allocation(allocation)
53
    , m_heap(heap)
54
{
55
56
    m_atomsPerCell = (cellSize + atomSize - 1) / atomSize;
    m_endAtom = atomsPerBlock - m_atomsPerCell + 1;
57
58
}

59
60
61
62
void MarkedBlock::sweep()
{
    Structure* dummyMarkableCellStructure = m_heap->globalData()->dummyMarkableCellStructure.get();

63
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
64
        if (m_marks.get(i))
65
66
            continue;

67
        JSCell* cell = reinterpret_cast<JSCell*>(&atoms()[i]);
68
        cell->~JSCell();
69
        new (cell) JSCell(*m_heap->globalData(), dummyMarkableCellStructure);
70
71
72
    }
}

73
74
75
76
77
78
MarkedBlock::FreeCell* MarkedBlock::lazySweep()
{
    // This returns a free list that is ordered in reverse through the block.
    // This is fine, since the allocation code makes no assumptions about the
    // order of the free list.
    
79
80
    void* jsFinalObjectVPtr = m_heap->globalData()->jsFinalObjectVPtr;
    
81
82
83
84
85
    FreeCell* result = 0;
    
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
        if (!m_marks.testAndSet(i)) {
            JSCell* cell = reinterpret_cast<JSCell*>(&atoms()[i]);
86
87
88
89
90
            if (cell->vptr() == jsFinalObjectVPtr) {
                JSFinalObject* object = reinterpret_cast<JSFinalObject*>(cell);
                object->JSFinalObject::~JSFinalObject();
            } else
                cell->~JSCell();
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            FreeCell* freeCell = reinterpret_cast<FreeCell*>(cell);
            freeCell->next = result;
            result = freeCell;
        }
    }
    
    return result;
}

MarkedBlock::FreeCell* MarkedBlock::blessNewBlockForFastPath()
{
    // This returns a free list that is ordered in reverse through the block,
    // as in lazySweep() above.
    
    FreeCell* result = 0;
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
        m_marks.set(i);
        FreeCell* freeCell = reinterpret_cast<FreeCell*>(&atoms()[i]);
        freeCell->next = result;
        result = freeCell;
    }
    return result;
}

void MarkedBlock::blessNewBlockForSlowPath()
{
    Structure* dummyMarkableCellStructure = m_heap->globalData()->dummyMarkableCellStructure.get();
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell)
        new (&atoms()[i]) JSCell(*m_heap->globalData(), dummyMarkableCellStructure, JSCell::CreatingEarlyCell);
}

void MarkedBlock::canonicalizeBlock(FreeCell* firstFreeCell)
{
    Structure* dummyMarkableCellStructure = m_heap->globalData()->dummyMarkableCellStructure.get();
    
    for (FreeCell* current = firstFreeCell; current;) {
        FreeCell* next = current->next;
        size_t i = atomNumber(current);
        
        m_marks.clear(i);
        new (static_cast<void*>(current)) JSCell(*m_heap->globalData(), dummyMarkableCellStructure, JSCell::CreatingEarlyCell);

        current = next;
    }
}

137
} // namespace JSC