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)
36 #include <wtf/MetaAllocator.h>
37 #include <wtf/PageReservation.h>
38 #include <wtf/VMTags.h>
49 static const size_t fixedPoolSize = 16 * 1024 * 1024;
51 static const size_t fixedPoolSize = 1024 * 1024 * 1024;
53 static const size_t fixedPoolSize = 32 * 1024 * 1024;
56 class FixedVMPoolExecutableAllocator: public MetaAllocator {
58 FixedVMPoolExecutableAllocator()
59 : MetaAllocator(32) // round up all allocations to 32 bytes
61 m_reservation = PageReservation::reserveWithGuardPages(fixedPoolSize, OSAllocator::JSJITCodePages, EXECUTABLE_POOL_WRITABLE, true);
62 #if !ENABLE(INTERPRETER)
67 ASSERT(m_reservation.size() == fixedPoolSize);
68 addFreshFreeSpace(m_reservation.base(), m_reservation.size());
73 virtual void* allocateNewSpace(size_t&)
75 // We're operating in a fixed pool, so new allocation is always prohibited.
79 virtual void notifyNeedPage(void* page)
81 m_reservation.commit(page, pageSize());
84 virtual void notifyPageIsFree(void* page)
86 m_reservation.decommit(page, pageSize());
90 PageReservation m_reservation;
93 static FixedVMPoolExecutableAllocator* allocator;
95 void ExecutableAllocator::initializeAllocator()
98 allocator = new FixedVMPoolExecutableAllocator();
101 ExecutableAllocator::ExecutableAllocator(JSGlobalData&)
106 bool ExecutableAllocator::isValid() const
108 return !!allocator->bytesReserved();
111 bool ExecutableAllocator::underMemoryPressure()
113 MetaAllocator::Statistics statistics = allocator->currentStatistics();
114 return statistics.bytesAllocated > statistics.bytesReserved / 2;
117 PassRefPtr<ExecutableMemoryHandle> ExecutableAllocator::allocate(JSGlobalData& globalData, size_t sizeInBytes)
119 RefPtr<ExecutableMemoryHandle> result = allocator->allocate(sizeInBytes);
121 releaseExecutableMemory(globalData);
122 result = allocator->allocate(sizeInBytes);
126 return result.release();
129 size_t ExecutableAllocator::committedByteCount()
131 return allocator->bytesCommitted();
134 #if ENABLE(META_ALLOCATOR_PROFILE)
135 void ExecutableAllocator::dumpProfile()
137 allocator->dumpProfile();
144 #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)