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