ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +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 | #include <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | #include "cctest.h" |
| 32 | |
| 33 | using namespace v8::internal; |
| 34 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 35 | static void VerifyRegionMarking(Address page_start) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 36 | Page* p = Page::FromAddress(page_start); |
| 37 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 38 | p->SetRegionMarks(Page::kAllRegionsCleanMarks); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 39 | |
| 40 | for (Address addr = p->ObjectAreaStart(); |
| 41 | addr < p->ObjectAreaEnd(); |
| 42 | addr += kPointerSize) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 43 | CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | for (Address addr = p->ObjectAreaStart(); |
| 47 | addr < p->ObjectAreaEnd(); |
| 48 | addr += kPointerSize) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 49 | Page::FromAddress(addr)->MarkRegionDirty(addr); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | for (Address addr = p->ObjectAreaStart(); |
| 53 | addr < p->ObjectAreaEnd(); |
| 54 | addr += kPointerSize) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 55 | CHECK(Page::FromAddress(addr)->IsRegionDirty(addr)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | TEST(Page) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 61 | 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 68 | // Initialized Page has heap pointer, normally set by memory_allocator. |
| 69 | p->heap_ = HEAP; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 70 | CHECK(p->address() == page_start); |
| 71 | CHECK(p->is_valid()); |
| 72 | |
| 73 | p->opaque_header = 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 74 | p->SetIsLargeObjectPage(false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 75 | 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.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 87 | // test region marking |
| 88 | VerifyRegionMarking(page_start); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 89 | |
| 90 | DeleteArray(mem); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | TEST(MemoryAllocator) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 95 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 100 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 101 | OldSpace faked_space(HEAP, |
| 102 | HEAP->MaxReserved(), |
| 103 | OLD_POINTER_SPACE, |
| 104 | NOT_EXECUTABLE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 105 | int total_pages = 0; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 106 | int requested = MemoryAllocator::kPagesPerChunk; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 107 | int allocated; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 108 | // If we request n pages, we should get n or n - 1. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 109 | Page* first_page = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 110 | isolate->memory_allocator()->AllocatePages( |
| 111 | requested, &allocated, &faked_space); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 112 | CHECK(first_page->is_valid()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 113 | CHECK(allocated == requested || allocated == requested - 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 114 | 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 118 | CHECK(isolate->memory_allocator()->IsPageInSpace(p, &faked_space)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 119 | last_page = p; |
| 120 | } |
| 121 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 122 | // Again, we should get n or n - 1 pages. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 123 | Page* others = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 124 | isolate->memory_allocator()->AllocatePages( |
| 125 | requested, &allocated, &faked_space); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 126 | CHECK(others->is_valid()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 127 | CHECK(allocated == requested || allocated == requested - 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 128 | total_pages += allocated; |
| 129 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 130 | isolate->memory_allocator()->SetNextPage(last_page, others); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 131 | int page_count = 0; |
| 132 | for (Page* p = first_page; p->is_valid(); p = p->next_page()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 133 | CHECK(isolate->memory_allocator()->IsPageInSpace(p, &faked_space)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 134 | 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.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 142 | // 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 144 | Page* free_return = isolate->memory_allocator()->FreePages(second_page); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 145 | CHECK(free_return == second_page); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 146 | isolate->memory_allocator()->SetNextPage(first_page, free_return); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 147 | |
| 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 150 | Page* invalid_page = isolate->memory_allocator()->FreePages(first_page); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 151 | CHECK(!invalid_page->is_valid()); |
| 152 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 153 | isolate->memory_allocator()->TearDown(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | TEST(NewSpace) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 158 | OS::Setup(); |
| 159 | CHECK(HEAP->ConfigureHeapDefault()); |
| 160 | CHECK(Isolate::Current()->memory_allocator()->Setup( |
| 161 | HEAP->MaxReserved(), HEAP->MaxExecutableSize())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 162 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 163 | NewSpace new_space(HEAP); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 164 | |
| 165 | void* chunk = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 166 | Isolate::Current()->memory_allocator()->ReserveInitialChunk( |
| 167 | 4 * HEAP->ReservedSemiSpaceSize()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 168 | CHECK(chunk != NULL); |
| 169 | Address start = RoundUp(static_cast<Address>(chunk), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 170 | 2 * HEAP->ReservedSemiSpaceSize()); |
| 171 | CHECK(new_space.Setup(start, 2 * HEAP->ReservedSemiSpaceSize())); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 172 | CHECK(new_space.HasBeenSetup()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 173 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 174 | while (new_space.Available() >= Page::kMaxHeapObjectSize) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 175 | Object* obj = |
| 176 | new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 177 | CHECK(new_space.Contains(HeapObject::cast(obj))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 178 | } |
| 179 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 180 | new_space.TearDown(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 181 | Isolate::Current()->memory_allocator()->TearDown(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | TEST(OldSpace) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 186 | OS::Setup(); |
| 187 | CHECK(HEAP->ConfigureHeapDefault()); |
| 188 | CHECK(Isolate::Current()->memory_allocator()->Setup( |
| 189 | HEAP->MaxReserved(), HEAP->MaxExecutableSize())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 190 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 191 | OldSpace* s = new OldSpace(HEAP, |
| 192 | HEAP->MaxOldGenerationSize(), |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 193 | OLD_POINTER_SPACE, |
| 194 | NOT_EXECUTABLE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 195 | CHECK(s != NULL); |
| 196 | |
| 197 | void* chunk = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 198 | Isolate::Current()->memory_allocator()->ReserveInitialChunk( |
| 199 | 4 * HEAP->ReservedSemiSpaceSize()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 200 | CHECK(chunk != NULL); |
| 201 | Address start = static_cast<Address>(chunk); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 202 | size_t size = RoundUp(start, 2 * HEAP->ReservedSemiSpaceSize()) - start; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 203 | |
| 204 | CHECK(s->Setup(start, size)); |
| 205 | |
| 206 | while (s->Available() > 0) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 207 | s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | s->TearDown(); |
| 211 | delete s; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 212 | Isolate::Current()->memory_allocator()->TearDown(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
| 216 | TEST(LargeObjectSpace) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 217 | OS::Setup(); |
| 218 | CHECK(HEAP->Setup(false)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 219 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 220 | LargeObjectSpace* lo = HEAP->lo_space(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 221 | CHECK(lo != NULL); |
| 222 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 223 | Map* faked_map = reinterpret_cast<Map*>(HeapObject::FromAddress(0)); |
| 224 | int lo_size = Page::kPageSize; |
| 225 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 226 | Object* obj = lo->AllocateRaw(lo_size)->ToObjectUnchecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 227 | 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.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 239 | intptr_t available = lo->Available(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 240 | { MaybeObject* maybe_obj = lo->AllocateRaw(lo_size); |
| 241 | if (!maybe_obj->ToObject(&obj)) break; |
| 242 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 243 | HeapObject::cast(obj)->set_map(faked_map); |
| 244 | CHECK(lo->Available() < available); |
| 245 | }; |
| 246 | |
| 247 | CHECK(!lo->IsEmpty()); |
| 248 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 249 | CHECK(lo->AllocateRaw(lo_size)->IsFailure()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 250 | |
| 251 | lo->TearDown(); |
| 252 | delete lo; |
| 253 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 254 | Isolate::Current()->memory_allocator()->TearDown(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 255 | } |