| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 | 59 |
| 60 TEST(Page) { | 60 TEST(Page) { |
| 61 byte* mem = NewArray<byte>(2*Page::kPageSize); | 61 byte* mem = NewArray<byte>(2*Page::kPageSize); |
| 62 CHECK(mem != NULL); | 62 CHECK(mem != NULL); |
| 63 | 63 |
| 64 Address start = reinterpret_cast<Address>(mem); | 64 Address start = reinterpret_cast<Address>(mem); |
| 65 Address page_start = RoundUp(start, Page::kPageSize); | 65 Address page_start = RoundUp(start, Page::kPageSize); |
| 66 | 66 |
| 67 Page* p = Page::FromAddress(page_start); | 67 Page* p = Page::FromAddress(page_start); |
| 68 // Initialized Page has heap pointer, normally set by memory_allocator. |
| 69 p->heap_ = HEAP; |
| 68 CHECK(p->address() == page_start); | 70 CHECK(p->address() == page_start); |
| 69 CHECK(p->is_valid()); | 71 CHECK(p->is_valid()); |
| 70 | 72 |
| 71 p->opaque_header = 0; | 73 p->opaque_header = 0; |
| 72 p->SetIsLargeObjectPage(false); | 74 p->SetIsLargeObjectPage(false); |
| 73 CHECK(!p->next_page()->is_valid()); | 75 CHECK(!p->next_page()->is_valid()); |
| 74 | 76 |
| 75 CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset); | 77 CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset); |
| 76 CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize); | 78 CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize); |
| 77 | 79 |
| 78 CHECK(p->Offset(page_start + Page::kObjectStartOffset) == | 80 CHECK(p->Offset(page_start + Page::kObjectStartOffset) == |
| 79 Page::kObjectStartOffset); | 81 Page::kObjectStartOffset); |
| 80 CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize); | 82 CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize); |
| 81 | 83 |
| 82 CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart()); | 84 CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart()); |
| 83 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd()); | 85 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd()); |
| 84 | 86 |
| 85 // test region marking | 87 // test region marking |
| 86 VerifyRegionMarking(page_start); | 88 VerifyRegionMarking(page_start); |
| 87 | 89 |
| 88 DeleteArray(mem); | 90 DeleteArray(mem); |
| 89 } | 91 } |
| 90 | 92 |
| 91 | 93 |
| 92 TEST(MemoryAllocator) { | 94 TEST(MemoryAllocator) { |
| 93 CHECK(Heap::ConfigureHeapDefault()); | 95 OS::Setup(); |
| 94 CHECK(MemoryAllocator::Setup(Heap::MaxReserved(), Heap::MaxExecutableSize())); | 96 Isolate* isolate = Isolate::Current(); |
| 97 CHECK(HEAP->ConfigureHeapDefault()); |
| 98 CHECK(isolate->memory_allocator()->Setup(HEAP->MaxReserved(), |
| 99 HEAP->MaxExecutableSize())); |
| 95 | 100 |
| 96 OldSpace faked_space(Heap::MaxReserved(), OLD_POINTER_SPACE, NOT_EXECUTABLE); | 101 OldSpace faked_space(HEAP, |
| 102 HEAP->MaxReserved(), |
| 103 OLD_POINTER_SPACE, |
| 104 NOT_EXECUTABLE); |
| 97 int total_pages = 0; | 105 int total_pages = 0; |
| 98 int requested = MemoryAllocator::kPagesPerChunk; | 106 int requested = MemoryAllocator::kPagesPerChunk; |
| 99 int allocated; | 107 int allocated; |
| 100 // If we request n pages, we should get n or n - 1. | 108 // If we request n pages, we should get n or n - 1. |
| 101 Page* first_page = | 109 Page* first_page = |
| 102 MemoryAllocator::AllocatePages(requested, &allocated, &faked_space); | 110 isolate->memory_allocator()->AllocatePages( |
| 111 requested, &allocated, &faked_space); |
| 103 CHECK(first_page->is_valid()); | 112 CHECK(first_page->is_valid()); |
| 104 CHECK(allocated == requested || allocated == requested - 1); | 113 CHECK(allocated == requested || allocated == requested - 1); |
| 105 total_pages += allocated; | 114 total_pages += allocated; |
| 106 | 115 |
| 107 Page* last_page = first_page; | 116 Page* last_page = first_page; |
| 108 for (Page* p = first_page; p->is_valid(); p = p->next_page()) { | 117 for (Page* p = first_page; p->is_valid(); p = p->next_page()) { |
| 109 CHECK(MemoryAllocator::IsPageInSpace(p, &faked_space)); | 118 CHECK(isolate->memory_allocator()->IsPageInSpace(p, &faked_space)); |
| 110 last_page = p; | 119 last_page = p; |
| 111 } | 120 } |
| 112 | 121 |
| 113 // Again, we should get n or n - 1 pages. | 122 // Again, we should get n or n - 1 pages. |
| 114 Page* others = | 123 Page* others = |
| 115 MemoryAllocator::AllocatePages(requested, &allocated, &faked_space); | 124 isolate->memory_allocator()->AllocatePages( |
| 125 requested, &allocated, &faked_space); |
| 116 CHECK(others->is_valid()); | 126 CHECK(others->is_valid()); |
| 117 CHECK(allocated == requested || allocated == requested - 1); | 127 CHECK(allocated == requested || allocated == requested - 1); |
| 118 total_pages += allocated; | 128 total_pages += allocated; |
| 119 | 129 |
| 120 MemoryAllocator::SetNextPage(last_page, others); | 130 isolate->memory_allocator()->SetNextPage(last_page, others); |
| 121 int page_count = 0; | 131 int page_count = 0; |
| 122 for (Page* p = first_page; p->is_valid(); p = p->next_page()) { | 132 for (Page* p = first_page; p->is_valid(); p = p->next_page()) { |
| 123 CHECK(MemoryAllocator::IsPageInSpace(p, &faked_space)); | 133 CHECK(isolate->memory_allocator()->IsPageInSpace(p, &faked_space)); |
| 124 page_count++; | 134 page_count++; |
| 125 } | 135 } |
| 126 CHECK(total_pages == page_count); | 136 CHECK(total_pages == page_count); |
| 127 | 137 |
| 128 Page* second_page = first_page->next_page(); | 138 Page* second_page = first_page->next_page(); |
| 129 CHECK(second_page->is_valid()); | 139 CHECK(second_page->is_valid()); |
| 130 | 140 |
| 131 // Freeing pages at the first chunk starting at or after the second page | 141 // Freeing pages at the first chunk starting at or after the second page |
| 132 // should free the entire second chunk. It will return the page it was passed | 142 // should free the entire second chunk. It will return the page it was passed |
| 133 // (since the second page was in the first chunk). | 143 // (since the second page was in the first chunk). |
| 134 Page* free_return = MemoryAllocator::FreePages(second_page); | 144 Page* free_return = isolate->memory_allocator()->FreePages(second_page); |
| 135 CHECK(free_return == second_page); | 145 CHECK(free_return == second_page); |
| 136 MemoryAllocator::SetNextPage(first_page, free_return); | 146 isolate->memory_allocator()->SetNextPage(first_page, free_return); |
| 137 | 147 |
| 138 // Freeing pages in the first chunk starting at the first page should free | 148 // Freeing pages in the first chunk starting at the first page should free |
| 139 // the first chunk and return an invalid page. | 149 // the first chunk and return an invalid page. |
| 140 Page* invalid_page = MemoryAllocator::FreePages(first_page); | 150 Page* invalid_page = isolate->memory_allocator()->FreePages(first_page); |
| 141 CHECK(!invalid_page->is_valid()); | 151 CHECK(!invalid_page->is_valid()); |
| 142 | 152 |
| 143 MemoryAllocator::TearDown(); | 153 isolate->memory_allocator()->TearDown(); |
| 144 } | 154 } |
| 145 | 155 |
| 146 | 156 |
| 147 TEST(NewSpace) { | 157 TEST(NewSpace) { |
| 148 CHECK(Heap::ConfigureHeapDefault()); | 158 OS::Setup(); |
| 149 CHECK(MemoryAllocator::Setup(Heap::MaxReserved(), Heap::MaxExecutableSize())); | 159 CHECK(HEAP->ConfigureHeapDefault()); |
| 160 CHECK(Isolate::Current()->memory_allocator()->Setup( |
| 161 HEAP->MaxReserved(), HEAP->MaxExecutableSize())); |
| 150 | 162 |
| 151 NewSpace new_space; | 163 NewSpace new_space(HEAP); |
| 152 | 164 |
| 153 void* chunk = | 165 void* chunk = |
| 154 MemoryAllocator::ReserveInitialChunk(4 * Heap::ReservedSemiSpaceSize()); | 166 Isolate::Current()->memory_allocator()->ReserveInitialChunk( |
| 167 4 * HEAP->ReservedSemiSpaceSize()); |
| 155 CHECK(chunk != NULL); | 168 CHECK(chunk != NULL); |
| 156 Address start = RoundUp(static_cast<Address>(chunk), | 169 Address start = RoundUp(static_cast<Address>(chunk), |
| 157 2 * Heap::ReservedSemiSpaceSize()); | 170 2 * HEAP->ReservedSemiSpaceSize()); |
| 158 CHECK(new_space.Setup(start, 2 * Heap::ReservedSemiSpaceSize())); | 171 CHECK(new_space.Setup(start, 2 * HEAP->ReservedSemiSpaceSize())); |
| 159 CHECK(new_space.HasBeenSetup()); | 172 CHECK(new_space.HasBeenSetup()); |
| 160 | 173 |
| 161 while (new_space.Available() >= Page::kMaxHeapObjectSize) { | 174 while (new_space.Available() >= Page::kMaxHeapObjectSize) { |
| 162 Object* obj = | 175 Object* obj = |
| 163 new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); | 176 new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
| 164 CHECK(new_space.Contains(HeapObject::cast(obj))); | 177 CHECK(new_space.Contains(HeapObject::cast(obj))); |
| 165 } | 178 } |
| 166 | 179 |
| 167 new_space.TearDown(); | 180 new_space.TearDown(); |
| 168 MemoryAllocator::TearDown(); | 181 Isolate::Current()->memory_allocator()->TearDown(); |
| 169 } | 182 } |
| 170 | 183 |
| 171 | 184 |
| 172 TEST(OldSpace) { | 185 TEST(OldSpace) { |
| 173 CHECK(Heap::ConfigureHeapDefault()); | 186 OS::Setup(); |
| 174 CHECK(MemoryAllocator::Setup(Heap::MaxReserved(), Heap::MaxExecutableSize())); | 187 CHECK(HEAP->ConfigureHeapDefault()); |
| 188 CHECK(Isolate::Current()->memory_allocator()->Setup( |
| 189 HEAP->MaxReserved(), HEAP->MaxExecutableSize())); |
| 175 | 190 |
| 176 OldSpace* s = new OldSpace(Heap::MaxOldGenerationSize(), | 191 OldSpace* s = new OldSpace(HEAP, |
| 192 HEAP->MaxOldGenerationSize(), |
| 177 OLD_POINTER_SPACE, | 193 OLD_POINTER_SPACE, |
| 178 NOT_EXECUTABLE); | 194 NOT_EXECUTABLE); |
| 179 CHECK(s != NULL); | 195 CHECK(s != NULL); |
| 180 | 196 |
| 181 void* chunk = | 197 void* chunk = |
| 182 MemoryAllocator::ReserveInitialChunk(4 * Heap::ReservedSemiSpaceSize()); | 198 Isolate::Current()->memory_allocator()->ReserveInitialChunk( |
| 199 4 * HEAP->ReservedSemiSpaceSize()); |
| 183 CHECK(chunk != NULL); | 200 CHECK(chunk != NULL); |
| 184 Address start = static_cast<Address>(chunk); | 201 Address start = static_cast<Address>(chunk); |
| 185 size_t size = RoundUp(start, 2 * Heap::ReservedSemiSpaceSize()) - start; | 202 size_t size = RoundUp(start, 2 * HEAP->ReservedSemiSpaceSize()) - start; |
| 186 | 203 |
| 187 CHECK(s->Setup(start, size)); | 204 CHECK(s->Setup(start, size)); |
| 188 | 205 |
| 189 while (s->Available() > 0) { | 206 while (s->Available() > 0) { |
| 190 s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); | 207 s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
| 191 } | 208 } |
| 192 | 209 |
| 193 s->TearDown(); | 210 s->TearDown(); |
| 194 delete s; | 211 delete s; |
| 195 MemoryAllocator::TearDown(); | 212 Isolate::Current()->memory_allocator()->TearDown(); |
| 196 } | 213 } |
| 197 | 214 |
| 198 | 215 |
| 199 TEST(LargeObjectSpace) { | 216 TEST(LargeObjectSpace) { |
| 200 CHECK(Heap::Setup(false)); | 217 OS::Setup(); |
| 218 CHECK(HEAP->Setup(false)); |
| 201 | 219 |
| 202 LargeObjectSpace* lo = Heap::lo_space(); | 220 LargeObjectSpace* lo = HEAP->lo_space(); |
| 203 CHECK(lo != NULL); | 221 CHECK(lo != NULL); |
| 204 | 222 |
| 205 Map* faked_map = reinterpret_cast<Map*>(HeapObject::FromAddress(0)); | 223 Map* faked_map = reinterpret_cast<Map*>(HeapObject::FromAddress(0)); |
| 206 int lo_size = Page::kPageSize; | 224 int lo_size = Page::kPageSize; |
| 207 | 225 |
| 208 Object* obj = lo->AllocateRaw(lo_size)->ToObjectUnchecked(); | 226 Object* obj = lo->AllocateRaw(lo_size)->ToObjectUnchecked(); |
| 209 CHECK(obj->IsHeapObject()); | 227 CHECK(obj->IsHeapObject()); |
| 210 | 228 |
| 211 HeapObject* ho = HeapObject::cast(obj); | 229 HeapObject* ho = HeapObject::cast(obj); |
| 212 ho->set_map(faked_map); | 230 ho->set_map(faked_map); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 226 CHECK(lo->Available() < available); | 244 CHECK(lo->Available() < available); |
| 227 }; | 245 }; |
| 228 | 246 |
| 229 CHECK(!lo->IsEmpty()); | 247 CHECK(!lo->IsEmpty()); |
| 230 | 248 |
| 231 CHECK(lo->AllocateRaw(lo_size)->IsFailure()); | 249 CHECK(lo->AllocateRaw(lo_size)->IsFailure()); |
| 232 | 250 |
| 233 lo->TearDown(); | 251 lo->TearDown(); |
| 234 delete lo; | 252 delete lo; |
| 235 | 253 |
| 236 MemoryAllocator::TearDown(); | 254 Isolate::Current()->memory_allocator()->TearDown(); |
| 237 } | 255 } |
| OLD | NEW |