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