| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 100 |
| 101 DeleteArray(mem); | 101 DeleteArray(mem); |
| 102 } | 102 } |
| 103 #endif | 103 #endif |
| 104 | 104 |
| 105 | 105 |
| 106 // Temporarily sets a given allocator in an isolate. | 106 // Temporarily sets a given allocator in an isolate. |
| 107 class TestMemoryAllocatorScope { | 107 class TestMemoryAllocatorScope { |
| 108 public: | 108 public: |
| 109 TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator) | 109 TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator) |
| 110 : isolate_(isolate), | 110 : isolate_(isolate), old_allocator_(isolate->heap()->memory_allocator()) { |
| 111 old_allocator_(isolate->memory_allocator_) { | 111 isolate->heap()->memory_allocator_ = allocator; |
| 112 isolate->memory_allocator_ = allocator; | |
| 113 } | 112 } |
| 114 | 113 |
| 115 ~TestMemoryAllocatorScope() { | 114 ~TestMemoryAllocatorScope() { |
| 116 isolate_->memory_allocator_ = old_allocator_; | 115 isolate_->heap()->memory_allocator_ = old_allocator_; |
| 117 } | 116 } |
| 118 | 117 |
| 119 private: | 118 private: |
| 120 Isolate* isolate_; | 119 Isolate* isolate_; |
| 121 MemoryAllocator* old_allocator_; | 120 MemoryAllocator* old_allocator_; |
| 122 | 121 |
| 123 DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope); | 122 DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope); |
| 124 }; | 123 }; |
| 125 | 124 |
| 126 | 125 |
| 127 // Temporarily sets a given code range in an isolate. | 126 // Temporarily sets a given code range in an isolate. |
| 128 class TestCodeRangeScope { | 127 class TestCodeRangeScope { |
| 129 public: | 128 public: |
| 130 TestCodeRangeScope(Isolate* isolate, CodeRange* code_range) | 129 TestCodeRangeScope(Isolate* isolate, CodeRange* code_range) |
| 131 : isolate_(isolate), | 130 : isolate_(isolate), |
| 132 old_code_range_(isolate->code_range_) { | 131 old_code_range_(isolate->heap()->memory_allocator()->code_range()) { |
| 133 isolate->code_range_ = code_range; | 132 isolate->heap()->memory_allocator()->code_range_ = code_range; |
| 134 } | 133 } |
| 135 | 134 |
| 136 ~TestCodeRangeScope() { | 135 ~TestCodeRangeScope() { |
| 137 isolate_->code_range_ = old_code_range_; | 136 isolate_->heap()->memory_allocator()->code_range_ = old_code_range_; |
| 138 } | 137 } |
| 139 | 138 |
| 140 private: | 139 private: |
| 141 Isolate* isolate_; | 140 Isolate* isolate_; |
| 142 CodeRange* old_code_range_; | 141 CodeRange* old_code_range_; |
| 143 | 142 |
| 144 DISALLOW_COPY_AND_ASSIGN(TestCodeRangeScope); | 143 DISALLOW_COPY_AND_ASSIGN(TestCodeRangeScope); |
| 145 }; | 144 }; |
| 146 | 145 |
| 147 | 146 |
| 148 static void VerifyMemoryChunk(Isolate* isolate, | 147 static void VerifyMemoryChunk(Isolate* isolate, |
| 149 Heap* heap, | 148 Heap* heap, |
| 150 CodeRange* code_range, | 149 CodeRange* code_range, |
| 151 size_t reserve_area_size, | 150 size_t reserve_area_size, |
| 152 size_t commit_area_size, | 151 size_t commit_area_size, |
| 153 size_t second_commit_area_size, | 152 size_t second_commit_area_size, |
| 154 Executability executable) { | 153 Executability executable) { |
| 155 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); | 154 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 156 CHECK(memory_allocator->SetUp(heap->MaxReserved(), | 155 CHECK(memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize(), |
| 157 heap->MaxExecutableSize())); | 156 0)); |
| 158 TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator); | 157 { |
| 159 TestCodeRangeScope test_code_range_scope(isolate, code_range); | 158 TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator); |
| 159 TestCodeRangeScope test_code_range_scope(isolate, code_range); |
| 160 | 160 |
| 161 size_t header_size = (executable == EXECUTABLE) | 161 size_t header_size = (executable == EXECUTABLE) |
| 162 ? MemoryAllocator::CodePageGuardStartOffset() | 162 ? MemoryAllocator::CodePageGuardStartOffset() |
| 163 : MemoryChunk::kObjectStartOffset; | 163 : MemoryChunk::kObjectStartOffset; |
| 164 size_t guard_size = (executable == EXECUTABLE) | 164 size_t guard_size = |
| 165 ? MemoryAllocator::CodePageGuardSize() | 165 (executable == EXECUTABLE) ? MemoryAllocator::CodePageGuardSize() : 0; |
| 166 : 0; | |
| 167 | 166 |
| 168 MemoryChunk* memory_chunk = memory_allocator->AllocateChunk(reserve_area_size, | 167 MemoryChunk* memory_chunk = memory_allocator->AllocateChunk( |
| 169 commit_area_size, | 168 reserve_area_size, commit_area_size, executable, NULL); |
| 170 executable, | 169 size_t alignment = code_range != NULL && code_range->valid() |
| 171 NULL); | 170 ? MemoryChunk::kAlignment |
| 172 size_t alignment = code_range != NULL && code_range->valid() | 171 : base::OS::CommitPageSize(); |
| 173 ? MemoryChunk::kAlignment | 172 size_t reserved_size = |
| 174 : base::OS::CommitPageSize(); | 173 ((executable == EXECUTABLE)) |
| 175 size_t reserved_size = | 174 ? RoundUp(header_size + guard_size + reserve_area_size + guard_size, |
| 176 ((executable == EXECUTABLE)) | 175 alignment) |
| 177 ? RoundUp(header_size + guard_size + reserve_area_size + guard_size, | 176 : RoundUp(header_size + reserve_area_size, |
| 178 alignment) | 177 base::OS::CommitPageSize()); |
| 179 : RoundUp(header_size + reserve_area_size, | 178 CHECK(memory_chunk->size() == reserved_size); |
| 180 base::OS::CommitPageSize()); | 179 CHECK(memory_chunk->area_start() < |
| 181 CHECK(memory_chunk->size() == reserved_size); | 180 memory_chunk->address() + memory_chunk->size()); |
| 182 CHECK(memory_chunk->area_start() < memory_chunk->address() + | 181 CHECK(memory_chunk->area_end() <= |
| 183 memory_chunk->size()); | 182 memory_chunk->address() + memory_chunk->size()); |
| 184 CHECK(memory_chunk->area_end() <= memory_chunk->address() + | 183 CHECK(static_cast<size_t>(memory_chunk->area_size()) == commit_area_size); |
| 185 memory_chunk->size()); | |
| 186 CHECK(static_cast<size_t>(memory_chunk->area_size()) == commit_area_size); | |
| 187 | 184 |
| 188 Address area_start = memory_chunk->area_start(); | 185 Address area_start = memory_chunk->area_start(); |
| 189 | 186 |
| 190 memory_chunk->CommitArea(second_commit_area_size); | 187 memory_chunk->CommitArea(second_commit_area_size); |
| 191 CHECK(area_start == memory_chunk->area_start()); | 188 CHECK(area_start == memory_chunk->area_start()); |
| 192 CHECK(memory_chunk->area_start() < memory_chunk->address() + | 189 CHECK(memory_chunk->area_start() < |
| 193 memory_chunk->size()); | 190 memory_chunk->address() + memory_chunk->size()); |
| 194 CHECK(memory_chunk->area_end() <= memory_chunk->address() + | 191 CHECK(memory_chunk->area_end() <= |
| 195 memory_chunk->size()); | 192 memory_chunk->address() + memory_chunk->size()); |
| 196 CHECK(static_cast<size_t>(memory_chunk->area_size()) == | 193 CHECK(static_cast<size_t>(memory_chunk->area_size()) == |
| 197 second_commit_area_size); | 194 second_commit_area_size); |
| 198 | 195 |
| 199 memory_allocator->Free(memory_chunk); | 196 memory_allocator->Free(memory_chunk); |
| 197 } |
| 200 memory_allocator->TearDown(); | 198 memory_allocator->TearDown(); |
| 201 delete memory_allocator; | 199 delete memory_allocator; |
| 202 } | 200 } |
| 203 | 201 |
| 204 | 202 |
| 205 TEST(Regress3540) { | 203 TEST(Regress3540) { |
| 206 Isolate* isolate = CcTest::i_isolate(); | 204 Isolate* isolate = CcTest::i_isolate(); |
| 207 Heap* heap = isolate->heap(); | 205 Heap* heap = isolate->heap(); |
| 208 const int pageSize = Page::kPageSize; | 206 const int pageSize = Page::kPageSize; |
| 209 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); | 207 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 210 CHECK( | 208 CHECK(memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize(), |
| 211 memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize())); | 209 0)); |
| 212 TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator); | 210 TestMemoryAllocatorScope test_allocator_scope(isolate, memory_allocator); |
| 213 CodeRange* code_range = new CodeRange(isolate); | 211 CodeRange* code_range = new CodeRange(isolate); |
| 214 const size_t code_range_size = 4 * pageSize; | 212 const size_t code_range_size = 4 * pageSize; |
| 215 if (!code_range->SetUp( | 213 if (!code_range->SetUp( |
| 216 code_range_size + | 214 code_range_size + |
| 217 RoundUp(v8::base::OS::CommitPageSize() * kReservedCodeRangePages, | 215 RoundUp(v8::base::OS::CommitPageSize() * kReservedCodeRangePages, |
| 218 MemoryChunk::kAlignment) + | 216 MemoryChunk::kAlignment) + |
| 219 v8::internal::MemoryAllocator::CodePageAreaSize())) { | 217 v8::internal::MemoryAllocator::CodePageAreaSize())) { |
| 220 return; | 218 return; |
| 221 } | 219 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 301 } |
| 304 } | 302 } |
| 305 | 303 |
| 306 | 304 |
| 307 TEST(MemoryAllocator) { | 305 TEST(MemoryAllocator) { |
| 308 Isolate* isolate = CcTest::i_isolate(); | 306 Isolate* isolate = CcTest::i_isolate(); |
| 309 Heap* heap = isolate->heap(); | 307 Heap* heap = isolate->heap(); |
| 310 | 308 |
| 311 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); | 309 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 312 CHECK(memory_allocator != nullptr); | 310 CHECK(memory_allocator != nullptr); |
| 313 CHECK(memory_allocator->SetUp(heap->MaxReserved(), | 311 CHECK(memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize(), |
| 314 heap->MaxExecutableSize())); | 312 0)); |
| 315 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); | 313 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); |
| 316 | 314 |
| 317 { | 315 { |
| 318 int total_pages = 0; | 316 int total_pages = 0; |
| 319 OldSpace faked_space(heap, OLD_SPACE, NOT_EXECUTABLE); | 317 OldSpace faked_space(heap, OLD_SPACE, NOT_EXECUTABLE); |
| 320 Page* first_page = memory_allocator->AllocatePage<Page>( | 318 Page* first_page = memory_allocator->AllocatePage<Page>( |
| 321 faked_space.AreaSize(), static_cast<PagedSpace*>(&faked_space), | 319 faked_space.AreaSize(), static_cast<PagedSpace*>(&faked_space), |
| 322 NOT_EXECUTABLE); | 320 NOT_EXECUTABLE); |
| 323 | 321 |
| 324 first_page->InsertAfter(faked_space.anchor()->prev_page()); | 322 first_page->InsertAfter(faked_space.anchor()->prev_page()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 351 } | 349 } |
| 352 memory_allocator->TearDown(); | 350 memory_allocator->TearDown(); |
| 353 delete memory_allocator; | 351 delete memory_allocator; |
| 354 } | 352 } |
| 355 | 353 |
| 356 | 354 |
| 357 TEST(NewSpace) { | 355 TEST(NewSpace) { |
| 358 Isolate* isolate = CcTest::i_isolate(); | 356 Isolate* isolate = CcTest::i_isolate(); |
| 359 Heap* heap = isolate->heap(); | 357 Heap* heap = isolate->heap(); |
| 360 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); | 358 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 361 CHECK(memory_allocator->SetUp(heap->MaxReserved(), | 359 CHECK(memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize(), |
| 362 heap->MaxExecutableSize())); | 360 0)); |
| 363 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); | 361 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); |
| 364 | 362 |
| 365 NewSpace new_space(heap); | 363 NewSpace new_space(heap); |
| 366 | 364 |
| 367 CHECK(new_space.SetUp(CcTest::heap()->InitialSemiSpaceSize(), | 365 CHECK(new_space.SetUp(CcTest::heap()->InitialSemiSpaceSize(), |
| 368 CcTest::heap()->InitialSemiSpaceSize())); | 366 CcTest::heap()->InitialSemiSpaceSize())); |
| 369 CHECK(new_space.HasBeenSetUp()); | 367 CHECK(new_space.HasBeenSetUp()); |
| 370 | 368 |
| 371 while (new_space.Available() >= Page::kMaxRegularHeapObjectSize) { | 369 while (new_space.Available() >= Page::kMaxRegularHeapObjectSize) { |
| 372 Object* obj = | 370 Object* obj = |
| 373 new_space.AllocateRawUnaligned(Page::kMaxRegularHeapObjectSize) | 371 new_space.AllocateRawUnaligned(Page::kMaxRegularHeapObjectSize) |
| 374 .ToObjectChecked(); | 372 .ToObjectChecked(); |
| 375 CHECK(new_space.Contains(HeapObject::cast(obj))); | 373 CHECK(new_space.Contains(HeapObject::cast(obj))); |
| 376 } | 374 } |
| 377 | 375 |
| 378 new_space.TearDown(); | 376 new_space.TearDown(); |
| 379 memory_allocator->TearDown(); | 377 memory_allocator->TearDown(); |
| 380 delete memory_allocator; | 378 delete memory_allocator; |
| 381 } | 379 } |
| 382 | 380 |
| 383 | 381 |
| 384 TEST(OldSpace) { | 382 TEST(OldSpace) { |
| 385 Isolate* isolate = CcTest::i_isolate(); | 383 Isolate* isolate = CcTest::i_isolate(); |
| 386 Heap* heap = isolate->heap(); | 384 Heap* heap = isolate->heap(); |
| 387 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); | 385 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 388 CHECK(memory_allocator->SetUp(heap->MaxReserved(), | 386 CHECK(memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize(), |
| 389 heap->MaxExecutableSize())); | 387 0)); |
| 390 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); | 388 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); |
| 391 | 389 |
| 392 OldSpace* s = new OldSpace(heap, OLD_SPACE, NOT_EXECUTABLE); | 390 OldSpace* s = new OldSpace(heap, OLD_SPACE, NOT_EXECUTABLE); |
| 393 CHECK(s != NULL); | 391 CHECK(s != NULL); |
| 394 | 392 |
| 395 CHECK(s->SetUp()); | 393 CHECK(s->SetUp()); |
| 396 | 394 |
| 397 while (s->Available() > 0) { | 395 while (s->Available() > 0) { |
| 398 s->AllocateRawUnaligned(Page::kMaxRegularHeapObjectSize).ToObjectChecked(); | 396 s->AllocateRawUnaligned(Page::kMaxRegularHeapObjectSize).ToObjectChecked(); |
| 399 } | 397 } |
| 400 | 398 |
| 401 delete s; | 399 delete s; |
| 402 memory_allocator->TearDown(); | 400 memory_allocator->TearDown(); |
| 403 delete memory_allocator; | 401 delete memory_allocator; |
| 404 } | 402 } |
| 405 | 403 |
| 406 | 404 |
| 407 TEST(CompactionSpace) { | 405 TEST(CompactionSpace) { |
| 408 Isolate* isolate = CcTest::i_isolate(); | 406 Isolate* isolate = CcTest::i_isolate(); |
| 409 Heap* heap = isolate->heap(); | 407 Heap* heap = isolate->heap(); |
| 410 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); | 408 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 411 CHECK(memory_allocator != nullptr); | 409 CHECK(memory_allocator != nullptr); |
| 412 CHECK( | 410 CHECK(memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize(), |
| 413 memory_allocator->SetUp(heap->MaxReserved(), heap->MaxExecutableSize())); | 411 0)); |
| 414 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); | 412 TestMemoryAllocatorScope test_scope(isolate, memory_allocator); |
| 415 | 413 |
| 416 CompactionSpace* compaction_space = | 414 CompactionSpace* compaction_space = |
| 417 new CompactionSpace(heap, OLD_SPACE, NOT_EXECUTABLE); | 415 new CompactionSpace(heap, OLD_SPACE, NOT_EXECUTABLE); |
| 418 CHECK(compaction_space != NULL); | 416 CHECK(compaction_space != NULL); |
| 419 CHECK(compaction_space->SetUp()); | 417 CHECK(compaction_space->SetUp()); |
| 420 | 418 |
| 421 OldSpace* old_space = new OldSpace(heap, OLD_SPACE, NOT_EXECUTABLE); | 419 OldSpace* old_space = new OldSpace(heap, OLD_SPACE, NOT_EXECUTABLE); |
| 422 CHECK(old_space != NULL); | 420 CHECK(old_space != NULL); |
| 423 CHECK(old_space->SetUp()); | 421 CHECK(old_space->SetUp()); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 new_space->RemoveAllocationObserver(&observer2); | 673 new_space->RemoveAllocationObserver(&observer2); |
| 676 | 674 |
| 677 CHECK_EQ(observer1.count(), 32); | 675 CHECK_EQ(observer1.count(), 32); |
| 678 CHECK_EQ(observer2.count(), 28); | 676 CHECK_EQ(observer2.count(), 28); |
| 679 } | 677 } |
| 680 isolate->Dispose(); | 678 isolate->Dispose(); |
| 681 } | 679 } |
| 682 | 680 |
| 683 } // namespace internal | 681 } // namespace internal |
| 684 } // namespace v8 | 682 } // namespace v8 |
| OLD | NEW |