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