OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_HEAP_HEAP_INL_H_ | 5 #ifndef V8_HEAP_HEAP_INL_H_ |
6 #define V8_HEAP_HEAP_INL_H_ | 6 #define V8_HEAP_HEAP_INL_H_ |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 return AllocateInternalizedStringImpl<false>(t, chars, hash_field); | 74 return AllocateInternalizedStringImpl<false>(t, chars, hash_field); |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 AllocationResult Heap::AllocateOneByteInternalizedString( | 78 AllocationResult Heap::AllocateOneByteInternalizedString( |
79 Vector<const uint8_t> str, uint32_t hash_field) { | 79 Vector<const uint8_t> str, uint32_t hash_field) { |
80 CHECK_GE(String::kMaxLength, str.length()); | 80 CHECK_GE(String::kMaxLength, str.length()); |
81 // Compute map and object size. | 81 // Compute map and object size. |
82 Map* map = one_byte_internalized_string_map(); | 82 Map* map = one_byte_internalized_string_map(); |
83 int size = SeqOneByteString::SizeFor(str.length()); | 83 int size = SeqOneByteString::SizeFor(str.length()); |
84 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, TENURED); | 84 AllocationSpace space = SelectSpace(size, TENURED); |
85 | 85 |
86 // Allocate string. | 86 // Allocate string. |
87 HeapObject* result; | 87 HeapObject* result; |
88 { | 88 { |
89 AllocationResult allocation = AllocateRaw(size, space, OLD_DATA_SPACE); | 89 AllocationResult allocation = AllocateRaw(size, space, OLD_SPACE); |
90 if (!allocation.To(&result)) return allocation; | 90 if (!allocation.To(&result)) return allocation; |
91 } | 91 } |
92 | 92 |
93 // String maps are all immortal immovable objects. | 93 // String maps are all immortal immovable objects. |
94 result->set_map_no_write_barrier(map); | 94 result->set_map_no_write_barrier(map); |
95 // Set length and hash fields of the allocated string. | 95 // Set length and hash fields of the allocated string. |
96 String* answer = String::cast(result); | 96 String* answer = String::cast(result); |
97 answer->set_length(str.length()); | 97 answer->set_length(str.length()); |
98 answer->set_hash_field(hash_field); | 98 answer->set_hash_field(hash_field); |
99 | 99 |
100 DCHECK_EQ(size, answer->Size()); | 100 DCHECK_EQ(size, answer->Size()); |
101 | 101 |
102 // Fill in the characters. | 102 // Fill in the characters. |
103 MemCopy(answer->address() + SeqOneByteString::kHeaderSize, str.start(), | 103 MemCopy(answer->address() + SeqOneByteString::kHeaderSize, str.start(), |
104 str.length()); | 104 str.length()); |
105 | 105 |
106 return answer; | 106 return answer; |
107 } | 107 } |
108 | 108 |
109 | 109 |
110 AllocationResult Heap::AllocateTwoByteInternalizedString(Vector<const uc16> str, | 110 AllocationResult Heap::AllocateTwoByteInternalizedString(Vector<const uc16> str, |
111 uint32_t hash_field) { | 111 uint32_t hash_field) { |
112 CHECK_GE(String::kMaxLength, str.length()); | 112 CHECK_GE(String::kMaxLength, str.length()); |
113 // Compute map and object size. | 113 // Compute map and object size. |
114 Map* map = internalized_string_map(); | 114 Map* map = internalized_string_map(); |
115 int size = SeqTwoByteString::SizeFor(str.length()); | 115 int size = SeqTwoByteString::SizeFor(str.length()); |
116 AllocationSpace space = SelectSpace(size, OLD_DATA_SPACE, TENURED); | 116 AllocationSpace space = SelectSpace(size, TENURED); |
117 | 117 |
118 // Allocate string. | 118 // Allocate string. |
119 HeapObject* result; | 119 HeapObject* result; |
120 { | 120 { |
121 AllocationResult allocation = AllocateRaw(size, space, OLD_DATA_SPACE); | 121 AllocationResult allocation = AllocateRaw(size, space, OLD_SPACE); |
122 if (!allocation.To(&result)) return allocation; | 122 if (!allocation.To(&result)) return allocation; |
123 } | 123 } |
124 | 124 |
125 result->set_map(map); | 125 result->set_map(map); |
126 // Set length and hash fields of the allocated string. | 126 // Set length and hash fields of the allocated string. |
127 String* answer = String::cast(result); | 127 String* answer = String::cast(result); |
128 answer->set_length(str.length()); | 128 answer->set_length(str.length()); |
129 answer->set_hash_field(hash_field); | 129 answer->set_hash_field(hash_field); |
130 | 130 |
131 DCHECK_EQ(size, answer->Size()); | 131 DCHECK_EQ(size, answer->Size()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 if (always_allocate() && allocation.IsRetry() && retry_space != NEW_SPACE) { | 176 if (always_allocate() && allocation.IsRetry() && retry_space != NEW_SPACE) { |
177 space = retry_space; | 177 space = retry_space; |
178 } else { | 178 } else { |
179 if (allocation.To(&object)) { | 179 if (allocation.To(&object)) { |
180 OnAllocationEvent(object, size_in_bytes); | 180 OnAllocationEvent(object, size_in_bytes); |
181 } | 181 } |
182 return allocation; | 182 return allocation; |
183 } | 183 } |
184 } | 184 } |
185 | 185 |
186 if (OLD_POINTER_SPACE == space) { | 186 if (OLD_SPACE == space) { |
187 allocation = old_pointer_space_->AllocateRaw(size_in_bytes); | 187 allocation = old_space_->AllocateRaw(size_in_bytes); |
188 } else if (OLD_DATA_SPACE == space) { | |
189 allocation = old_data_space_->AllocateRaw(size_in_bytes); | |
190 } else if (CODE_SPACE == space) { | 188 } else if (CODE_SPACE == space) { |
191 if (size_in_bytes <= code_space()->AreaSize()) { | 189 if (size_in_bytes <= code_space()->AreaSize()) { |
192 allocation = code_space_->AllocateRaw(size_in_bytes); | 190 allocation = code_space_->AllocateRaw(size_in_bytes); |
193 } else { | 191 } else { |
194 // Large code objects are allocated in large object space. | 192 // Large code objects are allocated in large object space. |
195 allocation = lo_space_->AllocateRaw(size_in_bytes, EXECUTABLE); | 193 allocation = lo_space_->AllocateRaw(size_in_bytes, EXECUTABLE); |
196 } | 194 } |
197 } else if (LO_SPACE == space) { | 195 } else if (LO_SPACE == space) { |
198 allocation = lo_space_->AllocateRaw(size_in_bytes, NOT_EXECUTABLE); | 196 allocation = lo_space_->AllocateRaw(size_in_bytes, NOT_EXECUTABLE); |
199 } else if (CELL_SPACE == space) { | 197 } else if (CELL_SPACE == space) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 bool Heap::InFromSpace(Object* object) { | 320 bool Heap::InFromSpace(Object* object) { |
323 return new_space_.FromSpaceContains(object); | 321 return new_space_.FromSpaceContains(object); |
324 } | 322 } |
325 | 323 |
326 | 324 |
327 bool Heap::InToSpace(Object* object) { | 325 bool Heap::InToSpace(Object* object) { |
328 return new_space_.ToSpaceContains(object); | 326 return new_space_.ToSpaceContains(object); |
329 } | 327 } |
330 | 328 |
331 | 329 |
332 bool Heap::InOldPointerSpace(Address address) { | 330 bool Heap::InOldSpace(Address address) { return old_space_->Contains(address); } |
333 return old_pointer_space_->Contains(address); | 331 |
| 332 |
| 333 bool Heap::InOldSpace(Object* object) { |
| 334 return InOldSpace(reinterpret_cast<Address>(object)); |
334 } | 335 } |
335 | 336 |
336 | 337 |
337 bool Heap::InOldPointerSpace(Object* object) { | |
338 return InOldPointerSpace(reinterpret_cast<Address>(object)); | |
339 } | |
340 | |
341 | |
342 bool Heap::InOldDataSpace(Address address) { | |
343 return old_data_space_->Contains(address); | |
344 } | |
345 | |
346 | |
347 bool Heap::InOldDataSpace(Object* object) { | |
348 return InOldDataSpace(reinterpret_cast<Address>(object)); | |
349 } | |
350 | |
351 | |
352 bool Heap::OldGenerationAllocationLimitReached() { | 338 bool Heap::OldGenerationAllocationLimitReached() { |
353 if (!incremental_marking()->IsStopped()) return false; | 339 if (!incremental_marking()->IsStopped()) return false; |
354 return OldGenerationSpaceAvailable() < 0; | 340 return OldGenerationSpaceAvailable() < 0; |
355 } | 341 } |
356 | 342 |
357 | 343 |
358 bool Heap::ShouldBePromoted(Address old_address, int object_size) { | 344 bool Heap::ShouldBePromoted(Address old_address, int object_size) { |
359 NewSpacePage* page = NewSpacePage::FromAddress(old_address); | 345 NewSpacePage* page = NewSpacePage::FromAddress(old_address); |
360 Address age_mark = new_space_.age_mark(); | 346 Address age_mark = new_space_.age_mark(); |
361 return page->IsFlagSet(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK) && | 347 return page->IsFlagSet(MemoryChunk::NEW_SPACE_BELOW_AGE_MARK) && |
362 (!page->ContainsLimit(age_mark) || old_address < age_mark); | 348 (!page->ContainsLimit(age_mark) || old_address < age_mark); |
363 } | 349 } |
364 | 350 |
365 | 351 |
366 void Heap::RecordWrite(Address address, int offset) { | 352 void Heap::RecordWrite(Address address, int offset) { |
367 if (!InNewSpace(address)) store_buffer_.Mark(address + offset); | 353 if (!InNewSpace(address)) store_buffer_.Mark(address + offset); |
368 } | 354 } |
369 | 355 |
370 | 356 |
371 void Heap::RecordWrites(Address address, int start, int len) { | 357 void Heap::RecordWrites(Address address, int start, int len) { |
372 if (!InNewSpace(address)) { | 358 if (!InNewSpace(address)) { |
373 for (int i = 0; i < len; i++) { | 359 for (int i = 0; i < len; i++) { |
374 store_buffer_.Mark(address + start + i * kPointerSize); | 360 store_buffer_.Mark(address + start + i * kPointerSize); |
375 } | 361 } |
376 } | 362 } |
377 } | 363 } |
378 | 364 |
379 | 365 |
380 OldSpace* Heap::TargetSpace(HeapObject* object) { | |
381 InstanceType type = object->map()->instance_type(); | |
382 AllocationSpace space = TargetSpaceId(type); | |
383 return (space == OLD_POINTER_SPACE) ? old_pointer_space_ : old_data_space_; | |
384 } | |
385 | |
386 | |
387 AllocationSpace Heap::TargetSpaceId(InstanceType type) { | |
388 // Heap numbers and sequential strings are promoted to old data space, all | |
389 // other object types are promoted to old pointer space. We do not use | |
390 // object->IsHeapNumber() and object->IsSeqString() because we already | |
391 // know that object has the heap object tag. | |
392 | |
393 // These objects are never allocated in new space. | |
394 DCHECK(type != MAP_TYPE); | |
395 DCHECK(type != CODE_TYPE); | |
396 DCHECK(type != ODDBALL_TYPE); | |
397 DCHECK(type != CELL_TYPE); | |
398 DCHECK(type != PROPERTY_CELL_TYPE); | |
399 | |
400 if (type <= LAST_NAME_TYPE) { | |
401 if (type == SYMBOL_TYPE) return OLD_POINTER_SPACE; | |
402 DCHECK(type < FIRST_NONSTRING_TYPE); | |
403 // There are four string representations: sequential strings, external | |
404 // strings, cons strings, and sliced strings. | |
405 // Only the latter two contain non-map-word pointers to heap objects. | |
406 return ((type & kIsIndirectStringMask) == kIsIndirectStringTag) | |
407 ? OLD_POINTER_SPACE | |
408 : OLD_DATA_SPACE; | |
409 } else { | |
410 return (type <= LAST_DATA_TYPE) ? OLD_DATA_SPACE : OLD_POINTER_SPACE; | |
411 } | |
412 } | |
413 | |
414 | |
415 bool Heap::AllowedToBeMigrated(HeapObject* obj, AllocationSpace dst) { | 366 bool Heap::AllowedToBeMigrated(HeapObject* obj, AllocationSpace dst) { |
416 // Object migration is governed by the following rules: | 367 // Object migration is governed by the following rules: |
417 // | 368 // |
418 // 1) Objects in new-space can be migrated to one of the old spaces | 369 // 1) Objects in new-space can be migrated to the old space |
419 // that matches their target space or they stay in new-space. | 370 // that matches their target space or they stay in new-space. |
420 // 2) Objects in old-space stay in the same space when migrating. | 371 // 2) Objects in old-space stay in the same space when migrating. |
421 // 3) Fillers (two or more words) can migrate due to left-trimming of | 372 // 3) Fillers (two or more words) can migrate due to left-trimming of |
422 // fixed arrays in new-space, old-data-space and old-pointer-space. | 373 // fixed arrays in new-space or old space. |
423 // 4) Fillers (one word) can never migrate, they are skipped by | 374 // 4) Fillers (one word) can never migrate, they are skipped by |
424 // incremental marking explicitly to prevent invalid pattern. | 375 // incremental marking explicitly to prevent invalid pattern. |
425 // 5) Short external strings can end up in old pointer space when a cons | |
426 // string in old pointer space is made external (String::MakeExternal). | |
427 // | 376 // |
428 // Since this function is used for debugging only, we do not place | 377 // Since this function is used for debugging only, we do not place |
429 // asserts here, but check everything explicitly. | 378 // asserts here, but check everything explicitly. |
430 if (obj->map() == one_pointer_filler_map()) return false; | 379 if (obj->map() == one_pointer_filler_map()) return false; |
431 InstanceType type = obj->map()->instance_type(); | 380 InstanceType type = obj->map()->instance_type(); |
432 MemoryChunk* chunk = MemoryChunk::FromAddress(obj->address()); | 381 MemoryChunk* chunk = MemoryChunk::FromAddress(obj->address()); |
433 AllocationSpace src = chunk->owner()->identity(); | 382 AllocationSpace src = chunk->owner()->identity(); |
434 switch (src) { | 383 switch (src) { |
435 case NEW_SPACE: | 384 case NEW_SPACE: |
436 return dst == src || dst == TargetSpaceId(type); | 385 return dst == src || dst == OLD_SPACE; |
437 case OLD_POINTER_SPACE: | 386 case OLD_SPACE: |
438 return dst == src && (dst == TargetSpaceId(type) || obj->IsFiller() || | 387 return dst == src && |
439 obj->IsExternalString()); | 388 (dst == OLD_SPACE || obj->IsFiller() || obj->IsExternalString()); |
440 case OLD_DATA_SPACE: | |
441 return dst == src && dst == TargetSpaceId(type); | |
442 case CODE_SPACE: | 389 case CODE_SPACE: |
443 return dst == src && type == CODE_TYPE; | 390 return dst == src && type == CODE_TYPE; |
444 case MAP_SPACE: | 391 case MAP_SPACE: |
445 case CELL_SPACE: | 392 case CELL_SPACE: |
446 case PROPERTY_CELL_SPACE: | 393 case PROPERTY_CELL_SPACE: |
447 case LO_SPACE: | 394 case LO_SPACE: |
448 return false; | 395 return false; |
449 } | 396 } |
450 UNREACHABLE(); | 397 UNREACHABLE(); |
451 return false; | 398 return false; |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 | 690 |
744 void VerifySmisVisitor::VisitPointers(Object** start, Object** end) { | 691 void VerifySmisVisitor::VisitPointers(Object** start, Object** end) { |
745 for (Object** current = start; current < end; current++) { | 692 for (Object** current = start; current < end; current++) { |
746 CHECK((*current)->IsSmi()); | 693 CHECK((*current)->IsSmi()); |
747 } | 694 } |
748 } | 695 } |
749 } | 696 } |
750 } // namespace v8::internal | 697 } // namespace v8::internal |
751 | 698 |
752 #endif // V8_HEAP_HEAP_INL_H_ | 699 #endif // V8_HEAP_HEAP_INL_H_ |
OLD | NEW |