]> git.saurik.com Git - apple/javascriptcore.git/blame - jit/ExecutableAllocatorFixedVMPool.cpp
JavaScriptCore-1218.35.tar.gz
[apple/javascriptcore.git] / jit / ExecutableAllocatorFixedVMPool.cpp
CommitLineData
ba379fdc
A
1/*
2 * Copyright (C) 2009 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
28#include "ExecutableAllocator.h"
29
4e4e5a6f 30#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
ba379fdc 31
6fe7ccc8 32#include "CodeProfiling.h"
4e4e5a6f 33#include <errno.h>
ba379fdc 34#include <unistd.h>
6fe7ccc8 35#include <wtf/MetaAllocator.h>
14957cd0 36#include <wtf/PageReservation.h>
ba379fdc
A
37#include <wtf/VMTags.h>
38
93a37866
A
39#if OS(DARWIN)
40#include <sys/mman.h>
41#endif
42
14957cd0
A
43#if OS(LINUX)
44#include <stdio.h>
45#endif
46
93a37866
A
47#if !PLATFORM(IOS) && PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1090
48// MADV_FREE_REUSABLE does not work for JIT memory on older OSes so use MADV_FREE in that case.
49#define WTF_USE_MADV_FREE_FOR_JIT_MEMORY 1
50#endif
4e4e5a6f 51
ba379fdc
A
52using namespace WTF;
53
54namespace JSC {
14957cd0 55
93a37866 56uintptr_t startOfFixedExecutableMemoryPool;
ba379fdc 57
6fe7ccc8 58class FixedVMPoolExecutableAllocator : public MetaAllocator {
93a37866 59 WTF_MAKE_FAST_ALLOCATED;
b80e6193 60public:
6fe7ccc8 61 FixedVMPoolExecutableAllocator()
93a37866 62 : MetaAllocator(jitAllocationGranule) // round up all allocations to 32 bytes
b80e6193 63 {
93a37866
A
64 m_reservation = PageReservation::reserveWithGuardPages(fixedExecutableMemoryPoolSize, OSAllocator::JSJITCodePages, EXECUTABLE_POOL_WRITABLE, true);
65#if !ENABLE(LLINT)
66 RELEASE_ASSERT(m_reservation);
6fe7ccc8
A
67#endif
68 if (m_reservation) {
93a37866 69 ASSERT(m_reservation.size() == fixedExecutableMemoryPoolSize);
6fe7ccc8 70 addFreshFreeSpace(m_reservation.base(), m_reservation.size());
93a37866
A
71
72 startOfFixedExecutableMemoryPool = reinterpret_cast<uintptr_t>(m_reservation.base());
ba379fdc 73 }
b80e6193 74 }
93a37866
A
75
76 virtual ~FixedVMPoolExecutableAllocator();
6fe7ccc8
A
77
78protected:
79 virtual void* allocateNewSpace(size_t&)
b80e6193 80 {
6fe7ccc8
A
81 // We're operating in a fixed pool, so new allocation is always prohibited.
82 return 0;
b80e6193 83 }
6fe7ccc8
A
84
85 virtual void notifyNeedPage(void* page)
b80e6193 86 {
93a37866 87#if USE(MADV_FREE_FOR_JIT_MEMORY)
6fe7ccc8
A
88 UNUSED_PARAM(page);
89#else
90 m_reservation.commit(page, pageSize());
b80e6193 91#endif
b80e6193 92 }
6fe7ccc8
A
93
94 virtual void notifyPageIsFree(void* page)
95 {
93a37866 96#if USE(MADV_FREE_FOR_JIT_MEMORY)
6fe7ccc8
A
97 for (;;) {
98 int result = madvise(page, pageSize(), MADV_FREE);
99 if (!result)
100 return;
101 ASSERT(result == -1);
102 if (errno != EAGAIN) {
93a37866 103 RELEASE_ASSERT_NOT_REACHED(); // In debug mode, this should be a hard failure.
6fe7ccc8 104 break; // In release mode, we should just ignore the error - not returning memory to the OS is better than crashing, especially since we _will_ be able to reuse the memory internally anyway.
ba379fdc 105 }
b80e6193 106 }
b80e6193 107#else
6fe7ccc8 108 m_reservation.decommit(page, pageSize());
4e4e5a6f 109#endif
ba379fdc 110 }
b80e6193 111
14957cd0 112private:
14957cd0 113 PageReservation m_reservation;
ba379fdc
A
114};
115
6fe7ccc8 116static FixedVMPoolExecutableAllocator* allocator;
14957cd0 117
6fe7ccc8
A
118void ExecutableAllocator::initializeAllocator()
119{
120 ASSERT(!allocator);
121 allocator = new FixedVMPoolExecutableAllocator();
122 CodeProfiling::notifyAllocator(allocator);
123}
14957cd0 124
93a37866 125ExecutableAllocator::ExecutableAllocator(VM&)
14957cd0 126{
6fe7ccc8
A
127 ASSERT(allocator);
128}
14957cd0 129
6fe7ccc8 130ExecutableAllocator::~ExecutableAllocator()
ba379fdc 131{
ba379fdc
A
132}
133
93a37866
A
134FixedVMPoolExecutableAllocator::~FixedVMPoolExecutableAllocator()
135{
136 m_reservation.deallocate();
137}
138
4e4e5a6f
A
139bool ExecutableAllocator::isValid() const
140{
6fe7ccc8 141 return !!allocator->bytesReserved();
4e4e5a6f
A
142}
143
14957cd0 144bool ExecutableAllocator::underMemoryPressure()
ba379fdc 145{
6fe7ccc8
A
146 MetaAllocator::Statistics statistics = allocator->currentStatistics();
147 return statistics.bytesAllocated > statistics.bytesReserved / 2;
ba379fdc
A
148}
149
6fe7ccc8 150double ExecutableAllocator::memoryPressureMultiplier(size_t addedMemoryUsage)
ba379fdc 151{
6fe7ccc8
A
152 MetaAllocator::Statistics statistics = allocator->currentStatistics();
153 ASSERT(statistics.bytesAllocated <= statistics.bytesReserved);
154 size_t bytesAllocated = statistics.bytesAllocated + addedMemoryUsage;
155 if (bytesAllocated >= statistics.bytesReserved)
156 bytesAllocated = statistics.bytesReserved;
157 double result = 1.0;
158 size_t divisor = statistics.bytesReserved - bytesAllocated;
159 if (divisor)
160 result = static_cast<double>(statistics.bytesReserved) / divisor;
161 if (result < 1.0)
162 result = 1.0;
163 return result;
b80e6193
A
164}
165
93a37866 166PassRefPtr<ExecutableMemoryHandle> ExecutableAllocator::allocate(VM& vm, size_t sizeInBytes, void* ownerUID, JITCompilationEffort effort)
b80e6193 167{
6fe7ccc8
A
168 RefPtr<ExecutableMemoryHandle> result = allocator->allocate(sizeInBytes, ownerUID);
169 if (!result) {
170 if (effort == JITCompilationCanFail)
171 return result;
93a37866 172 releaseExecutableMemory(vm);
6fe7ccc8 173 result = allocator->allocate(sizeInBytes, ownerUID);
93a37866 174 RELEASE_ASSERT(result);
6fe7ccc8
A
175 }
176 return result.release();
ba379fdc
A
177}
178
6fe7ccc8
A
179size_t ExecutableAllocator::committedByteCount()
180{
181 return allocator->bytesCommitted();
182}
183
184#if ENABLE(META_ALLOCATOR_PROFILE)
185void ExecutableAllocator::dumpProfile()
186{
187 allocator->dumpProfile();
188}
189#endif
190
ba379fdc
A
191}
192
14957cd0 193
6fe7ccc8 194#endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
14957cd0 195
93a37866 196#if PLATFORM(IOS)
6fe7ccc8 197#if !ENABLE(ASSEMBLER)
14957cd0
A
198// FIXME: Needed to satisfy JavaScriptCore.exp requirements when building only the interpreter.
199namespace JSC {
200size_t ExecutableAllocator::committedByteCount()
201{
202 return 0;
203}
204} // namespace JSC
205#endif // !ENABLE(JIT)
93a37866 206#endif // PLATFORM(IOS)