fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | #ifndef V8_HEAP_INL_H_ |
| 29 | #define V8_HEAP_INL_H_ |
| 30 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 31 | #include "heap.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 32 | #include "isolate.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 33 | #include "list-inl.h" |
| 34 | #include "objects.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "v8-counters.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 36 | #include "store-buffer.h" |
| 37 | #include "store-buffer-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 42 | void PromotionQueue::insert(HeapObject* target, int size) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 43 | if (emergency_stack_ != NULL) { |
| 44 | emergency_stack_->Add(Entry(target, size)); |
| 45 | return; |
| 46 | } |
| 47 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 48 | if (NewSpacePage::IsAtStart(reinterpret_cast<Address>(rear_))) { |
| 49 | NewSpacePage* rear_page = |
| 50 | NewSpacePage::FromAddress(reinterpret_cast<Address>(rear_)); |
| 51 | ASSERT(!rear_page->prev_page()->is_anchor()); |
| 52 | rear_ = reinterpret_cast<intptr_t*>(rear_page->prev_page()->body_limit()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 53 | ActivateGuardIfOnTheSamePage(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 54 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 55 | |
| 56 | if (guard_) { |
| 57 | ASSERT(GetHeadPage() == |
| 58 | Page::FromAllocationTop(reinterpret_cast<Address>(limit_))); |
| 59 | |
| 60 | if ((rear_ - 2) < limit_) { |
| 61 | RelocateQueueHead(); |
| 62 | emergency_stack_->Add(Entry(target, size)); |
| 63 | return; |
| 64 | } |
| 65 | } |
| 66 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | *(--rear_) = reinterpret_cast<intptr_t>(target); |
| 68 | *(--rear_) = size; |
| 69 | // Assert no overflow into live objects. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 70 | #ifdef DEBUG |
| 71 | SemiSpace::AssertValidRange(HEAP->new_space()->top(), |
| 72 | reinterpret_cast<Address>(rear_)); |
| 73 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 77 | void PromotionQueue::ActivateGuardIfOnTheSamePage() { |
| 78 | guard_ = guard_ || |
| 79 | heap_->new_space()->active_space()->current_page()->address() == |
| 80 | GetHeadPage()->address(); |
| 81 | } |
| 82 | |
| 83 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 84 | int Heap::MaxObjectSizeInPagedSpace() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | return Page::kMaxHeapObjectSize; |
| 86 | } |
| 87 | |
| 88 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 89 | MaybeObject* Heap::AllocateStringFromUtf8(Vector<const char> str, |
| 90 | PretenureFlag pretenure) { |
| 91 | // Check for ASCII first since this is the common case. |
| 92 | if (String::IsAscii(str.start(), str.length())) { |
| 93 | // If the string is ASCII, we do not need to convert the characters |
| 94 | // since UTF8 is backwards compatible with ASCII. |
| 95 | return AllocateStringFromAscii(str, pretenure); |
| 96 | } |
| 97 | // Non-ASCII and we need to decode. |
| 98 | return AllocateStringFromUtf8Slow(str, pretenure); |
| 99 | } |
| 100 | |
| 101 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 102 | MaybeObject* Heap::AllocateSymbol(Vector<const char> str, |
| 103 | int chars, |
| 104 | uint32_t hash_field) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 105 | unibrow::Utf8InputBuffer<> buffer(str.start(), |
| 106 | static_cast<unsigned>(str.length())); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 107 | return AllocateInternalSymbol(&buffer, chars, hash_field); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 111 | MaybeObject* Heap::AllocateAsciiSymbol(Vector<const char> str, |
| 112 | uint32_t hash_field) { |
| 113 | if (str.length() > SeqAsciiString::kMaxLength) { |
| 114 | return Failure::OutOfMemoryException(); |
| 115 | } |
| 116 | // Compute map and object size. |
| 117 | Map* map = ascii_symbol_map(); |
| 118 | int size = SeqAsciiString::SizeFor(str.length()); |
| 119 | |
| 120 | // Allocate string. |
| 121 | Object* result; |
| 122 | { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace()) |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 123 | ? lo_space_->AllocateRaw(size, NOT_EXECUTABLE) |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 124 | : old_data_space_->AllocateRaw(size); |
| 125 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 126 | } |
| 127 | |
| 128 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
| 129 | // Set length and hash fields of the allocated string. |
| 130 | String* answer = String::cast(result); |
| 131 | answer->set_length(str.length()); |
| 132 | answer->set_hash_field(hash_field); |
| 133 | |
| 134 | ASSERT_EQ(size, answer->Size()); |
| 135 | |
| 136 | // Fill in the characters. |
| 137 | memcpy(answer->address() + SeqAsciiString::kHeaderSize, |
| 138 | str.start(), str.length()); |
| 139 | |
| 140 | return answer; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | MaybeObject* Heap::AllocateTwoByteSymbol(Vector<const uc16> str, |
| 145 | uint32_t hash_field) { |
| 146 | if (str.length() > SeqTwoByteString::kMaxLength) { |
| 147 | return Failure::OutOfMemoryException(); |
| 148 | } |
| 149 | // Compute map and object size. |
| 150 | Map* map = symbol_map(); |
| 151 | int size = SeqTwoByteString::SizeFor(str.length()); |
| 152 | |
| 153 | // Allocate string. |
| 154 | Object* result; |
| 155 | { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace()) |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 156 | ? lo_space_->AllocateRaw(size, NOT_EXECUTABLE) |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 157 | : old_data_space_->AllocateRaw(size); |
| 158 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 159 | } |
| 160 | |
| 161 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
| 162 | // Set length and hash fields of the allocated string. |
| 163 | String* answer = String::cast(result); |
| 164 | answer->set_length(str.length()); |
| 165 | answer->set_hash_field(hash_field); |
| 166 | |
| 167 | ASSERT_EQ(size, answer->Size()); |
| 168 | |
| 169 | // Fill in the characters. |
| 170 | memcpy(answer->address() + SeqTwoByteString::kHeaderSize, |
| 171 | str.start(), str.length() * kUC16Size); |
| 172 | |
| 173 | return answer; |
| 174 | } |
| 175 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 176 | MaybeObject* Heap::CopyFixedArray(FixedArray* src) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 177 | return CopyFixedArrayWithMap(src, src->map()); |
| 178 | } |
| 179 | |
| 180 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 181 | MaybeObject* Heap::CopyFixedDoubleArray(FixedDoubleArray* src) { |
| 182 | return CopyFixedDoubleArrayWithMap(src, src->map()); |
| 183 | } |
| 184 | |
| 185 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 186 | MaybeObject* Heap::AllocateRaw(int size_in_bytes, |
| 187 | AllocationSpace space, |
| 188 | AllocationSpace retry_space) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 189 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 190 | ASSERT(space != NEW_SPACE || |
| 191 | retry_space == OLD_POINTER_SPACE || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 192 | retry_space == OLD_DATA_SPACE || |
| 193 | retry_space == LO_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 194 | #ifdef DEBUG |
| 195 | if (FLAG_gc_interval >= 0 && |
| 196 | !disallow_allocation_failure_ && |
| 197 | Heap::allocation_timeout_-- <= 0) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 198 | return Failure::RetryAfterGC(space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 200 | isolate_->counters()->objs_since_last_full()->Increment(); |
| 201 | isolate_->counters()->objs_since_last_young()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 203 | MaybeObject* result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | if (NEW_SPACE == space) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 205 | result = new_space_.AllocateRaw(size_in_bytes); |
| 206 | if (always_allocate() && result->IsFailure()) { |
| 207 | space = retry_space; |
| 208 | } else { |
| 209 | return result; |
| 210 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | } |
| 212 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 213 | if (OLD_POINTER_SPACE == space) { |
| 214 | result = old_pointer_space_->AllocateRaw(size_in_bytes); |
| 215 | } else if (OLD_DATA_SPACE == space) { |
| 216 | result = old_data_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | } else if (CODE_SPACE == space) { |
| 218 | result = code_space_->AllocateRaw(size_in_bytes); |
| 219 | } else if (LO_SPACE == space) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 220 | result = lo_space_->AllocateRaw(size_in_bytes, NOT_EXECUTABLE); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 221 | } else if (CELL_SPACE == space) { |
| 222 | result = cell_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | } else { |
| 224 | ASSERT(MAP_SPACE == space); |
| 225 | result = map_space_->AllocateRaw(size_in_bytes); |
| 226 | } |
| 227 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 228 | return result; |
| 229 | } |
| 230 | |
| 231 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 232 | MaybeObject* Heap::NumberFromInt32( |
| 233 | int32_t value, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 234 | if (Smi::IsValid(value)) return Smi::FromInt(value); |
| 235 | // Bypass NumberFromDouble to avoid various redundant checks. |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 236 | return AllocateHeapNumber(FastI2D(value), pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 240 | MaybeObject* Heap::NumberFromUint32( |
| 241 | uint32_t value, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) { |
| 243 | return Smi::FromInt((int32_t)value); |
| 244 | } |
| 245 | // Bypass NumberFromDouble to avoid various redundant checks. |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 246 | return AllocateHeapNumber(FastUI2D(value), pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 250 | void Heap::FinalizeExternalString(String* string) { |
| 251 | ASSERT(string->IsExternalString()); |
| 252 | v8::String::ExternalStringResourceBase** resource_addr = |
| 253 | reinterpret_cast<v8::String::ExternalStringResourceBase**>( |
| 254 | reinterpret_cast<byte*>(string) + |
| 255 | ExternalString::kResourceOffset - |
| 256 | kHeapObjectTag); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 257 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 258 | // Dispose of the C++ object if it has not already been disposed. |
| 259 | if (*resource_addr != NULL) { |
| 260 | (*resource_addr)->Dispose(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame^] | 261 | *resource_addr = NULL; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 262 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 266 | MaybeObject* Heap::AllocateRawMap() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 268 | isolate_->counters()->objs_since_last_full()->Increment(); |
| 269 | isolate_->counters()->objs_since_last_young()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 271 | MaybeObject* result = map_space_->AllocateRaw(Map::kSize); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 272 | if (result->IsFailure()) old_gen_exhausted_ = true; |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 273 | #ifdef DEBUG |
| 274 | if (!result->IsFailure()) { |
| 275 | // Maps have their own alignment. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 276 | CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) == |
| 277 | static_cast<intptr_t>(kHeapObjectTag)); |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 278 | } |
| 279 | #endif |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 280 | return result; |
| 281 | } |
| 282 | |
| 283 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 284 | MaybeObject* Heap::AllocateRawCell() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 285 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 286 | isolate_->counters()->objs_since_last_full()->Increment(); |
| 287 | isolate_->counters()->objs_since_last_young()->Increment(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 288 | #endif |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 289 | MaybeObject* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | bool Heap::InNewSpace(Object* object) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 296 | bool result = new_space_.Contains(object); |
| 297 | ASSERT(!result || // Either not in new space |
| 298 | gc_state_ != NOT_IN_GC || // ... or in the middle of GC |
| 299 | InToSpace(object)); // ... or in to-space (where we allocate). |
| 300 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 304 | bool Heap::InNewSpace(Address addr) { |
| 305 | return new_space_.Contains(addr); |
| 306 | } |
| 307 | |
| 308 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 309 | bool Heap::InFromSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 310 | return new_space_.FromSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
| 314 | bool Heap::InToSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 315 | return new_space_.ToSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 319 | bool Heap::OldGenerationAllocationLimitReached() { |
| 320 | if (!incremental_marking()->IsStopped()) return false; |
| 321 | return OldGenerationSpaceAvailable() < 0; |
| 322 | } |
| 323 | |
| 324 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | bool Heap::ShouldBePromoted(Address old_address, int object_size) { |
| 326 | // An object should be promoted if: |
| 327 | // - the object has survived a scavenge operation or |
| 328 | // - to space is already 25% full. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 329 | NewSpacePage* page = NewSpacePage::FromAddress(old_address); |
| 330 | Address age_mark = new_space_.age_mark(); |
| 331 | bool below_mark = page->IsFlagSet(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK) && |
| 332 | (!page->ContainsLimit(age_mark) || old_address < age_mark); |
| 333 | return below_mark || (new_space_.Size() + object_size) >= |
| 334 | (new_space_.EffectiveCapacity() >> 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
| 338 | void Heap::RecordWrite(Address address, int offset) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 339 | if (!InNewSpace(address)) store_buffer_.Mark(address + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 343 | void Heap::RecordWrites(Address address, int start, int len) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 344 | if (!InNewSpace(address)) { |
| 345 | for (int i = 0; i < len; i++) { |
| 346 | store_buffer_.Mark(address + start + i * kPointerSize); |
| 347 | } |
| 348 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 352 | OldSpace* Heap::TargetSpace(HeapObject* object) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 353 | InstanceType type = object->map()->instance_type(); |
| 354 | AllocationSpace space = TargetSpaceId(type); |
| 355 | return (space == OLD_POINTER_SPACE) |
| 356 | ? old_pointer_space_ |
| 357 | : old_data_space_; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | AllocationSpace Heap::TargetSpaceId(InstanceType type) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 362 | // Heap numbers and sequential strings are promoted to old data space, all |
| 363 | // other object types are promoted to old pointer space. We do not use |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 364 | // object->IsHeapNumber() and object->IsSeqString() because we already |
| 365 | // know that object has the heap object tag. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 366 | |
| 367 | // These objects are never allocated in new space. |
| 368 | ASSERT(type != MAP_TYPE); |
| 369 | ASSERT(type != CODE_TYPE); |
| 370 | ASSERT(type != ODDBALL_TYPE); |
| 371 | ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE); |
| 372 | |
| 373 | if (type < FIRST_NONSTRING_TYPE) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 374 | // There are four string representations: sequential strings, external |
| 375 | // strings, cons strings, and sliced strings. |
| 376 | // Only the latter two contain non-map-word pointers to heap objects. |
| 377 | return ((type & kIsIndirectStringMask) == kIsIndirectStringTag) |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 378 | ? OLD_POINTER_SPACE |
| 379 | : OLD_DATA_SPACE; |
| 380 | } else { |
| 381 | return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE; |
| 382 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 386 | void Heap::CopyBlock(Address dst, Address src, int byte_size) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 387 | CopyWords(reinterpret_cast<Object**>(dst), |
| 388 | reinterpret_cast<Object**>(src), |
| 389 | byte_size / kPointerSize); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 390 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 392 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 393 | void Heap::MoveBlock(Address dst, Address src, int byte_size) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 394 | ASSERT(IsAligned(byte_size, kPointerSize)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 395 | |
| 396 | int size_in_words = byte_size / kPointerSize; |
| 397 | |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 398 | if ((dst < src) || (dst >= (src + byte_size))) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 399 | Object** src_slot = reinterpret_cast<Object**>(src); |
| 400 | Object** dst_slot = reinterpret_cast<Object**>(dst); |
| 401 | Object** end_slot = src_slot + size_in_words; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 402 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 403 | while (src_slot != end_slot) { |
| 404 | *dst_slot++ = *src_slot++; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 405 | } |
| 406 | } else { |
| 407 | memmove(dst, src, byte_size); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 412 | void Heap::ScavengePointer(HeapObject** p) { |
| 413 | ScavengeObject(p, *p); |
| 414 | } |
| 415 | |
| 416 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 417 | void Heap::ScavengeObject(HeapObject** p, HeapObject* object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 418 | ASSERT(HEAP->InFromSpace(object)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 419 | |
| 420 | // We use the first word (where the map pointer usually is) of a heap |
| 421 | // object to record the forwarding pointer. A forwarding pointer can |
| 422 | // point to an old space, the code space, or the to space of the new |
| 423 | // generation. |
| 424 | MapWord first_word = object->map_word(); |
| 425 | |
| 426 | // If the first word is a forwarding address, the object has already been |
| 427 | // copied. |
| 428 | if (first_word.IsForwardingAddress()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 429 | HeapObject* dest = first_word.ToForwardingAddress(); |
| 430 | ASSERT(HEAP->InFromSpace(*p)); |
| 431 | *p = dest; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
| 435 | // Call the slow part of scavenge object. |
| 436 | return ScavengeObjectSlow(p, object); |
| 437 | } |
| 438 | |
| 439 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame] | 440 | bool Heap::CollectGarbage(AllocationSpace space) { |
| 441 | return CollectGarbage(space, SelectGarbageCollector(space)); |
| 442 | } |
| 443 | |
| 444 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 445 | MaybeObject* Heap::PrepareForCompare(String* str) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 446 | // Always flatten small strings and force flattening of long strings |
| 447 | // after we have accumulated a certain amount we failed to flatten. |
| 448 | static const int kMaxAlwaysFlattenLength = 32; |
| 449 | static const int kFlattenLongThreshold = 16*KB; |
| 450 | |
| 451 | const int length = str->length(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 452 | MaybeObject* obj = str->TryFlatten(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 453 | if (length <= kMaxAlwaysFlattenLength || |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 454 | unflattened_strings_length_ >= kFlattenLongThreshold) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 455 | return obj; |
| 456 | } |
| 457 | if (obj->IsFailure()) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 458 | unflattened_strings_length_ += length; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 459 | } |
| 460 | return str; |
| 461 | } |
| 462 | |
| 463 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 464 | int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) { |
| 465 | ASSERT(HasBeenSetup()); |
| 466 | int amount = amount_of_external_allocated_memory_ + change_in_bytes; |
| 467 | if (change_in_bytes >= 0) { |
| 468 | // Avoid overflow. |
| 469 | if (amount > amount_of_external_allocated_memory_) { |
| 470 | amount_of_external_allocated_memory_ = amount; |
| 471 | } |
| 472 | int amount_since_last_global_gc = |
| 473 | amount_of_external_allocated_memory_ - |
| 474 | amount_of_external_allocated_memory_at_last_global_gc_; |
| 475 | if (amount_since_last_global_gc > external_allocation_limit_) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 476 | CollectAllGarbage(kNoGCFlags); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 477 | } |
| 478 | } else { |
| 479 | // Avoid underflow. |
| 480 | if (amount >= 0) { |
| 481 | amount_of_external_allocated_memory_ = amount; |
| 482 | } |
| 483 | } |
| 484 | ASSERT(amount_of_external_allocated_memory_ >= 0); |
| 485 | return amount_of_external_allocated_memory_; |
| 486 | } |
| 487 | |
| 488 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 489 | void Heap::SetLastScriptId(Object* last_script_id) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 490 | roots_[kLastScriptIdRootIndex] = last_script_id; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 491 | } |
| 492 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 493 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 494 | Isolate* Heap::isolate() { |
| 495 | return reinterpret_cast<Isolate*>(reinterpret_cast<intptr_t>(this) - |
| 496 | reinterpret_cast<size_t>(reinterpret_cast<Isolate*>(4)->heap()) + 4); |
| 497 | } |
| 498 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 499 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 500 | #ifdef DEBUG |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 501 | #define GC_GREEDY_CHECK() \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 502 | if (FLAG_gc_greedy) HEAP->GarbageCollectionGreedyCheck() |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 503 | #else |
| 504 | #define GC_GREEDY_CHECK() { } |
| 505 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 507 | |
| 508 | // Calls the FUNCTION_CALL function and retries it up to three times |
| 509 | // to guarantee that any allocations performed during the call will |
| 510 | // succeed if there's enough memory. |
| 511 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 512 | // Warning: Do not use the identifiers __object__, __maybe_object__ or |
| 513 | // __scope__ in a call to this macro. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 514 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 515 | #define CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY)\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 516 | do { \ |
| 517 | GC_GREEDY_CHECK(); \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 518 | MaybeObject* __maybe_object__ = FUNCTION_CALL; \ |
| 519 | Object* __object__ = NULL; \ |
| 520 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 521 | if (__maybe_object__->IsOutOfMemory()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 522 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 523 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 524 | if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 525 | ISOLATE->heap()->CollectGarbage(Failure::cast(__maybe_object__)-> \ |
| 526 | allocation_space()); \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 527 | __maybe_object__ = FUNCTION_CALL; \ |
| 528 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 529 | if (__maybe_object__->IsOutOfMemory()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 530 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 531 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 532 | if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 533 | ISOLATE->counters()->gc_last_resort_from_handles()->Increment(); \ |
| 534 | ISOLATE->heap()->CollectAllAvailableGarbage(); \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 535 | { \ |
| 536 | AlwaysAllocateScope __scope__; \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 537 | __maybe_object__ = FUNCTION_CALL; \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 538 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 539 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 540 | if (__maybe_object__->IsOutOfMemory() || \ |
| 541 | __maybe_object__->IsRetryAfterGC()) { \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 542 | /* TODO(1181417): Fix this. */ \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 543 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 544 | } \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 545 | RETURN_EMPTY; \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | } while (false) |
| 547 | |
| 548 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 549 | #define CALL_HEAP_FUNCTION(ISOLATE, FUNCTION_CALL, TYPE) \ |
| 550 | CALL_AND_RETRY(ISOLATE, \ |
| 551 | FUNCTION_CALL, \ |
| 552 | return Handle<TYPE>(TYPE::cast(__object__), ISOLATE), \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 553 | return Handle<TYPE>()) |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 554 | |
| 555 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 556 | #define CALL_HEAP_FUNCTION_VOID(ISOLATE, FUNCTION_CALL) \ |
| 557 | CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, return, return) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 558 | |
| 559 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 560 | #ifdef DEBUG |
| 561 | |
| 562 | inline bool Heap::allow_allocation(bool new_state) { |
| 563 | bool old = allocation_allowed_; |
| 564 | allocation_allowed_ = new_state; |
| 565 | return old; |
| 566 | } |
| 567 | |
| 568 | #endif |
| 569 | |
| 570 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 571 | void ExternalStringTable::AddString(String* string) { |
| 572 | ASSERT(string->IsExternalString()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 573 | if (heap_->InNewSpace(string)) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 574 | new_space_strings_.Add(string); |
| 575 | } else { |
| 576 | old_space_strings_.Add(string); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | |
| 581 | void ExternalStringTable::Iterate(ObjectVisitor* v) { |
| 582 | if (!new_space_strings_.is_empty()) { |
| 583 | Object** start = &new_space_strings_[0]; |
| 584 | v->VisitPointers(start, start + new_space_strings_.length()); |
| 585 | } |
| 586 | if (!old_space_strings_.is_empty()) { |
| 587 | Object** start = &old_space_strings_[0]; |
| 588 | v->VisitPointers(start, start + old_space_strings_.length()); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | |
| 593 | // Verify() is inline to avoid ifdef-s around its calls in release |
| 594 | // mode. |
| 595 | void ExternalStringTable::Verify() { |
| 596 | #ifdef DEBUG |
| 597 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 598 | ASSERT(heap_->InNewSpace(new_space_strings_[i])); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 599 | ASSERT(new_space_strings_[i] != HEAP->raw_unchecked_the_hole_value()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 600 | } |
| 601 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 602 | ASSERT(!heap_->InNewSpace(old_space_strings_[i])); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 603 | ASSERT(old_space_strings_[i] != HEAP->raw_unchecked_the_hole_value()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 604 | } |
| 605 | #endif |
| 606 | } |
| 607 | |
| 608 | |
| 609 | void ExternalStringTable::AddOldString(String* string) { |
| 610 | ASSERT(string->IsExternalString()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 611 | ASSERT(!heap_->InNewSpace(string)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 612 | old_space_strings_.Add(string); |
| 613 | } |
| 614 | |
| 615 | |
| 616 | void ExternalStringTable::ShrinkNewStrings(int position) { |
| 617 | new_space_strings_.Rewind(position); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 618 | if (FLAG_verify_heap) { |
| 619 | Verify(); |
| 620 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 621 | } |
| 622 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 623 | |
| 624 | void Heap::ClearInstanceofCache() { |
| 625 | set_instanceof_cache_function(the_hole_value()); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | Object* Heap::ToBoolean(bool condition) { |
| 630 | return condition ? true_value() : false_value(); |
| 631 | } |
| 632 | |
| 633 | |
| 634 | void Heap::CompletelyClearInstanceofCache() { |
| 635 | set_instanceof_cache_map(the_hole_value()); |
| 636 | set_instanceof_cache_function(the_hole_value()); |
| 637 | } |
| 638 | |
| 639 | |
| 640 | MaybeObject* TranscendentalCache::Get(Type type, double input) { |
| 641 | SubCache* cache = caches_[type]; |
| 642 | if (cache == NULL) { |
| 643 | caches_[type] = cache = new SubCache(type); |
| 644 | } |
| 645 | return cache->Get(input); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | Address TranscendentalCache::cache_array_address() { |
| 650 | return reinterpret_cast<Address>(caches_); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | double TranscendentalCache::SubCache::Calculate(double input) { |
| 655 | switch (type_) { |
| 656 | case ACOS: |
| 657 | return acos(input); |
| 658 | case ASIN: |
| 659 | return asin(input); |
| 660 | case ATAN: |
| 661 | return atan(input); |
| 662 | case COS: |
| 663 | return cos(input); |
| 664 | case EXP: |
| 665 | return exp(input); |
| 666 | case LOG: |
| 667 | return log(input); |
| 668 | case SIN: |
| 669 | return sin(input); |
| 670 | case TAN: |
| 671 | return tan(input); |
| 672 | default: |
| 673 | return 0.0; // Never happens. |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | |
| 678 | MaybeObject* TranscendentalCache::SubCache::Get(double input) { |
| 679 | Converter c; |
| 680 | c.dbl = input; |
| 681 | int hash = Hash(c); |
| 682 | Element e = elements_[hash]; |
| 683 | if (e.in[0] == c.integers[0] && |
| 684 | e.in[1] == c.integers[1]) { |
| 685 | ASSERT(e.output != NULL); |
| 686 | isolate_->counters()->transcendental_cache_hit()->Increment(); |
| 687 | return e.output; |
| 688 | } |
| 689 | double answer = Calculate(input); |
| 690 | isolate_->counters()->transcendental_cache_miss()->Increment(); |
| 691 | Object* heap_number; |
| 692 | { MaybeObject* maybe_heap_number = |
| 693 | isolate_->heap()->AllocateHeapNumber(answer); |
| 694 | if (!maybe_heap_number->ToObject(&heap_number)) return maybe_heap_number; |
| 695 | } |
| 696 | elements_[hash].in[0] = c.integers[0]; |
| 697 | elements_[hash].in[1] = c.integers[1]; |
| 698 | elements_[hash].output = heap_number; |
| 699 | return heap_number; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | Heap* _inline_get_heap_() { |
| 704 | return HEAP; |
| 705 | } |
| 706 | |
| 707 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 708 | } } // namespace v8::internal |
| 709 | |
| 710 | #endif // V8_HEAP_INL_H_ |