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); |
| 68 | CHECK(p->address() == page_start); |
| 69 | CHECK(p->is_valid()); |
| 70 | |
| 71 | p->opaque_header = 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 72 | p->SetIsLargeObjectPage(false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 73 | CHECK(!p->next_page()->is_valid()); |
| 74 | |
| 75 | CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset); |
| 76 | CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize); |
| 77 | |
| 78 | CHECK(p->Offset(page_start + Page::kObjectStartOffset) == |
| 79 | Page::kObjectStartOffset); |
| 80 | CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize); |
| 81 | |
| 82 | CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart()); |
| 83 | CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd()); |
| 84 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 85 | // test region marking |
| 86 | VerifyRegionMarking(page_start); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 87 | |
| 88 | DeleteArray(mem); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | TEST(MemoryAllocator) { |
| 93 | CHECK(Heap::ConfigureHeapDefault()); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 94 | CHECK(MemoryAllocator::Setup(Heap::MaxReserved(), Heap::MaxExecutableSize())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 95 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 96 | OldSpace faked_space(Heap::MaxReserved(), OLD_POINTER_SPACE, NOT_EXECUTABLE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 97 | int total_pages = 0; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 98 | int requested = MemoryAllocator::kPagesPerChunk; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 99 | int allocated; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 100 | // 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] | 101 | Page* first_page = |
| 102 | MemoryAllocator::AllocatePages(requested, &allocated, &faked_space); |
| 103 | CHECK(first_page->is_valid()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 104 | CHECK(allocated == requested || allocated == requested - 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 105 | total_pages += allocated; |
| 106 | |
| 107 | Page* last_page = first_page; |
| 108 | for (Page* p = first_page; p->is_valid(); p = p->next_page()) { |
| 109 | CHECK(MemoryAllocator::IsPageInSpace(p, &faked_space)); |
| 110 | last_page = p; |
| 111 | } |
| 112 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 113 | // Again, we should get n or n - 1 pages. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 114 | Page* others = |
| 115 | MemoryAllocator::AllocatePages(requested, &allocated, &faked_space); |
| 116 | CHECK(others->is_valid()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 117 | CHECK(allocated == requested || allocated == requested - 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 118 | total_pages += allocated; |
| 119 | |
| 120 | MemoryAllocator::SetNextPage(last_page, others); |
| 121 | int page_count = 0; |
| 122 | for (Page* p = first_page; p->is_valid(); p = p->next_page()) { |
| 123 | CHECK(MemoryAllocator::IsPageInSpace(p, &faked_space)); |
| 124 | page_count++; |
| 125 | } |
| 126 | CHECK(total_pages == page_count); |
| 127 | |
| 128 | Page* second_page = first_page->next_page(); |
| 129 | CHECK(second_page->is_valid()); |
| 130 | |
| 131 | // 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] | 132 | // should free the entire second chunk. It will return the page it was passed |
| 133 | // (since the second page was in the first chunk). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 134 | Page* free_return = MemoryAllocator::FreePages(second_page); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 135 | CHECK(free_return == second_page); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 136 | MemoryAllocator::SetNextPage(first_page, free_return); |
| 137 | |
| 138 | // Freeing pages in the first chunk starting at the first page should free |
| 139 | // the first chunk and return an invalid page. |
| 140 | Page* invalid_page = MemoryAllocator::FreePages(first_page); |
| 141 | CHECK(!invalid_page->is_valid()); |
| 142 | |
| 143 | MemoryAllocator::TearDown(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | TEST(NewSpace) { |
| 148 | CHECK(Heap::ConfigureHeapDefault()); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 149 | CHECK(MemoryAllocator::Setup(Heap::MaxReserved(), Heap::MaxExecutableSize())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 150 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 151 | NewSpace new_space; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 152 | |
| 153 | void* chunk = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 154 | MemoryAllocator::ReserveInitialChunk(4 * Heap::ReservedSemiSpaceSize()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 155 | CHECK(chunk != NULL); |
| 156 | Address start = RoundUp(static_cast<Address>(chunk), |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 157 | 2 * Heap::ReservedSemiSpaceSize()); |
| 158 | CHECK(new_space.Setup(start, 2 * Heap::ReservedSemiSpaceSize())); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 159 | CHECK(new_space.HasBeenSetup()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 160 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 161 | while (new_space.Available() >= Page::kMaxHeapObjectSize) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 162 | Object* obj = |
| 163 | new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 164 | CHECK(new_space.Contains(HeapObject::cast(obj))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 165 | } |
| 166 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 167 | new_space.TearDown(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 168 | MemoryAllocator::TearDown(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | TEST(OldSpace) { |
| 173 | CHECK(Heap::ConfigureHeapDefault()); |
ager@chromium.org | 01fe7df | 2010-11-10 11:59:11 +0000 | [diff] [blame] | 174 | CHECK(MemoryAllocator::Setup(Heap::MaxReserved(), Heap::MaxExecutableSize())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 175 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 176 | OldSpace* s = new OldSpace(Heap::MaxOldGenerationSize(), |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 177 | OLD_POINTER_SPACE, |
| 178 | NOT_EXECUTABLE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 179 | CHECK(s != NULL); |
| 180 | |
| 181 | void* chunk = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 182 | MemoryAllocator::ReserveInitialChunk(4 * Heap::ReservedSemiSpaceSize()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 183 | CHECK(chunk != NULL); |
| 184 | Address start = static_cast<Address>(chunk); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 185 | size_t size = RoundUp(start, 2 * Heap::ReservedSemiSpaceSize()) - start; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 186 | |
| 187 | CHECK(s->Setup(start, size)); |
| 188 | |
| 189 | while (s->Available() > 0) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 190 | s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | s->TearDown(); |
| 194 | delete s; |
| 195 | MemoryAllocator::TearDown(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | TEST(LargeObjectSpace) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 200 | CHECK(Heap::Setup(false)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 201 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 202 | LargeObjectSpace* lo = Heap::lo_space(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 203 | CHECK(lo != NULL); |
| 204 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 205 | Map* faked_map = reinterpret_cast<Map*>(HeapObject::FromAddress(0)); |
| 206 | int lo_size = Page::kPageSize; |
| 207 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 208 | Object* obj = lo->AllocateRaw(lo_size)->ToObjectUnchecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 209 | CHECK(obj->IsHeapObject()); |
| 210 | |
| 211 | HeapObject* ho = HeapObject::cast(obj); |
| 212 | ho->set_map(faked_map); |
| 213 | |
| 214 | CHECK(lo->Contains(HeapObject::cast(obj))); |
| 215 | |
| 216 | CHECK(lo->FindObject(ho->address()) == obj); |
| 217 | |
| 218 | CHECK(lo->Contains(ho)); |
| 219 | |
| 220 | while (true) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 221 | intptr_t available = lo->Available(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 222 | { MaybeObject* maybe_obj = lo->AllocateRaw(lo_size); |
| 223 | if (!maybe_obj->ToObject(&obj)) break; |
| 224 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 225 | HeapObject::cast(obj)->set_map(faked_map); |
| 226 | CHECK(lo->Available() < available); |
| 227 | }; |
| 228 | |
| 229 | CHECK(!lo->IsEmpty()); |
| 230 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 231 | CHECK(lo->AllocateRaw(lo_size)->IsFailure()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 232 | |
| 233 | lo->TearDown(); |
| 234 | delete lo; |
| 235 | |
| 236 | MemoryAllocator::TearDown(); |
| 237 | } |