summaryrefslogtreecommitdiffstats
path: root/Source/JavaScriptCore/runtime/MarkedSpace.cpp
blob: 2f8075dba4f04063b6b7e7212eb97716818b2d6f (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
137
138
/*
 *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"
#include "MarkedSpace.h"

#include "JSCell.h"
#include "JSGlobalData.h"
#include "JSLock.h"

namespace JSC {

class Structure;

MarkedSpace::MarkedSpace(JSGlobalData* globalData)
    : m_waterMark(0)
    , m_highWaterMark(0)
    , m_globalData(globalData)
{
    allocateBlock();
}

void MarkedSpace::destroy()
{
    clearMarks(); // Make sure weak pointers appear dead during destruction.

    while (m_heap.blocks.size())
        freeBlock(0);
    m_heap.blocks.clear();
}

NEVER_INLINE MarkedBlock* MarkedSpace::allocateBlock()
{
    MarkedBlock* block = MarkedBlock::create(globalData());
    m_heap.blocks.append(block);
    return block;
}

NEVER_INLINE void MarkedSpace::freeBlock(size_t block)
{
    MarkedBlock::destroy(m_heap.blocks[block]);

    // swap with the last block so we compact as we go
    m_heap.blocks[block] = m_heap.blocks.last();
    m_heap.blocks.removeLast();
}

void* MarkedSpace::allocate(size_t)
{
    do {
        ASSERT(m_heap.nextBlock < m_heap.blocks.size());
        MarkedBlock* block = m_heap.collectorBlock(m_heap.nextBlock);
        if (void* result = block->allocate(m_heap.nextCell))
            return result;

        m_waterMark += block->capacity();
    } while (++m_heap.nextBlock != m_heap.blocks.size());

    if (m_waterMark < m_highWaterMark)
        return allocateBlock()->allocate(m_heap.nextCell);

    return 0;
}

void MarkedSpace::shrink()
{
    for (size_t i = 0; i != m_heap.blocks.size() && m_heap.blocks.size() > 1; ) { // We assume at least one block exists at all times.
        if (m_heap.collectorBlock(i)->isEmpty()) {
            freeBlock(i);
        } else
            ++i;
    }
}

void MarkedSpace::clearMarks()
{
    for (size_t i = 0; i < m_heap.blocks.size(); ++i)
        m_heap.collectorBlock(i)->clearMarks();
}

void MarkedSpace::sweep()
{
    for (size_t i = 0; i < m_heap.blocks.size(); ++i)
        m_heap.collectorBlock(i)->sweep();
}

size_t MarkedSpace::objectCount() const
{
    size_t result = 0;
    for (size_t i = 0; i < m_heap.blocks.size(); ++i)
        result += m_heap.collectorBlock(i)->markCount();
    return result;
}

size_t MarkedSpace::size() const
{
    size_t result = 0;
    for (size_t i = 0; i < m_heap.blocks.size(); ++i)
        result += m_heap.collectorBlock(i)->size();
    return result;
}

size_t MarkedSpace::capacity() const
{
    size_t result = 0;
    for (size_t i = 0; i < m_heap.blocks.size(); ++i)
        result += m_heap.collectorBlock(i)->capacity();
    return result;
}

void MarkedSpace::reset()
{
    m_heap.nextCell = 0;
    m_heap.nextBlock = 0;
    m_waterMark = 0;
#if ENABLE(JSC_ZOMBIES)
    sweep();
#endif
}

} // namespace JSC