1/*
2 * Copyright (C) 2017-2018 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 "Subspace.h"
28
29#include "AlignedMemoryAllocator.h"
30#include "BlockDirectoryInlines.h"
31#include "HeapCellType.h"
32#include "JSCInlines.h"
33#include "MarkedBlockInlines.h"
34#include "MarkedSpaceInlines.h"
35#include "ParallelSourceAdapter.h"
36#include "PreventCollectionScope.h"
37#include "SubspaceInlines.h"
38
39namespace JSC {
40
41Subspace::Subspace(CString name, Heap& heap)
42 : m_space(heap.objectSpace())
43 , m_name(name)
44{
45}
46
47void Subspace::initialize(HeapCellType* heapCellType, AlignedMemoryAllocator* alignedMemoryAllocator)
48{
49 m_heapCellType = heapCellType;
50 m_alignedMemoryAllocator = alignedMemoryAllocator;
51 m_directoryForEmptyAllocation = m_alignedMemoryAllocator->firstDirectory();
52
53 Heap& heap = m_space.heap();
54 heap.objectSpace().m_subspaces.append(this);
55 m_alignedMemoryAllocator->registerSubspace(this);
56}
57
58Subspace::~Subspace()
59{
60}
61
62void Subspace::finishSweep(MarkedBlock::Handle& block, FreeList* freeList)
63{
64 m_heapCellType->finishSweep(block, freeList);
65}
66
67void Subspace::destroy(VM& vm, JSCell* cell)
68{
69 m_heapCellType->destroy(vm, cell);
70}
71
72void Subspace::prepareForAllocation()
73{
74 forEachDirectory(
75 [&] (BlockDirectory& directory) {
76 directory.prepareForAllocation();
77 });
78
79 m_directoryForEmptyAllocation = m_alignedMemoryAllocator->firstDirectory();
80}
81
82MarkedBlock::Handle* Subspace::findEmptyBlockToSteal()
83{
84 for (; m_directoryForEmptyAllocation; m_directoryForEmptyAllocation = m_directoryForEmptyAllocation->nextDirectoryInAlignedMemoryAllocator()) {
85 if (MarkedBlock::Handle* block = m_directoryForEmptyAllocation->findEmptyBlockToSteal())
86 return block;
87 }
88 return nullptr;
89}
90
91Ref<SharedTask<BlockDirectory*()>> Subspace::parallelDirectorySource()
92{
93 class Task : public SharedTask<BlockDirectory*()> {
94 public:
95 Task(BlockDirectory* directory)
96 : m_directory(directory)
97 {
98 }
99
100 BlockDirectory* run() override
101 {
102 auto locker = holdLock(m_lock);
103 BlockDirectory* result = m_directory;
104 if (result)
105 m_directory = result->nextDirectoryInSubspace();
106 return result;
107 }
108
109 private:
110 BlockDirectory* m_directory;
111 Lock m_lock;
112 };
113
114 return adoptRef(*new Task(m_firstDirectory));
115}
116
117Ref<SharedTask<MarkedBlock::Handle*()>> Subspace::parallelNotEmptyMarkedBlockSource()
118{
119 return createParallelSourceAdapter<BlockDirectory*, MarkedBlock::Handle*>(
120 parallelDirectorySource(),
121 [] (BlockDirectory* directory) -> RefPtr<SharedTask<MarkedBlock::Handle*()>> {
122 if (!directory)
123 return nullptr;
124 return directory->parallelNotEmptyBlockSource();
125 });
126}
127
128void Subspace::sweepBlocks()
129{
130 forEachDirectory(
131 [&] (BlockDirectory& directory) {
132 directory.sweep();
133 });
134}
135
136void Subspace::didResizeBits(size_t)
137{
138}
139
140void Subspace::didRemoveBlock(size_t)
141{
142}
143
144void Subspace::didBeginSweepingToFreeList(MarkedBlock::Handle*)
145{
146}
147
148} // namespace JSC
149
150