• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "simulator.h"
34 #include "stub-cache.h"
35 #include "oprofile-agent.h"
36 #include "log.h"
37 
38 namespace v8 {
39 namespace internal {
40 
41 bool V8::is_running_ = false;
42 bool V8::has_been_setup_ = false;
43 bool V8::has_been_disposed_ = false;
44 bool V8::has_fatal_error_ = false;
45 
Initialize(Deserializer * des)46 bool V8::Initialize(Deserializer *des) {
47   bool create_heap_objects = des == NULL;
48   if (has_been_disposed_ || has_fatal_error_) return false;
49   if (IsRunning()) return true;
50 
51   is_running_ = true;
52   has_been_setup_ = true;
53   has_fatal_error_ = false;
54   has_been_disposed_ = false;
55 #ifdef DEBUG
56   // The initialization process does not handle memory exhaustion.
57   DisallowAllocationFailure disallow_allocation_failure;
58 #endif
59 
60   // Enable logging before setting up the heap
61   Logger::Setup();
62 
63   // Setup the platform OS support.
64   OS::Setup();
65 
66   // Initialize other runtime facilities
67 #if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM
68   ::assembler::arm::Simulator::Initialize();
69 #endif
70 
71   { // NOLINT
72     // Ensure that the thread has a valid stack guard.  The v8::Locker object
73     // will ensure this too, but we don't have to use lockers if we are only
74     // using one thread.
75     ExecutionAccess lock;
76     StackGuard::InitThread(lock);
77   }
78 
79   // Setup the object heap
80   ASSERT(!Heap::HasBeenSetup());
81   if (!Heap::Setup(create_heap_objects)) {
82     SetFatalError();
83     return false;
84   }
85 
86   Bootstrapper::Initialize(create_heap_objects);
87   Builtins::Setup(create_heap_objects);
88   Top::Initialize();
89 
90   if (FLAG_preemption) {
91     v8::Locker locker;
92     v8::Locker::StartPreemption(100);
93   }
94 
95 #ifdef ENABLE_DEBUGGER_SUPPORT
96   Debug::Setup(create_heap_objects);
97 #endif
98   StubCache::Initialize(create_heap_objects);
99 
100   // If we are deserializing, read the state into the now-empty heap.
101   if (des != NULL) {
102     des->Deserialize();
103     StubCache::Clear();
104   }
105 
106   // Deserializing may put strange things in the root array's copy of the
107   // stack guard.
108   Heap::SetStackLimits();
109 
110   // Setup the CPU support. Must be done after heap setup and after
111   // any deserialization because we have to have the initial heap
112   // objects in place for creating the code object used for probing.
113   CPU::Setup();
114 
115   OProfileAgent::Initialize();
116 
117   // If we are deserializing, log non-function code objects and compiled
118   // functions found in the snapshot.
119   if (des != NULL && FLAG_log_code) {
120     HandleScope scope;
121     LOG(LogCodeObjects());
122     LOG(LogCompiledFunctions());
123   }
124 
125   return true;
126 }
127 
128 
SetFatalError()129 void V8::SetFatalError() {
130   is_running_ = false;
131   has_fatal_error_ = true;
132 }
133 
134 
TearDown()135 void V8::TearDown() {
136   if (!has_been_setup_ || has_been_disposed_) return;
137 
138   OProfileAgent::TearDown();
139 
140   if (FLAG_preemption) {
141     v8::Locker locker;
142     v8::Locker::StopPreemption();
143   }
144 
145   Builtins::TearDown();
146   Bootstrapper::TearDown();
147 
148   Top::TearDown();
149 
150   Heap::TearDown();
151   Logger::TearDown();
152 
153   is_running_ = false;
154   has_been_disposed_ = true;
155 }
156 
157 
Random()158 uint32_t V8::Random() {
159   // Random number generator using George Marsaglia's MWC algorithm.
160   static uint32_t hi = 0;
161   static uint32_t lo = 0;
162 
163   // Initialize seed using the system random(). If one of the seeds
164   // should ever become zero again, or if random() returns zero, we
165   // avoid getting stuck with zero bits in hi or lo by re-initializing
166   // them on demand.
167   if (hi == 0) hi = random();
168   if (lo == 0) lo = random();
169 
170   // Mix the bits.
171   hi = 36969 * (hi & 0xFFFF) + (hi >> 16);
172   lo = 18273 * (lo & 0xFFFF) + (lo >> 16);
173   return (hi << 16) + (lo & 0xFFFF);
174 }
175 
176 
IdleNotification()177 bool V8::IdleNotification() {
178   // Returning true tells the caller that there is no need to call
179   // IdleNotification again.
180   if (!FLAG_use_idle_notification) return true;
181 
182   // Tell the heap that it may want to adjust.
183   return Heap::IdleNotification();
184 }
185 
186 static const uint32_t kRandomPositiveSmiMax = 0x3fffffff;
187 
RandomPositiveSmi()188 Smi* V8::RandomPositiveSmi() {
189   uint32_t random = Random();
190   ASSERT(static_cast<uint32_t>(Smi::kMaxValue) >= kRandomPositiveSmiMax);
191   // kRandomPositiveSmiMax must match the value being divided
192   // by in math.js.
193   return Smi::FromInt(random & kRandomPositiveSmiMax);
194 }
195 
196 } }  // namespace v8::internal
197