Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "debug.h" |
| 32 | #include "serialize.h" |
| 33 | #include "stub-cache.h" |
| 34 | #include "oprofile-agent.h" |
| 35 | |
| 36 | #if V8_TARGET_ARCH_ARM |
| 37 | #include "arm/simulator-arm.h" |
| 38 | #endif |
| 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | bool V8::is_running_ = false; |
| 44 | bool V8::has_been_setup_ = false; |
| 45 | bool V8::has_been_disposed_ = false; |
| 46 | bool V8::has_fatal_error_ = false; |
| 47 | |
| 48 | bool V8::Initialize(Deserializer *des) { |
| 49 | bool create_heap_objects = des == NULL; |
| 50 | if (has_been_disposed_ || has_fatal_error_) return false; |
| 51 | if (IsRunning()) return true; |
| 52 | |
| 53 | is_running_ = true; |
| 54 | has_been_setup_ = true; |
| 55 | has_fatal_error_ = false; |
| 56 | has_been_disposed_ = false; |
| 57 | #ifdef DEBUG |
| 58 | // The initialization process does not handle memory exhaustion. |
| 59 | DisallowAllocationFailure disallow_allocation_failure; |
| 60 | #endif |
| 61 | |
| 62 | // Enable logging before setting up the heap |
| 63 | Logger::Setup(); |
| 64 | if (des) des->GetLog(); |
| 65 | |
| 66 | // Setup the platform OS support. |
| 67 | OS::Setup(); |
| 68 | |
| 69 | // Initialize other runtime facilities |
| 70 | #if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM |
| 71 | ::assembler::arm::Simulator::Initialize(); |
| 72 | #endif |
| 73 | |
| 74 | { // NOLINT |
| 75 | // Ensure that the thread has a valid stack guard. The v8::Locker object |
| 76 | // will ensure this too, but we don't have to use lockers if we are only |
| 77 | // using one thread. |
| 78 | ExecutionAccess lock; |
| 79 | StackGuard::InitThread(lock); |
| 80 | } |
| 81 | |
| 82 | // Setup the object heap |
| 83 | ASSERT(!Heap::HasBeenSetup()); |
| 84 | if (!Heap::Setup(create_heap_objects)) { |
| 85 | SetFatalError(); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | Bootstrapper::Initialize(create_heap_objects); |
| 90 | Builtins::Setup(create_heap_objects); |
| 91 | Top::Initialize(); |
| 92 | |
| 93 | if (FLAG_preemption) { |
| 94 | v8::Locker locker; |
| 95 | v8::Locker::StartPreemption(100); |
| 96 | } |
| 97 | |
| 98 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 99 | Debug::Setup(create_heap_objects); |
| 100 | #endif |
| 101 | StubCache::Initialize(create_heap_objects); |
| 102 | |
| 103 | // If we are deserializing, read the state into the now-empty heap. |
| 104 | if (des != NULL) { |
| 105 | des->Deserialize(); |
| 106 | StubCache::Clear(); |
| 107 | } |
| 108 | |
| 109 | // Deserializing may put strange things in the root array's copy of the |
| 110 | // stack guard. |
| 111 | Heap::SetStackLimit(StackGuard::jslimit()); |
| 112 | |
| 113 | // Setup the CPU support. Must be done after heap setup and after |
| 114 | // any deserialization because we have to have the initial heap |
| 115 | // objects in place for creating the code object used for probing. |
| 116 | CPU::Setup(); |
| 117 | |
| 118 | OProfileAgent::Initialize(); |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void V8::SetFatalError() { |
| 125 | is_running_ = false; |
| 126 | has_fatal_error_ = true; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | void V8::TearDown() { |
| 131 | if (!has_been_setup_ || has_been_disposed_) return; |
| 132 | |
| 133 | OProfileAgent::TearDown(); |
| 134 | |
| 135 | if (FLAG_preemption) { |
| 136 | v8::Locker locker; |
| 137 | v8::Locker::StopPreemption(); |
| 138 | } |
| 139 | |
| 140 | Builtins::TearDown(); |
| 141 | Bootstrapper::TearDown(); |
| 142 | |
| 143 | Top::TearDown(); |
| 144 | |
| 145 | Heap::TearDown(); |
| 146 | Logger::TearDown(); |
| 147 | |
| 148 | is_running_ = false; |
| 149 | has_been_disposed_ = true; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | uint32_t V8::Random() { |
| 154 | // Random number generator using George Marsaglia's MWC algorithm. |
| 155 | static uint32_t hi = 0; |
| 156 | static uint32_t lo = 0; |
| 157 | |
| 158 | // Initialize seed using the system random(). If one of the seeds |
| 159 | // should ever become zero again, or if random() returns zero, we |
| 160 | // avoid getting stuck with zero bits in hi or lo by re-initializing |
| 161 | // them on demand. |
| 162 | if (hi == 0) hi = random(); |
| 163 | if (lo == 0) lo = random(); |
| 164 | |
| 165 | // Mix the bits. |
| 166 | hi = 36969 * (hi & 0xFFFF) + (hi >> 16); |
| 167 | lo = 18273 * (lo & 0xFFFF) + (lo >> 16); |
| 168 | return (hi << 16) + (lo & 0xFFFF); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | bool V8::IdleNotification(bool is_high_priority) { |
| 173 | // Returning true tells the caller that there is no need to call |
| 174 | // IdleNotification again. |
| 175 | if (!FLAG_use_idle_notification) return true; |
| 176 | // Ignore high priority instances of V8. |
| 177 | if (is_high_priority) return true; |
| 178 | |
| 179 | // Tell the heap that it may want to adjust. |
| 180 | return Heap::IdleNotification(); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | Smi* V8::RandomPositiveSmi() { |
| 185 | uint32_t random = Random(); |
| 186 | ASSERT(IsPowerOf2(Smi::kMaxValue + 1)); |
| 187 | return Smi::FromInt(random & Smi::kMaxValue); |
| 188 | } |
| 189 | |
| 190 | } } // namespace v8::internal |