ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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" |
| 32 | #include "objects.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "v8-counters.h" |
| 34 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 35 | namespace v8 { |
| 36 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 38 | void Heap::UpdateOldSpaceLimits() { |
| 39 | int old_gen_size = PromotedSpaceSize(); |
| 40 | old_gen_promotion_limit_ = |
| 41 | old_gen_size + Max(kMinimumPromotionLimit, old_gen_size / 3); |
| 42 | old_gen_allocation_limit_ = |
| 43 | old_gen_size + Max(kMinimumAllocationLimit, old_gen_size / 2); |
| 44 | old_gen_exhausted_ = false; |
| 45 | } |
| 46 | |
| 47 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 48 | int Heap::MaxObjectSizeInPagedSpace() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | return Page::kMaxHeapObjectSize; |
| 50 | } |
| 51 | |
| 52 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 53 | Object* Heap::AllocateSymbol(Vector<const char> str, |
| 54 | int chars, |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 55 | uint32_t hash_field) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 56 | unibrow::Utf8InputBuffer<> buffer(str.start(), |
| 57 | static_cast<unsigned>(str.length())); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 58 | return AllocateInternalSymbol(&buffer, chars, hash_field); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame^] | 62 | Object* Heap::CopyFixedArray(FixedArray* src) { |
| 63 | return CopyFixedArrayWithMap(src, src->map()); |
| 64 | } |
| 65 | |
| 66 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 67 | Object* Heap::AllocateRaw(int size_in_bytes, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 68 | AllocationSpace space, |
| 69 | AllocationSpace retry_space) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 71 | ASSERT(space != NEW_SPACE || |
| 72 | retry_space == OLD_POINTER_SPACE || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 73 | retry_space == OLD_DATA_SPACE || |
| 74 | retry_space == LO_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | #ifdef DEBUG |
| 76 | if (FLAG_gc_interval >= 0 && |
| 77 | !disallow_allocation_failure_ && |
| 78 | Heap::allocation_timeout_-- <= 0) { |
| 79 | return Failure::RetryAfterGC(size_in_bytes, space); |
| 80 | } |
| 81 | Counters::objs_since_last_full.Increment(); |
| 82 | Counters::objs_since_last_young.Increment(); |
| 83 | #endif |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 84 | Object* result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 85 | if (NEW_SPACE == space) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 86 | result = new_space_.AllocateRaw(size_in_bytes); |
| 87 | if (always_allocate() && result->IsFailure()) { |
| 88 | space = retry_space; |
| 89 | } else { |
| 90 | return result; |
| 91 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 92 | } |
| 93 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 94 | if (OLD_POINTER_SPACE == space) { |
| 95 | result = old_pointer_space_->AllocateRaw(size_in_bytes); |
| 96 | } else if (OLD_DATA_SPACE == space) { |
| 97 | result = old_data_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | } else if (CODE_SPACE == space) { |
| 99 | result = code_space_->AllocateRaw(size_in_bytes); |
| 100 | } else if (LO_SPACE == space) { |
| 101 | result = lo_space_->AllocateRaw(size_in_bytes); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 102 | } else if (CELL_SPACE == space) { |
| 103 | result = cell_space_->AllocateRaw(size_in_bytes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | } else { |
| 105 | ASSERT(MAP_SPACE == space); |
| 106 | result = map_space_->AllocateRaw(size_in_bytes); |
| 107 | } |
| 108 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | Object* Heap::NumberFromInt32(int32_t value) { |
| 114 | if (Smi::IsValid(value)) return Smi::FromInt(value); |
| 115 | // Bypass NumberFromDouble to avoid various redundant checks. |
| 116 | return AllocateHeapNumber(FastI2D(value)); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | Object* Heap::NumberFromUint32(uint32_t value) { |
| 121 | if ((int32_t)value >= 0 && Smi::IsValid((int32_t)value)) { |
| 122 | return Smi::FromInt((int32_t)value); |
| 123 | } |
| 124 | // Bypass NumberFromDouble to avoid various redundant checks. |
| 125 | return AllocateHeapNumber(FastUI2D(value)); |
| 126 | } |
| 127 | |
| 128 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 129 | void Heap::FinalizeExternalString(String* string) { |
| 130 | ASSERT(string->IsExternalString()); |
| 131 | v8::String::ExternalStringResourceBase** resource_addr = |
| 132 | reinterpret_cast<v8::String::ExternalStringResourceBase**>( |
| 133 | reinterpret_cast<byte*>(string) + |
| 134 | ExternalString::kResourceOffset - |
| 135 | kHeapObjectTag); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 136 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 137 | // Dispose of the C++ object if it has not already been disposed. |
| 138 | if (*resource_addr != NULL) { |
| 139 | (*resource_addr)->Dispose(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 140 | } |
| 141 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 142 | // Clear the resource pointer in the string. |
| 143 | *resource_addr = NULL; |
| 144 | } |
| 145 | |
| 146 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 147 | Object* Heap::AllocateRawMap() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | #ifdef DEBUG |
| 149 | Counters::objs_since_last_full.Increment(); |
| 150 | Counters::objs_since_last_young.Increment(); |
| 151 | #endif |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 152 | Object* result = map_space_->AllocateRaw(Map::kSize); |
| 153 | if (result->IsFailure()) old_gen_exhausted_ = true; |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 154 | #ifdef DEBUG |
| 155 | if (!result->IsFailure()) { |
| 156 | // Maps have their own alignment. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 157 | CHECK((reinterpret_cast<intptr_t>(result) & kMapAlignmentMask) == |
| 158 | static_cast<intptr_t>(kHeapObjectTag)); |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 159 | } |
| 160 | #endif |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 161 | return result; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | Object* Heap::AllocateRawCell() { |
| 166 | #ifdef DEBUG |
| 167 | Counters::objs_since_last_full.Increment(); |
| 168 | Counters::objs_since_last_young.Increment(); |
| 169 | #endif |
| 170 | Object* result = cell_space_->AllocateRaw(JSGlobalPropertyCell::kSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | if (result->IsFailure()) old_gen_exhausted_ = true; |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | bool Heap::InNewSpace(Object* object) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 177 | bool result = new_space_.Contains(object); |
| 178 | ASSERT(!result || // Either not in new space |
| 179 | gc_state_ != NOT_IN_GC || // ... or in the middle of GC |
| 180 | InToSpace(object)); // ... or in to-space (where we allocate). |
| 181 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | bool Heap::InFromSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 186 | return new_space_.FromSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | bool Heap::InToSpace(Object* object) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 191 | return new_space_.ToSpaceContains(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | bool Heap::ShouldBePromoted(Address old_address, int object_size) { |
| 196 | // An object should be promoted if: |
| 197 | // - the object has survived a scavenge operation or |
| 198 | // - to space is already 25% full. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 199 | return old_address < new_space_.age_mark() |
| 200 | || (new_space_.Size() + object_size) >= (new_space_.Capacity() >> 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | void Heap::RecordWrite(Address address, int offset) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 205 | if (new_space_.Contains(address)) return; |
| 206 | ASSERT(!new_space_.FromSpaceContains(address)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | SLOW_ASSERT(Contains(address + offset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 208 | Page::FromAddress(address)->MarkRegionDirty(address + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 212 | void Heap::RecordWrites(Address address, int start, int len) { |
| 213 | if (new_space_.Contains(address)) return; |
| 214 | ASSERT(!new_space_.FromSpaceContains(address)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 215 | Page* page = Page::FromAddress(address); |
| 216 | page->SetRegionMarks(page->GetRegionMarks() | |
| 217 | page->GetRegionMaskForSpan(address + start, len * kPointerSize)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 221 | OldSpace* Heap::TargetSpace(HeapObject* object) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 222 | InstanceType type = object->map()->instance_type(); |
| 223 | AllocationSpace space = TargetSpaceId(type); |
| 224 | return (space == OLD_POINTER_SPACE) |
| 225 | ? old_pointer_space_ |
| 226 | : old_data_space_; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | AllocationSpace Heap::TargetSpaceId(InstanceType type) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 231 | // Heap numbers and sequential strings are promoted to old data space, all |
| 232 | // 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] | 233 | // object->IsHeapNumber() and object->IsSeqString() because we already |
| 234 | // know that object has the heap object tag. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 235 | |
| 236 | // These objects are never allocated in new space. |
| 237 | ASSERT(type != MAP_TYPE); |
| 238 | ASSERT(type != CODE_TYPE); |
| 239 | ASSERT(type != ODDBALL_TYPE); |
| 240 | ASSERT(type != JS_GLOBAL_PROPERTY_CELL_TYPE); |
| 241 | |
| 242 | if (type < FIRST_NONSTRING_TYPE) { |
| 243 | // There are three string representations: sequential strings, cons |
| 244 | // strings, and external strings. Only cons strings contain |
| 245 | // non-map-word pointers to heap objects. |
| 246 | return ((type & kStringRepresentationMask) == kConsStringTag) |
| 247 | ? OLD_POINTER_SPACE |
| 248 | : OLD_DATA_SPACE; |
| 249 | } else { |
| 250 | return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE; |
| 251 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 255 | void Heap::CopyBlock(Address dst, Address src, int byte_size) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 256 | ASSERT(IsAligned(byte_size, kPointerSize)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 257 | CopyWords(reinterpret_cast<Object**>(dst), |
| 258 | reinterpret_cast<Object**>(src), |
| 259 | byte_size / kPointerSize); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 260 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 262 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 263 | void Heap::CopyBlockToOldSpaceAndUpdateRegionMarks(Address dst, |
| 264 | Address src, |
| 265 | int byte_size) { |
| 266 | ASSERT(IsAligned(byte_size, kPointerSize)); |
| 267 | |
| 268 | Page* page = Page::FromAddress(dst); |
| 269 | uint32_t marks = page->GetRegionMarks(); |
| 270 | |
| 271 | for (int remaining = byte_size / kPointerSize; |
| 272 | remaining > 0; |
| 273 | remaining--) { |
| 274 | Memory::Object_at(dst) = Memory::Object_at(src); |
| 275 | |
| 276 | if (Heap::InNewSpace(Memory::Object_at(dst))) { |
| 277 | marks |= page->GetRegionMaskForAddress(dst); |
| 278 | } |
| 279 | |
| 280 | dst += kPointerSize; |
| 281 | src += kPointerSize; |
| 282 | } |
| 283 | |
| 284 | page->SetRegionMarks(marks); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | void Heap::MoveBlock(Address dst, Address src, int byte_size) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 289 | ASSERT(IsAligned(byte_size, kPointerSize)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 290 | |
| 291 | int size_in_words = byte_size / kPointerSize; |
| 292 | |
| 293 | if ((dst < src) || (dst >= (src + size_in_words))) { |
| 294 | ASSERT((dst >= (src + size_in_words)) || |
| 295 | ((OffsetFrom(reinterpret_cast<Address>(src)) - |
| 296 | OffsetFrom(reinterpret_cast<Address>(dst))) >= kPointerSize)); |
| 297 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 298 | Object** src_slot = reinterpret_cast<Object**>(src); |
| 299 | Object** dst_slot = reinterpret_cast<Object**>(dst); |
| 300 | Object** end_slot = src_slot + size_in_words; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 301 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 302 | while (src_slot != end_slot) { |
| 303 | *dst_slot++ = *src_slot++; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 304 | } |
| 305 | } else { |
| 306 | memmove(dst, src, byte_size); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 311 | void Heap::MoveBlockToOldSpaceAndUpdateRegionMarks(Address dst, |
| 312 | Address src, |
| 313 | int byte_size) { |
| 314 | ASSERT(IsAligned(byte_size, kPointerSize)); |
| 315 | ASSERT((dst >= (src + byte_size)) || |
| 316 | ((OffsetFrom(src) - OffsetFrom(dst)) >= kPointerSize)); |
| 317 | |
| 318 | CopyBlockToOldSpaceAndUpdateRegionMarks(dst, src, byte_size); |
| 319 | } |
| 320 | |
| 321 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 322 | void Heap::ScavengeObject(HeapObject** p, HeapObject* object) { |
| 323 | ASSERT(InFromSpace(object)); |
| 324 | |
| 325 | // We use the first word (where the map pointer usually is) of a heap |
| 326 | // object to record the forwarding pointer. A forwarding pointer can |
| 327 | // point to an old space, the code space, or the to space of the new |
| 328 | // generation. |
| 329 | MapWord first_word = object->map_word(); |
| 330 | |
| 331 | // If the first word is a forwarding address, the object has already been |
| 332 | // copied. |
| 333 | if (first_word.IsForwardingAddress()) { |
| 334 | *p = first_word.ToForwardingAddress(); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | // Call the slow part of scavenge object. |
| 339 | return ScavengeObjectSlow(p, object); |
| 340 | } |
| 341 | |
| 342 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 343 | Object* Heap::PrepareForCompare(String* str) { |
| 344 | // Always flatten small strings and force flattening of long strings |
| 345 | // after we have accumulated a certain amount we failed to flatten. |
| 346 | static const int kMaxAlwaysFlattenLength = 32; |
| 347 | static const int kFlattenLongThreshold = 16*KB; |
| 348 | |
| 349 | const int length = str->length(); |
| 350 | Object* obj = str->TryFlatten(); |
| 351 | if (length <= kMaxAlwaysFlattenLength || |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 352 | unflattened_strings_length_ >= kFlattenLongThreshold) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 353 | return obj; |
| 354 | } |
| 355 | if (obj->IsFailure()) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 356 | unflattened_strings_length_ += length; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 357 | } |
| 358 | return str; |
| 359 | } |
| 360 | |
| 361 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 362 | int Heap::AdjustAmountOfExternalAllocatedMemory(int change_in_bytes) { |
| 363 | ASSERT(HasBeenSetup()); |
| 364 | int amount = amount_of_external_allocated_memory_ + change_in_bytes; |
| 365 | if (change_in_bytes >= 0) { |
| 366 | // Avoid overflow. |
| 367 | if (amount > amount_of_external_allocated_memory_) { |
| 368 | amount_of_external_allocated_memory_ = amount; |
| 369 | } |
| 370 | int amount_since_last_global_gc = |
| 371 | amount_of_external_allocated_memory_ - |
| 372 | amount_of_external_allocated_memory_at_last_global_gc_; |
| 373 | if (amount_since_last_global_gc > external_allocation_limit_) { |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 374 | CollectAllGarbage(false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 375 | } |
| 376 | } else { |
| 377 | // Avoid underflow. |
| 378 | if (amount >= 0) { |
| 379 | amount_of_external_allocated_memory_ = amount; |
| 380 | } |
| 381 | } |
| 382 | ASSERT(amount_of_external_allocated_memory_ >= 0); |
| 383 | return amount_of_external_allocated_memory_; |
| 384 | } |
| 385 | |
| 386 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 387 | void Heap::SetLastScriptId(Object* last_script_id) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 388 | roots_[kLastScriptIdRootIndex] = last_script_id; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 392 | #define GC_GREEDY_CHECK() \ |
| 393 | ASSERT(!FLAG_gc_greedy || v8::internal::Heap::GarbageCollectionGreedyCheck()) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 395 | |
| 396 | // Calls the FUNCTION_CALL function and retries it up to three times |
| 397 | // to guarantee that any allocations performed during the call will |
| 398 | // succeed if there's enough memory. |
| 399 | |
| 400 | // Warning: Do not use the identifiers __object__ or __scope__ in a |
| 401 | // call to this macro. |
| 402 | |
| 403 | #define CALL_AND_RETRY(FUNCTION_CALL, RETURN_VALUE, RETURN_EMPTY) \ |
| 404 | do { \ |
| 405 | GC_GREEDY_CHECK(); \ |
| 406 | Object* __object__ = FUNCTION_CALL; \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 407 | if (!__object__->IsFailure()) RETURN_VALUE; \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 408 | if (__object__->IsOutOfMemoryFailure()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 409 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_0", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 410 | } \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 411 | if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
kasperl@chromium.org | 5887095 | 2008-10-30 14:34:19 +0000 | [diff] [blame] | 412 | Heap::CollectGarbage(Failure::cast(__object__)->requested(), \ |
| 413 | Failure::cast(__object__)->allocation_space()); \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 414 | __object__ = FUNCTION_CALL; \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 415 | if (!__object__->IsFailure()) RETURN_VALUE; \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 416 | if (__object__->IsOutOfMemoryFailure()) { \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 417 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_1", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 418 | } \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 419 | if (!__object__->IsRetryAfterGC()) RETURN_EMPTY; \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 420 | Counters::gc_last_resort_from_handles.Increment(); \ |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 421 | Heap::CollectAllAvailableGarbage(); \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 422 | { \ |
| 423 | AlwaysAllocateScope __scope__; \ |
| 424 | __object__ = FUNCTION_CALL; \ |
| 425 | } \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 426 | if (!__object__->IsFailure()) RETURN_VALUE; \ |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 427 | if (__object__->IsOutOfMemoryFailure() || \ |
| 428 | __object__->IsRetryAfterGC()) { \ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 429 | /* TODO(1181417): Fix this. */ \ |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 430 | v8::internal::V8::FatalProcessOutOfMemory("CALL_AND_RETRY_2", true);\ |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 431 | } \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 432 | RETURN_EMPTY; \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 433 | } while (false) |
| 434 | |
| 435 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 436 | #define CALL_HEAP_FUNCTION(FUNCTION_CALL, TYPE) \ |
| 437 | CALL_AND_RETRY(FUNCTION_CALL, \ |
| 438 | return Handle<TYPE>(TYPE::cast(__object__)), \ |
| 439 | return Handle<TYPE>()) |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 440 | |
| 441 | |
| 442 | #define CALL_HEAP_FUNCTION_VOID(FUNCTION_CALL) \ |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 443 | CALL_AND_RETRY(FUNCTION_CALL, return, return) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 444 | |
| 445 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 446 | #ifdef DEBUG |
| 447 | |
| 448 | inline bool Heap::allow_allocation(bool new_state) { |
| 449 | bool old = allocation_allowed_; |
| 450 | allocation_allowed_ = new_state; |
| 451 | return old; |
| 452 | } |
| 453 | |
| 454 | #endif |
| 455 | |
| 456 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 457 | void ExternalStringTable::AddString(String* string) { |
| 458 | ASSERT(string->IsExternalString()); |
| 459 | if (Heap::InNewSpace(string)) { |
| 460 | new_space_strings_.Add(string); |
| 461 | } else { |
| 462 | old_space_strings_.Add(string); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | |
| 467 | void ExternalStringTable::Iterate(ObjectVisitor* v) { |
| 468 | if (!new_space_strings_.is_empty()) { |
| 469 | Object** start = &new_space_strings_[0]; |
| 470 | v->VisitPointers(start, start + new_space_strings_.length()); |
| 471 | } |
| 472 | if (!old_space_strings_.is_empty()) { |
| 473 | Object** start = &old_space_strings_[0]; |
| 474 | v->VisitPointers(start, start + old_space_strings_.length()); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | |
| 479 | // Verify() is inline to avoid ifdef-s around its calls in release |
| 480 | // mode. |
| 481 | void ExternalStringTable::Verify() { |
| 482 | #ifdef DEBUG |
| 483 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
| 484 | ASSERT(Heap::InNewSpace(new_space_strings_[i])); |
| 485 | ASSERT(new_space_strings_[i] != Heap::raw_unchecked_null_value()); |
| 486 | } |
| 487 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
| 488 | ASSERT(!Heap::InNewSpace(old_space_strings_[i])); |
| 489 | ASSERT(old_space_strings_[i] != Heap::raw_unchecked_null_value()); |
| 490 | } |
| 491 | #endif |
| 492 | } |
| 493 | |
| 494 | |
| 495 | void ExternalStringTable::AddOldString(String* string) { |
| 496 | ASSERT(string->IsExternalString()); |
| 497 | ASSERT(!Heap::InNewSpace(string)); |
| 498 | old_space_strings_.Add(string); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | void ExternalStringTable::ShrinkNewStrings(int position) { |
| 503 | new_space_strings_.Rewind(position); |
| 504 | Verify(); |
| 505 | } |
| 506 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 507 | } } // namespace v8::internal |
| 508 | |
| 509 | #endif // V8_HEAP_INL_H_ |