blob: de0c41e2b83dfb9e32e46ebe7f72aedc8d2807cd [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// 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#include <stdlib.h>
29
30#include "v8.h"
31#include "cctest.h"
32
33using namespace v8::internal;
34
ricow@chromium.org30ce4112010-05-31 10:38:25 +000035static void VerifyRegionMarking(Address page_start) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000036 Page* p = Page::FromAddress(page_start);
37
ricow@chromium.org30ce4112010-05-31 10:38:25 +000038 p->SetRegionMarks(Page::kAllRegionsCleanMarks);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000039
40 for (Address addr = p->ObjectAreaStart();
41 addr < p->ObjectAreaEnd();
42 addr += kPointerSize) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000043 CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044 }
45
46 for (Address addr = p->ObjectAreaStart();
47 addr < p->ObjectAreaEnd();
48 addr += kPointerSize) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000049 Page::FromAddress(addr)->MarkRegionDirty(addr);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050 }
51
52 for (Address addr = p->ObjectAreaStart();
53 addr < p->ObjectAreaEnd();
54 addr += kPointerSize) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +000055 CHECK(Page::FromAddress(addr)->IsRegionDirty(addr));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000056 }
57}
58
59
60TEST(Page) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000061 byte* mem = NewArray<byte>(2*Page::kPageSize);
62 CHECK(mem != NULL);
63
64 Address start = reinterpret_cast<Address>(mem);
65 Address page_start = RoundUp(start, Page::kPageSize);
66
67 Page* p = Page::FromAddress(page_start);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068 // Initialized Page has heap pointer, normally set by memory_allocator.
69 p->heap_ = HEAP;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000070 CHECK(p->address() == page_start);
71 CHECK(p->is_valid());
72
73 p->opaque_header = 0;
fschneider@chromium.org013f3e12010-04-26 13:27:52 +000074 p->SetIsLargeObjectPage(false);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000075 CHECK(!p->next_page()->is_valid());
76
77 CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset);
78 CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize);
79
80 CHECK(p->Offset(page_start + Page::kObjectStartOffset) ==
81 Page::kObjectStartOffset);
82 CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize);
83
84 CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart());
85 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd());
86
ricow@chromium.org30ce4112010-05-31 10:38:25 +000087 // test region marking
88 VerifyRegionMarking(page_start);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000089
90 DeleteArray(mem);
91}
92
93
94TEST(MemoryAllocator) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 OS::Setup();
96 Isolate* isolate = Isolate::Current();
97 CHECK(HEAP->ConfigureHeapDefault());
98 CHECK(isolate->memory_allocator()->Setup(HEAP->MaxReserved(),
99 HEAP->MaxExecutableSize()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000100
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000101 OldSpace faked_space(HEAP,
102 HEAP->MaxReserved(),
103 OLD_POINTER_SPACE,
104 NOT_EXECUTABLE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000105 int total_pages = 0;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000106 int requested = MemoryAllocator::kPagesPerChunk;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000107 int allocated;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000108 // If we request n pages, we should get n or n - 1.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000109 Page* first_page =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000110 isolate->memory_allocator()->AllocatePages(
111 requested, &allocated, &faked_space);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000112 CHECK(first_page->is_valid());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000113 CHECK(allocated == requested || allocated == requested - 1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000114 total_pages += allocated;
115
116 Page* last_page = first_page;
117 for (Page* p = first_page; p->is_valid(); p = p->next_page()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 CHECK(isolate->memory_allocator()->IsPageInSpace(p, &faked_space));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000119 last_page = p;
120 }
121
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000122 // Again, we should get n or n - 1 pages.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000123 Page* others =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000124 isolate->memory_allocator()->AllocatePages(
125 requested, &allocated, &faked_space);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000126 CHECK(others->is_valid());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000127 CHECK(allocated == requested || allocated == requested - 1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000128 total_pages += allocated;
129
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000130 isolate->memory_allocator()->SetNextPage(last_page, others);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000131 int page_count = 0;
132 for (Page* p = first_page; p->is_valid(); p = p->next_page()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000133 CHECK(isolate->memory_allocator()->IsPageInSpace(p, &faked_space));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000134 page_count++;
135 }
136 CHECK(total_pages == page_count);
137
138 Page* second_page = first_page->next_page();
139 CHECK(second_page->is_valid());
140
141 // Freeing pages at the first chunk starting at or after the second page
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000142 // should free the entire second chunk. It will return the page it was passed
143 // (since the second page was in the first chunk).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000144 Page* free_return = isolate->memory_allocator()->FreePages(second_page);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000145 CHECK(free_return == second_page);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 isolate->memory_allocator()->SetNextPage(first_page, free_return);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000147
148 // Freeing pages in the first chunk starting at the first page should free
149 // the first chunk and return an invalid page.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150 Page* invalid_page = isolate->memory_allocator()->FreePages(first_page);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000151 CHECK(!invalid_page->is_valid());
152
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000153 isolate->memory_allocator()->TearDown();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000154}
155
156
157TEST(NewSpace) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000158 OS::Setup();
159 CHECK(HEAP->ConfigureHeapDefault());
160 CHECK(Isolate::Current()->memory_allocator()->Setup(
161 HEAP->MaxReserved(), HEAP->MaxExecutableSize()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000162
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000163 NewSpace new_space(HEAP);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000164
165 void* chunk =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000166 Isolate::Current()->memory_allocator()->ReserveInitialChunk(
167 4 * HEAP->ReservedSemiSpaceSize());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000168 CHECK(chunk != NULL);
169 Address start = RoundUp(static_cast<Address>(chunk),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170 2 * HEAP->ReservedSemiSpaceSize());
171 CHECK(new_space.Setup(start, 2 * HEAP->ReservedSemiSpaceSize()));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000172 CHECK(new_space.HasBeenSetup());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000173
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000174 while (new_space.Available() >= Page::kMaxHeapObjectSize) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000175 Object* obj =
176 new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000177 CHECK(new_space.Contains(HeapObject::cast(obj)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000178 }
179
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000180 new_space.TearDown();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000181 Isolate::Current()->memory_allocator()->TearDown();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000182}
183
184
185TEST(OldSpace) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000186 OS::Setup();
187 CHECK(HEAP->ConfigureHeapDefault());
188 CHECK(Isolate::Current()->memory_allocator()->Setup(
189 HEAP->MaxReserved(), HEAP->MaxExecutableSize()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000190
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000191 OldSpace* s = new OldSpace(HEAP,
192 HEAP->MaxOldGenerationSize(),
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000193 OLD_POINTER_SPACE,
194 NOT_EXECUTABLE);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000195 CHECK(s != NULL);
196
197 void* chunk =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000198 Isolate::Current()->memory_allocator()->ReserveInitialChunk(
199 4 * HEAP->ReservedSemiSpaceSize());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000200 CHECK(chunk != NULL);
201 Address start = static_cast<Address>(chunk);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 size_t size = RoundUp(start, 2 * HEAP->ReservedSemiSpaceSize()) - start;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000203
204 CHECK(s->Setup(start, size));
205
206 while (s->Available() > 0) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000207 s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000208 }
209
210 s->TearDown();
211 delete s;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000212 Isolate::Current()->memory_allocator()->TearDown();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000213}
214
215
216TEST(LargeObjectSpace) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217 OS::Setup();
218 CHECK(HEAP->Setup(false));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000219
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000220 LargeObjectSpace* lo = HEAP->lo_space();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000221 CHECK(lo != NULL);
222
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000223 Map* faked_map = reinterpret_cast<Map*>(HeapObject::FromAddress(0));
224 int lo_size = Page::kPageSize;
225
lrn@chromium.org303ada72010-10-27 09:33:13 +0000226 Object* obj = lo->AllocateRaw(lo_size)->ToObjectUnchecked();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000227 CHECK(obj->IsHeapObject());
228
229 HeapObject* ho = HeapObject::cast(obj);
230 ho->set_map(faked_map);
231
232 CHECK(lo->Contains(HeapObject::cast(obj)));
233
234 CHECK(lo->FindObject(ho->address()) == obj);
235
236 CHECK(lo->Contains(ho));
237
238 while (true) {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000239 intptr_t available = lo->Available();
lrn@chromium.org303ada72010-10-27 09:33:13 +0000240 { MaybeObject* maybe_obj = lo->AllocateRaw(lo_size);
241 if (!maybe_obj->ToObject(&obj)) break;
242 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000243 HeapObject::cast(obj)->set_map(faked_map);
244 CHECK(lo->Available() < available);
245 };
246
247 CHECK(!lo->IsEmpty());
248
lrn@chromium.org303ada72010-10-27 09:33:13 +0000249 CHECK(lo->AllocateRaw(lo_size)->IsFailure());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000250
251 lo->TearDown();
252 delete lo;
253
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000254 Isolate::Current()->memory_allocator()->TearDown();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000255}