initial import
[vuplus_webkit] / Source / JavaScriptCore / jit / ExecutableAllocatorFixedVMPool.cpp
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
30 #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
31
32 #include <errno.h>
33
34 #include <sys/mman.h>
35 #include <unistd.h>
36 #include <wtf/MetaAllocator.h>
37 #include <wtf/PageReservation.h>
38 #include <wtf/VMTags.h>
39
40 #if OS(LINUX)
41 #include <stdio.h>
42 #endif
43
44 using namespace WTF;
45
46 namespace JSC {
47     
48 #if CPU(ARM)
49 static const size_t fixedPoolSize = 16 * 1024 * 1024;
50 #elif CPU(X86_64)
51 static const size_t fixedPoolSize = 1024 * 1024 * 1024;
52 #else
53 static const size_t fixedPoolSize = 32 * 1024 * 1024;
54 #endif
55
56 class FixedVMPoolExecutableAllocator: public MetaAllocator {
57 public:
58     FixedVMPoolExecutableAllocator()
59         : MetaAllocator(32) // round up all allocations to 32 bytes
60     {
61         m_reservation = PageReservation::reserveWithGuardPages(fixedPoolSize, OSAllocator::JSJITCodePages, EXECUTABLE_POOL_WRITABLE, true);
62 #if !ENABLE(INTERPRETER)
63         if (!m_reservation)
64             CRASH();
65 #endif
66         if (m_reservation) {
67             ASSERT(m_reservation.size() == fixedPoolSize);
68             addFreshFreeSpace(m_reservation.base(), m_reservation.size());
69         }
70     }
71     
72 protected:
73     virtual void* allocateNewSpace(size_t&)
74     {
75         // We're operating in a fixed pool, so new allocation is always prohibited.
76         return 0;
77     }
78     
79     virtual void notifyNeedPage(void* page)
80     {
81         m_reservation.commit(page, pageSize());
82     }
83     
84     virtual void notifyPageIsFree(void* page)
85     {
86         m_reservation.decommit(page, pageSize());
87     }
88
89 private:
90     PageReservation m_reservation;
91 };
92
93 static FixedVMPoolExecutableAllocator* allocator;
94
95 void ExecutableAllocator::initializeAllocator()
96 {
97     ASSERT(!allocator);
98     allocator = new FixedVMPoolExecutableAllocator();
99 }
100
101 ExecutableAllocator::ExecutableAllocator(JSGlobalData&)
102 {
103     ASSERT(allocator);
104 }
105
106 bool ExecutableAllocator::isValid() const
107 {
108     return !!allocator->bytesReserved();
109 }
110
111 bool ExecutableAllocator::underMemoryPressure()
112 {
113     MetaAllocator::Statistics statistics = allocator->currentStatistics();
114     return statistics.bytesAllocated > statistics.bytesReserved / 2;
115 }
116
117 PassRefPtr<ExecutableMemoryHandle> ExecutableAllocator::allocate(JSGlobalData& globalData, size_t sizeInBytes)
118 {
119     RefPtr<ExecutableMemoryHandle> result = allocator->allocate(sizeInBytes);
120     if (!result) {
121         releaseExecutableMemory(globalData);
122         result = allocator->allocate(sizeInBytes);
123         if (!result)
124             CRASH();
125     }
126     return result.release();
127 }
128
129 size_t ExecutableAllocator::committedByteCount()
130 {
131     return allocator->bytesCommitted();
132 }
133
134 #if ENABLE(META_ALLOCATOR_PROFILE)
135 void ExecutableAllocator::dumpProfile()
136 {
137     allocator->dumpProfile();
138 }
139 #endif
140
141 }
142
143
144 #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED)