1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
33 using namespace v8::internal;
36 static void VerifyRegionMarking(Address page_start) {
37 #ifdef ENABLE_CARDMARKING_WRITE_BARRIER
38 Page* p = Page::FromAddress(page_start);
40 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
42 for (Address addr = p->ObjectAreaStart();
43 addr < p->ObjectAreaEnd();
44 addr += kPointerSize) {
45 CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr));
48 for (Address addr = p->ObjectAreaStart();
49 addr < p->ObjectAreaEnd();
50 addr += kPointerSize) {
51 Page::FromAddress(addr)->MarkRegionDirty(addr);
54 for (Address addr = p->ObjectAreaStart();
55 addr < p->ObjectAreaEnd();
56 addr += kPointerSize) {
57 CHECK(Page::FromAddress(addr)->IsRegionDirty(addr));
64 // TODO(gc) you can no longer allocate pages like this. Details are hidden.
67 byte* mem = NewArray<byte>(2*Page::kPageSize);
70 Address start = reinterpret_cast<Address>(mem);
71 Address page_start = RoundUp(start, Page::kPageSize);
73 Page* p = Page::FromAddress(page_start);
74 // Initialized Page has heap pointer, normally set by memory_allocator.
76 CHECK(p->address() == page_start);
80 p->SetIsLargeObjectPage(false);
81 CHECK(!p->next_page()->is_valid());
83 CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset);
84 CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize);
86 CHECK(p->Offset(page_start + Page::kObjectStartOffset) ==
87 Page::kObjectStartOffset);
88 CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize);
90 CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart());
91 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd());
93 // test region marking
94 VerifyRegionMarking(page_start);
104 // Temporarily sets a given allocator in an isolate.
105 class TestMemoryAllocatorScope {
107 TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator)
109 old_allocator_(isolate->memory_allocator_) {
110 isolate->memory_allocator_ = allocator;
113 ~TestMemoryAllocatorScope() {
114 isolate_->memory_allocator_ = old_allocator_;
119 MemoryAllocator* old_allocator_;
121 DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope);
124 } } // namespace v8::internal
127 TEST(MemoryAllocator) {
129 Isolate* isolate = Isolate::Current();
130 isolate->InitializeLoggingAndCounters();
131 Heap* heap = isolate->heap();
132 CHECK(isolate->heap()->ConfigureHeapDefault());
134 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
135 CHECK(memory_allocator->Setup(heap->MaxReserved(),
136 heap->MaxExecutableSize()));
139 OldSpace faked_space(heap,
144 memory_allocator->AllocatePage(&faked_space, NOT_EXECUTABLE);
146 first_page->InsertAfter(faked_space.anchor()->prev_page());
147 CHECK(first_page->is_valid());
148 CHECK(first_page->next_page() == faked_space.anchor());
151 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
152 CHECK(p->owner() == &faked_space);
155 // Again, we should get n or n - 1 pages.
157 memory_allocator->AllocatePage(&faked_space, NOT_EXECUTABLE);
158 CHECK(other->is_valid());
160 other->InsertAfter(first_page);
162 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
163 CHECK(p->owner() == &faked_space);
166 CHECK(total_pages == page_count);
168 Page* second_page = first_page->next_page();
169 CHECK(second_page->is_valid());
170 memory_allocator->Free(first_page);
171 memory_allocator->Free(second_page);
172 memory_allocator->TearDown();
173 delete memory_allocator;
179 Isolate* isolate = Isolate::Current();
180 isolate->InitializeLoggingAndCounters();
181 Heap* heap = isolate->heap();
182 CHECK(heap->ConfigureHeapDefault());
183 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
184 CHECK(memory_allocator->Setup(heap->MaxReserved(),
185 heap->MaxExecutableSize()));
186 TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
188 NewSpace new_space(heap);
190 CHECK(new_space.Setup(HEAP->ReservedSemiSpaceSize(),
191 HEAP->ReservedSemiSpaceSize()));
192 CHECK(new_space.HasBeenSetup());
194 while (new_space.Available() >= Page::kMaxHeapObjectSize) {
196 new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked();
197 CHECK(new_space.Contains(HeapObject::cast(obj)));
200 new_space.TearDown();
201 memory_allocator->TearDown();
202 delete memory_allocator;
208 Isolate* isolate = Isolate::Current();
209 isolate->InitializeLoggingAndCounters();
210 Heap* heap = isolate->heap();
211 CHECK(heap->ConfigureHeapDefault());
212 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
213 CHECK(memory_allocator->Setup(heap->MaxReserved(),
214 heap->MaxExecutableSize()));
215 TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
217 OldSpace* s = new OldSpace(heap,
218 heap->MaxOldGenerationSize(),
225 while (s->Available() > 0) {
226 s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked();
231 memory_allocator->TearDown();
232 delete memory_allocator;
236 TEST(LargeObjectSpace) {
237 v8::V8::Initialize();
239 LargeObjectSpace* lo = HEAP->lo_space();
242 int lo_size = Page::kPageSize;
244 Object* obj = lo->AllocateRaw(lo_size, NOT_EXECUTABLE)->ToObjectUnchecked();
245 CHECK(obj->IsHeapObject());
247 HeapObject* ho = HeapObject::cast(obj);
249 CHECK(lo->Contains(HeapObject::cast(obj)));
251 CHECK(lo->FindObject(ho->address()) == obj);
253 CHECK(lo->Contains(ho));
256 intptr_t available = lo->Available();
257 { MaybeObject* maybe_obj = lo->AllocateRaw(lo_size, NOT_EXECUTABLE);
258 if (!maybe_obj->ToObject(&obj)) break;
260 CHECK(lo->Available() < available);
263 CHECK(!lo->IsEmpty());
265 CHECK(lo->AllocateRaw(lo_size, NOT_EXECUTABLE)->IsFailure());