2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "ExecutableAllocator.h"
30 #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
32 #include "CodeProfiling.h"
35 #include <wtf/MetaAllocator.h>
36 #include <wtf/PageReservation.h>
37 #include <wtf/VMTags.h>
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
56 uintptr_t startOfFixedExecutableMemoryPool
;
58 class FixedVMPoolExecutableAllocator
: public MetaAllocator
{
59 WTF_MAKE_FAST_ALLOCATED
;
61 FixedVMPoolExecutableAllocator()
62 : MetaAllocator(jitAllocationGranule
) // round up all allocations to 32 bytes
64 m_reservation
= PageReservation::reserveWithGuardPages(fixedExecutableMemoryPoolSize
, OSAllocator::JSJITCodePages
, EXECUTABLE_POOL_WRITABLE
, true);
66 RELEASE_ASSERT(m_reservation
);
69 ASSERT(m_reservation
.size() == fixedExecutableMemoryPoolSize
);
70 addFreshFreeSpace(m_reservation
.base(), m_reservation
.size());
72 startOfFixedExecutableMemoryPool
= reinterpret_cast<uintptr_t>(m_reservation
.base());
76 virtual ~FixedVMPoolExecutableAllocator();
79 virtual void* allocateNewSpace(size_t&)
81 // We're operating in a fixed pool, so new allocation is always prohibited.
85 virtual void notifyNeedPage(void* page
)
87 #if USE(MADV_FREE_FOR_JIT_MEMORY)
90 m_reservation
.commit(page
, pageSize());
94 virtual void notifyPageIsFree(void* page
)
96 #if USE(MADV_FREE_FOR_JIT_MEMORY)
98 int result
= madvise(page
, pageSize(), MADV_FREE
);
101 ASSERT(result
== -1);
102 if (errno
!= EAGAIN
) {
103 RELEASE_ASSERT_NOT_REACHED(); // In debug mode, this should be a hard failure.
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.
108 m_reservation
.decommit(page
, pageSize());
113 PageReservation m_reservation
;
116 static FixedVMPoolExecutableAllocator
* allocator
;
118 void ExecutableAllocator::initializeAllocator()
121 allocator
= new FixedVMPoolExecutableAllocator();
122 CodeProfiling::notifyAllocator(allocator
);
125 ExecutableAllocator::ExecutableAllocator(VM
&)
130 ExecutableAllocator::~ExecutableAllocator()
134 FixedVMPoolExecutableAllocator::~FixedVMPoolExecutableAllocator()
136 m_reservation
.deallocate();
139 bool ExecutableAllocator::isValid() const
141 return !!allocator
->bytesReserved();
144 bool ExecutableAllocator::underMemoryPressure()
146 MetaAllocator::Statistics statistics
= allocator
->currentStatistics();
147 return statistics
.bytesAllocated
> statistics
.bytesReserved
/ 2;
150 double ExecutableAllocator::memoryPressureMultiplier(size_t addedMemoryUsage
)
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
;
158 size_t divisor
= statistics
.bytesReserved
- bytesAllocated
;
160 result
= static_cast<double>(statistics
.bytesReserved
) / divisor
;
166 PassRefPtr
<ExecutableMemoryHandle
> ExecutableAllocator::allocate(VM
& vm
, size_t sizeInBytes
, void* ownerUID
, JITCompilationEffort effort
)
168 RefPtr
<ExecutableMemoryHandle
> result
= allocator
->allocate(sizeInBytes
, ownerUID
);
170 if (effort
== JITCompilationCanFail
)
172 releaseExecutableMemory(vm
);
173 result
= allocator
->allocate(sizeInBytes
, ownerUID
);
174 RELEASE_ASSERT(result
);
176 return result
.release();
179 size_t ExecutableAllocator::committedByteCount()
181 return allocator
->bytesCommitted();
184 #if ENABLE(META_ALLOCATOR_PROFILE)
185 void ExecutableAllocator::dumpProfile()
187 allocator
->dumpProfile();
194 #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
197 #if !ENABLE(ASSEMBLER)
198 // FIXME: Needed to satisfy JavaScriptCore.exp requirements when building only the interpreter.
200 size_t ExecutableAllocator::committedByteCount()
205 #endif // !ENABLE(JIT)
206 #endif // PLATFORM(IOS)