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