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