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 "IsoSubspace.h"
28
29#include "AllocatorInlines.h"
30#include "BlockDirectoryInlines.h"
31#include "IsoAlignedMemoryAllocator.h"
32#include "IsoCellSetInlines.h"
33#include "IsoSubspaceInlines.h"
34#include "LocalAllocatorInlines.h"
35#include "MarkedSpaceInlines.h"
36
37namespace JSC {
38
39IsoSubspace::IsoSubspace(CString name, Heap& heap, HeapCellType* heapCellType, size_t size, uint8_t numberOfLowerTierCells)
40 : Subspace(name, heap)
41 , m_directory(WTF::roundUpToMultipleOf<MarkedBlock::atomSize>(size))
42 , m_localAllocator(&m_directory)
43 , m_isoAlignedMemoryAllocator(makeUnique<IsoAlignedMemoryAllocator>())
44{
45 m_remainingLowerTierCellCount = numberOfLowerTierCells;
46 ASSERT(WTF::roundUpToMultipleOf<MarkedBlock::atomSize>(size) == cellSize());
47 ASSERT(numberOfLowerTierCells <= MarkedBlock::maxNumberOfLowerTierCells);
48 m_isIsoSubspace = true;
49 initialize(heapCellType, m_isoAlignedMemoryAllocator.get());
50
51 auto locker = holdLock(m_space.directoryLock());
52 m_directory.setSubspace(this);
53 m_space.addBlockDirectory(locker, &m_directory);
54 m_alignedMemoryAllocator->registerDirectory(heap, &m_directory);
55 m_firstDirectory = &m_directory;
56}
57
58IsoSubspace::~IsoSubspace()
59{
60}
61
62Allocator IsoSubspace::allocatorFor(size_t size, AllocatorForMode mode)
63{
64 return allocatorForNonVirtual(size, mode);
65}
66
67void* IsoSubspace::allocate(VM& vm, size_t size, GCDeferralContext* deferralContext, AllocationFailureMode failureMode)
68{
69 return allocateNonVirtual(vm, size, deferralContext, failureMode);
70}
71
72void IsoSubspace::didResizeBits(size_t blockIndex)
73{
74 m_cellSets.forEach(
75 [&] (IsoCellSet* set) {
76 set->didResizeBits(blockIndex);
77 });
78}
79
80void IsoSubspace::didRemoveBlock(size_t blockIndex)
81{
82 m_cellSets.forEach(
83 [&] (IsoCellSet* set) {
84 set->didRemoveBlock(blockIndex);
85 });
86}
87
88void IsoSubspace::didBeginSweepingToFreeList(MarkedBlock::Handle* block)
89{
90 m_cellSets.forEach(
91 [&] (IsoCellSet* set) {
92 set->sweepToFreeList(block);
93 });
94}
95
96void* IsoSubspace::tryAllocateFromLowerTier()
97{
98 auto revive = [&] (PreciseAllocation* allocation) {
99 allocation->setIndexInSpace(m_space.m_preciseAllocations.size());
100 allocation->m_hasValidCell = true;
101 m_space.m_preciseAllocations.append(allocation);
102 if (auto* set = m_space.preciseAllocationSet())
103 set->add(allocation->cell());
104 ASSERT(allocation->indexInSpace() == m_space.m_preciseAllocations.size() - 1);
105 m_preciseAllocations.append(allocation);
106 return allocation->cell();
107 };
108
109 if (!m_lowerTierFreeList.isEmpty()) {
110 PreciseAllocation* allocation = m_lowerTierFreeList.begin();
111 allocation->remove();
112 return revive(allocation);
113 }
114 if (m_remainingLowerTierCellCount) {
115 PreciseAllocation* allocation = PreciseAllocation::createForLowerTier(m_space.heap(), cellSize(), this, --m_remainingLowerTierCellCount);
116 return revive(allocation);
117 }
118 return nullptr;
119}
120
121void IsoSubspace::sweepLowerTierCell(PreciseAllocation* preciseAllocation)
122{
123 preciseAllocation = preciseAllocation->reuseForLowerTier();
124 m_lowerTierFreeList.append(preciseAllocation);
125}
126
127void IsoSubspace::destroyLowerTierFreeList()
128{
129 m_lowerTierFreeList.forEach([&](PreciseAllocation* allocation) {
130 allocation->destroy();
131 });
132}
133
134} // namespace JSC
135
136