MarkedBlock.cpp 3.73 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 "JSZombie.h"
32
#include "ScopeChain.h"
33

34
35
namespace JSC {

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

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

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

59
    Structure* dummyMarkableCellStructure = heap->globalData()->dummyMarkableCellStructure.get();
60
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell)
61
        new (&atoms()[i]) JSCell(*heap->globalData(), dummyMarkableCellStructure, JSCell::CreatingEarlyCell);
62
63
}

64
65
66
67
void MarkedBlock::sweep()
{
    Structure* dummyMarkableCellStructure = m_heap->globalData()->dummyMarkableCellStructure.get();

68
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
69
        if (m_marks.get(i))
70
71
            continue;

72
        JSCell* cell = reinterpret_cast<JSCell*>(&atoms()[i]);
73
#if ENABLE(JSC_ZOMBIES)
74
        if (cell->structure() && cell->structure() != dummyMarkableCellStructure && !cell->isZombie()) {
75
76
            const ClassInfo* info = cell->classInfo();
            cell->~JSCell();
77
            new (cell) JSZombie(*m_heap->globalData(), info, m_heap->globalData()->zombieStructure.get());
78
            m_marks.set(i);
79
80
81
        }
#else
        cell->~JSCell();
82
        new (cell) JSCell(*m_heap->globalData(), dummyMarkableCellStructure);
83
84
85
86
#endif
    }
}

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#if ENABLE(JSC_ZOMBIES)
void MarkedBlock::clearMarks()
{
    /* Keep our precious zombies! */
    for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
        if (m_marks.get(i))
            continue;

        JSCell* cell = reinterpret_cast<JSCell*>(&atoms()[i]);
        if (!cell->isZombie())
            m_marks.clear(i);
    }
}
#endif

102
} // namespace JSC