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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* Copyright (C) 2017-2024 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. ``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
* 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 "Subspace.h"
#include "AlignedMemoryAllocator.h"
#include "AllocatorInlines.h"
#include "JSCellInlines.h"
#include "LocalAllocatorInlines.h"
#include "MarkedSpaceInlines.h"
#include "SubspaceInlines.h"
#include <wtf/RAMSize.h>
namespace JSC {
CompleteSubspace::CompleteSubspace(CString name, JSC::Heap& heap, const HeapCellType& heapCellType, AlignedMemoryAllocator* alignedMemoryAllocator)
: Subspace(SubspaceKind::CompleteSubspace, name, heap)
{
initialize(heapCellType, alignedMemoryAllocator);
}
CompleteSubspace::~CompleteSubspace() = default;
Allocator CompleteSubspace::allocatorForNonInline(size_t size, AllocatorForMode mode)
{
return allocatorFor(size, mode);
}
Allocator CompleteSubspace::allocatorForSlow(size_t size)
{
size_t index = MarkedSpace::sizeClassToIndex(size);
size_t sizeClass = MarkedSpace::s_sizeClassForSizeStep[index];
if (!sizeClass)
return Allocator();
// This is written in such a way that it's OK for the JIT threads to end up here if they want
// to generate code that uses some allocator that hadn't been used yet. Note that a possibly-
// just-as-good solution would be to return null if we're in the JIT since the JIT treats null
// allocator as "please always take the slow path". But, that could lead to performance
// surprises and the algorithm here is pretty easy. Only this code has to hold the lock, to
// prevent simultaneously BlockDirectory creations from multiple threads. This code ensures
// that any "forEachAllocator" traversals will only see this allocator after it's initialized
// enough: it will have
Locker locker { m_space.directoryLock() };
if (Allocator allocator = m_allocatorForSizeStep[index])
return allocator;
if (false)
dataLog("Creating BlockDirectory/LocalAllocator for ", m_name, ", ", attributes(), ", ", sizeClass, ".\n");
std::unique_ptr<BlockDirectory> uniqueDirectory = makeUnique<BlockDirectory>(sizeClass);
BlockDirectory* directory = uniqueDirectory.get();
m_directories.append(WTFMove(uniqueDirectory));
directory->setSubspace(this);
m_space.addBlockDirectory(locker, directory);
std::unique_ptr<LocalAllocator> uniqueLocalAllocator =
makeUnique<LocalAllocator>(directory);
LocalAllocator* localAllocator = uniqueLocalAllocator.get();
m_localAllocators.append(WTFMove(uniqueLocalAllocator));
Allocator allocator(localAllocator);
index = MarkedSpace::sizeClassToIndex(sizeClass);
for (;;) {
if (MarkedSpace::s_sizeClassForSizeStep[index] != sizeClass)
break;
m_allocatorForSizeStep[index] = allocator;
if (!index--)
break;
}
directory->setNextDirectoryInSubspace(m_firstDirectory);
m_alignedMemoryAllocator->registerDirectory(m_space.heap(), directory);
WTF::storeStoreFence();
m_firstDirectory = directory;
return allocator;
}
void* CompleteSubspace::allocateSlow(VM& vm, size_t size, GCDeferralContext* deferralContext, AllocationFailureMode failureMode)
{
void* result = tryAllocateSlow(vm, size, deferralContext);
if (failureMode == AllocationFailureMode::Assert)
RELEASE_ASSERT(result);
return result;
}
void* CompleteSubspace::tryAllocateSlow(VM& vm, size_t size, GCDeferralContext* deferralContext)
{
if constexpr (validateDFGDoesGC)
vm.verifyCanGC();
sanitizeStackForVM(vm);
if (Allocator allocator = allocatorForNonInline(size, AllocatorForMode::EnsureAllocator))
return allocator.allocate(vm.heap, allocator.cellSize(), deferralContext, AllocationFailureMode::ReturnNull);
if (size <= Options::preciseAllocationCutoff()
&& size <= MarkedSpace::largeCutoff) {
dataLog("FATAL: attampting to allocate small object using large allocation.\n");
dataLog("Requested allocation size: ", size, "\n");
RELEASE_ASSERT_NOT_REACHED();
}
vm.heap.collectIfNecessaryOrDefer(deferralContext);
if (UNLIKELY(Options::maxHeapSizeAsRAMSizeMultiple())) {
if (vm.heap.capacity() > Options::maxHeapSizeAsRAMSizeMultiple() * WTF::ramSize())
return nullptr;
}
size = WTF::roundUpToMultipleOf<MarkedSpace::sizeStep>(size);
PreciseAllocation* allocation = PreciseAllocation::tryCreate(vm.heap, size, this, m_space.m_preciseAllocations.size());
if (!allocation)
return nullptr;
m_preciseAllocations.append(allocation);
m_space.registerPreciseAllocation(allocation, /* isNewAllocation */ true);
return allocation->cell();
}
void* CompleteSubspace::reallocatePreciseAllocationNonVirtual(VM& vm, HeapCell* oldCell, size_t size, GCDeferralContext* deferralContext, AllocationFailureMode failureMode)
{
if constexpr (validateDFGDoesGC)
vm.verifyCanGC();
// The following conditions are met in Butterfly for example.
ASSERT(oldCell->isPreciseAllocation());
PreciseAllocation* oldAllocation = &oldCell->preciseAllocation();
ASSERT(oldAllocation->cellSize() <= size);
ASSERT(oldAllocation->weakSet().isTriviallyDestructible());
ASSERT(oldAllocation->attributes().destruction == DoesNotNeedDestruction);
ASSERT(oldAllocation->attributes().cellKind == HeapCell::Auxiliary);
ASSERT(size > MarkedSpace::largeCutoff);
sanitizeStackForVM(vm);
if (size <= Options::preciseAllocationCutoff()
&& size <= MarkedSpace::largeCutoff) {
dataLog("FATAL: attampting to allocate small object using large allocation.\n");
dataLog("Requested allocation size: ", size, "\n");
RELEASE_ASSERT_NOT_REACHED();
}
vm.heap.collectIfNecessaryOrDefer(deferralContext);
size = WTF::roundUpToMultipleOf<MarkedSpace::sizeStep>(size);
size_t difference = size - oldAllocation->cellSize();
unsigned oldIndexInSpace = oldAllocation->indexInSpace();
if (oldAllocation->isOnList())
oldAllocation->remove();
PreciseAllocation* allocation = oldAllocation->tryReallocate(size, this);
if (!allocation) {
RELEASE_ASSERT(failureMode != AllocationFailureMode::Assert);
m_preciseAllocations.append(oldAllocation);
return nullptr;
}
ASSERT(oldIndexInSpace == allocation->indexInSpace());
// If reallocation changes the address, we should update HashSet.
if (oldAllocation != allocation) {
if (auto* set = m_space.preciseAllocationSet()) {
set->remove(oldAllocation->cell());
set->add(allocation->cell());
}
}
m_space.m_preciseAllocations[oldIndexInSpace] = allocation;
vm.heap.didAllocate(difference);
m_space.m_capacity += difference;
m_preciseAllocations.append(allocation);
return allocation->cell();
}
} // namespace JSC
|