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