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