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 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 463 | intptr_t Heap::AdjustAmountOfExternalAllocatedMemory( |
| 464 | intptr_t change_in_bytes) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 465 | ASSERT(HasBeenSetUp()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 466 | intptr_t amount = amount_of_external_allocated_memory_ + change_in_bytes; |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 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 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 472 | intptr_t amount_since_last_global_gc = |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 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_) { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 476 | CollectAllGarbage(kNoGCFlags, "external memory allocation limit reached"); |
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 | // Calls the FUNCTION_CALL function and retries it up to three times |
| 508 | // to guarantee that any allocations performed during the call will |
| 509 | // succeed if there's enough memory. |
| 510 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 511 | // Warning: Do not use the identifiers __object__, __maybe_object__ or |
| 512 | // __scope__ in a call to this macro. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 513 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 514 | #define CALL_AND_RETRY(ISOLATE, FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY)\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 515 | do { \ |
| 516 | GC_GREEDY_CHECK(); \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 517 | MaybeObject* __maybe_object__ = FUNCTION_CALL; \ |
| 518 | Object* __object__ = NULL; \ |
| 519 | if (__maybe_object__->ToObject(&__object__)) RETURN_VALUE; \ |
| 520 | if (__maybe_object__->IsOutOfMemory()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 521 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 522 | } \ |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 523 | if (!__maybe_object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 524 | ISOLATE->heap()->CollectGarbage(Failure::cast(__maybe_object__)-> \ |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 525 | allocation_space(), \ |
| 526 | "allocation failure"); \ |
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(); \ |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 534 | ISOLATE->heap()->CollectAllAvailableGarbage("last resort gc"); \ |
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) { |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 598 | Object* obj = Object::cast(new_space_strings_[i]); |
| 599 | // TODO(yangguo): check that the object is indeed an external string. |
| 600 | ASSERT(heap_->InNewSpace(obj)); |
| 601 | ASSERT(obj != HEAP->raw_unchecked_the_hole_value()); |
| 602 | if (obj->IsExternalAsciiString()) { |
| 603 | ExternalAsciiString* string = ExternalAsciiString::cast(obj); |
| 604 | ASSERT(String::IsAscii(string->GetChars(), string->length())); |
| 605 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 606 | } |
| 607 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame] | 608 | Object* obj = Object::cast(old_space_strings_[i]); |
| 609 | // TODO(yangguo): check that the object is indeed an external string. |
| 610 | ASSERT(!heap_->InNewSpace(obj)); |
| 611 | ASSERT(obj != HEAP->raw_unchecked_the_hole_value()); |
| 612 | if (obj->IsExternalAsciiString()) { |
| 613 | ExternalAsciiString* string = ExternalAsciiString::cast(obj); |
| 614 | ASSERT(String::IsAscii(string->GetChars(), string->length())); |
| 615 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 616 | } |
| 617 | #endif |
| 618 | } |
| 619 | |
| 620 | |
| 621 | void ExternalStringTable::AddOldString(String* string) { |
| 622 | ASSERT(string->IsExternalString()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 623 | ASSERT(!heap_->InNewSpace(string)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 624 | old_space_strings_.Add(string); |
| 625 | } |
| 626 | |
| 627 | |
| 628 | void ExternalStringTable::ShrinkNewStrings(int position) { |
| 629 | new_space_strings_.Rewind(position); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 630 | if (FLAG_verify_heap) { |
| 631 | Verify(); |
| 632 | } |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 633 | } |
| 634 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 635 | |
| 636 | void Heap::ClearInstanceofCache() { |
| 637 | set_instanceof_cache_function(the_hole_value()); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | Object* Heap::ToBoolean(bool condition) { |
| 642 | return condition ? true_value() : false_value(); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void Heap::CompletelyClearInstanceofCache() { |
| 647 | set_instanceof_cache_map(the_hole_value()); |
| 648 | set_instanceof_cache_function(the_hole_value()); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | MaybeObject* TranscendentalCache::Get(Type type, double input) { |
| 653 | SubCache* cache = caches_[type]; |
| 654 | if (cache == NULL) { |
| 655 | caches_[type] = cache = new SubCache(type); |
| 656 | } |
| 657 | return cache->Get(input); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | Address TranscendentalCache::cache_array_address() { |
| 662 | return reinterpret_cast<Address>(caches_); |
| 663 | } |
| 664 | |
| 665 | |
| 666 | double TranscendentalCache::SubCache::Calculate(double input) { |
| 667 | switch (type_) { |
| 668 | case ACOS: |
| 669 | return acos(input); |
| 670 | case ASIN: |
| 671 | return asin(input); |
| 672 | case ATAN: |
| 673 | return atan(input); |
| 674 | case COS: |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 675 | return fast_cos(input); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 676 | case EXP: |
| 677 | return exp(input); |
| 678 | case LOG: |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 679 | return fast_log(input); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 680 | case SIN: |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 681 | return fast_sin(input); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 682 | case TAN: |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 683 | return fast_tan(input); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 684 | default: |
| 685 | return 0.0; // Never happens. |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | |
| 690 | MaybeObject* TranscendentalCache::SubCache::Get(double input) { |
| 691 | Converter c; |
| 692 | c.dbl = input; |
| 693 | int hash = Hash(c); |
| 694 | Element e = elements_[hash]; |
| 695 | if (e.in[0] == c.integers[0] && |
| 696 | e.in[1] == c.integers[1]) { |
| 697 | ASSERT(e.output != NULL); |
| 698 | isolate_->counters()->transcendental_cache_hit()->Increment(); |
| 699 | return e.output; |
| 700 | } |
| 701 | double answer = Calculate(input); |
| 702 | isolate_->counters()->transcendental_cache_miss()->Increment(); |
| 703 | Object* heap_number; |
| 704 | { MaybeObject* maybe_heap_number = |
| 705 | isolate_->heap()->AllocateHeapNumber(answer); |
| 706 | if (!maybe_heap_number->ToObject(&heap_number)) return maybe_heap_number; |
| 707 | } |
| 708 | elements_[hash].in[0] = c.integers[0]; |
| 709 | elements_[hash].in[1] = c.integers[1]; |
| 710 | elements_[hash].output = heap_number; |
| 711 | return heap_number; |
| 712 | } |
| 713 | |
| 714 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 715 | AlwaysAllocateScope::AlwaysAllocateScope() { |
| 716 | // We shouldn't hit any nested scopes, because that requires |
| 717 | // non-handle code to call handle code. The code still works but |
| 718 | // performance will degrade, so we want to catch this situation |
| 719 | // in debug mode. |
| 720 | ASSERT(HEAP->always_allocate_scope_depth_ == 0); |
| 721 | HEAP->always_allocate_scope_depth_++; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 725 | AlwaysAllocateScope::~AlwaysAllocateScope() { |
| 726 | HEAP->always_allocate_scope_depth_--; |
| 727 | ASSERT(HEAP->always_allocate_scope_depth_ == 0); |
| 728 | } |
| 729 | |
| 730 | |
| 731 | LinearAllocationScope::LinearAllocationScope() { |
| 732 | HEAP->linear_allocation_scope_depth_++; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | LinearAllocationScope::~LinearAllocationScope() { |
| 737 | HEAP->linear_allocation_scope_depth_--; |
| 738 | ASSERT(HEAP->linear_allocation_scope_depth_ >= 0); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | #ifdef DEBUG |
| 743 | void VerifyPointersVisitor::VisitPointers(Object** start, Object** end) { |
| 744 | for (Object** current = start; current < end; current++) { |
| 745 | if ((*current)->IsHeapObject()) { |
| 746 | HeapObject* object = HeapObject::cast(*current); |
| 747 | ASSERT(HEAP->Contains(object)); |
| 748 | ASSERT(object->map()->IsMap()); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | #endif |
| 753 | |
| 754 | |
| 755 | double GCTracer::SizeOfHeapObjects() { |
| 756 | return (static_cast<double>(HEAP->SizeOfObjects())) / MB; |
| 757 | } |
| 758 | |
| 759 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 760 | DisallowAllocationFailure::DisallowAllocationFailure() { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame^] | 761 | #ifdef DEBUG |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 762 | old_state_ = HEAP->disallow_allocation_failure_; |
| 763 | HEAP->disallow_allocation_failure_ = true; |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame^] | 764 | #endif |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | |
| 768 | DisallowAllocationFailure::~DisallowAllocationFailure() { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame^] | 769 | #ifdef DEBUG |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 770 | HEAP->disallow_allocation_failure_ = old_state_; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 771 | #endif |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame^] | 772 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 773 | |
| 774 | |
| 775 | #ifdef DEBUG |
| 776 | AssertNoAllocation::AssertNoAllocation() { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 777 | Isolate* isolate = ISOLATE; |
| 778 | active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread(); |
| 779 | if (active_) { |
| 780 | old_state_ = isolate->heap()->allow_allocation(false); |
| 781 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | |
| 785 | AssertNoAllocation::~AssertNoAllocation() { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 786 | if (active_) HEAP->allow_allocation(old_state_); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | |
| 790 | DisableAssertNoAllocation::DisableAssertNoAllocation() { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 791 | Isolate* isolate = ISOLATE; |
| 792 | active_ = !isolate->optimizing_compiler_thread()->IsOptimizerThread(); |
| 793 | if (active_) { |
| 794 | old_state_ = isolate->heap()->allow_allocation(true); |
| 795 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
| 799 | DisableAssertNoAllocation::~DisableAssertNoAllocation() { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 800 | if (active_) HEAP->allow_allocation(old_state_); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | #else |
| 804 | |
| 805 | AssertNoAllocation::AssertNoAllocation() { } |
| 806 | AssertNoAllocation::~AssertNoAllocation() { } |
| 807 | DisableAssertNoAllocation::DisableAssertNoAllocation() { } |
| 808 | DisableAssertNoAllocation::~DisableAssertNoAllocation() { } |
| 809 | |
| 810 | #endif |
| 811 | |
| 812 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 813 | } } // namespace v8::internal |
| 814 | |
| 815 | #endif // V8_HEAP_INL_H_ |