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