GC constraint solving should be parallel
[WebKit-https.git] / Source / JavaScriptCore / heap / Subspace.cpp
1 /*
2  * Copyright (C) 2017 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 "HeapCellType.h"
31 #include "JSCInlines.h"
32 #include "MarkedAllocatorInlines.h"
33 #include "MarkedBlockInlines.h"
34 #include "ParallelSourceAdapter.h"
35 #include "PreventCollectionScope.h"
36 #include "SubspaceInlines.h"
37
38 namespace JSC {
39
40 Subspace::Subspace(CString name, Heap& heap)
41     : m_space(heap.objectSpace())
42     , m_name(name)
43 {
44 }
45
46 void Subspace::initialize(HeapCellType* heapCellType, AlignedMemoryAllocator* alignedMemoryAllocator)
47 {
48     m_attributes = heapCellType->attributes();
49     m_heapCellType = heapCellType;
50     m_alignedMemoryAllocator = alignedMemoryAllocator;
51     m_allocatorForEmptyAllocation = m_alignedMemoryAllocator->firstAllocator();
52
53     Heap& heap = *m_space.heap();
54     PreventCollectionScope preventCollectionScope(heap);
55     heap.objectSpace().m_subspaces.append(this);
56     m_alignedMemoryAllocator->registerSubspace(this);
57 }
58
59 Subspace::~Subspace()
60 {
61 }
62
63 void Subspace::finishSweep(MarkedBlock::Handle& block, FreeList* freeList)
64 {
65     m_heapCellType->finishSweep(block, freeList);
66 }
67
68 void Subspace::destroy(VM& vm, JSCell* cell)
69 {
70     m_heapCellType->destroy(vm, cell);
71 }
72
73 void Subspace::prepareForAllocation()
74 {
75     forEachAllocator(
76         [&] (MarkedAllocator& allocator) {
77             allocator.prepareForAllocation();
78         });
79
80     m_allocatorForEmptyAllocation = m_alignedMemoryAllocator->firstAllocator();
81 }
82
83 MarkedBlock::Handle* Subspace::findEmptyBlockToSteal()
84 {
85     for (; m_allocatorForEmptyAllocation; m_allocatorForEmptyAllocation = m_allocatorForEmptyAllocation->nextAllocatorInAlignedMemoryAllocator()) {
86         if (MarkedBlock::Handle* block = m_allocatorForEmptyAllocation->findEmptyBlockToSteal())
87             return block;
88     }
89     return nullptr;
90 }
91
92 RefPtr<SharedTask<MarkedAllocator*()>> Subspace::parallelAllocatorSource()
93 {
94     class Task : public SharedTask<MarkedAllocator*()> {
95     public:
96         Task(MarkedAllocator* allocator)
97             : m_allocator(allocator)
98         {
99         }
100         
101         MarkedAllocator* run() override
102         {
103             auto locker = holdLock(m_lock);
104             MarkedAllocator* result = m_allocator;
105             if (result)
106                 m_allocator = result->nextAllocatorInSubspace();
107             return result;
108         }
109         
110     private:
111         MarkedAllocator* m_allocator;
112         Lock m_lock;
113     };
114     
115     return adoptRef(new Task(m_firstAllocator));
116 }
117
118 RefPtr<SharedTask<MarkedBlock::Handle*()>> Subspace::parallelNotEmptyMarkedBlockSource()
119 {
120     return createParallelSourceAdapter<MarkedAllocator*, MarkedBlock::Handle*>(
121         parallelAllocatorSource(),
122         [] (MarkedAllocator* allocator) -> RefPtr<SharedTask<MarkedBlock::Handle*()>> {
123             if (!allocator)
124                 return nullptr;
125             return allocator->parallelNotEmptyBlockSource();
126         });
127 }
128
129 } // namespace JSC
130