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