| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd()); | 91 CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd()); |
| 92 | 92 |
| 93 // test region marking | 93 // test region marking |
| 94 VerifyRegionMarking(page_start); | 94 VerifyRegionMarking(page_start); |
| 95 | 95 |
| 96 DeleteArray(mem); | 96 DeleteArray(mem); |
| 97 } | 97 } |
| 98 #endif | 98 #endif |
| 99 | 99 |
| 100 | 100 |
| 101 namespace v8 { |
| 102 namespace internal { |
| 103 |
| 104 // Temporarily sets a given allocator in an isolate. |
| 105 class TestMemoryAllocatorScope { |
| 106 public: |
| 107 TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator) |
| 108 : isolate_(isolate), |
| 109 old_allocator_(isolate->memory_allocator_) { |
| 110 isolate->memory_allocator_ = allocator; |
| 111 } |
| 112 |
| 113 ~TestMemoryAllocatorScope() { |
| 114 isolate_->memory_allocator_ = old_allocator_; |
| 115 } |
| 116 |
| 117 private: |
| 118 Isolate* isolate_; |
| 119 MemoryAllocator* old_allocator_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope); |
| 122 }; |
| 123 |
| 124 } } // namespace v8::internal |
| 125 |
| 126 |
| 101 TEST(MemoryAllocator) { | 127 TEST(MemoryAllocator) { |
| 102 OS::Setup(); | 128 OS::Setup(); |
| 103 Isolate* isolate = Isolate::Current(); | 129 Isolate* isolate = Isolate::Current(); |
| 104 CHECK(HEAP->ConfigureHeapDefault()); | 130 isolate->InitializeLoggingAndCounters(); |
| 105 CHECK(isolate->memory_allocator()->Setup(HEAP->MaxReserved(), | 131 Heap* heap = isolate->heap(); |
| 106 HEAP->MaxExecutableSize())); | 132 CHECK(isolate->heap()->ConfigureHeapDefault()); |
| 133 |
| 134 MemoryAllocator* memory_allocator = new MemoryAllocator(isolate); |
| 135 CHECK(memory_allocator->Setup(heap->MaxReserved(), |
| 136 heap->MaxExecutableSize())); |
| 107 | 137 |
| 108 int total_pages = 0; | 138 int total_pages = 0; |
| 109 OldSpace faked_space(HEAP, | 139 OldSpace faked_space(heap, |
| 110 HEAP->MaxReserved(), | 140 heap->MaxReserved(), |
| 111 OLD_POINTER_SPACE, | 141 OLD_POINTER_SPACE, |
| 112 NOT_EXECUTABLE); | 142 NOT_EXECUTABLE); |
| 113 Page* first_page = | 143 Page* first_page = |
| 114 isolate->memory_allocator()->AllocatePage(&faked_space, NOT_EXECUTABLE); | 144 memory_allocator->AllocatePage(&faked_space, NOT_EXECUTABLE); |
| 145 |
| 115 first_page->InsertAfter(faked_space.anchor()->prev_page()); | 146 first_page->InsertAfter(faked_space.anchor()->prev_page()); |
| 116 CHECK(first_page->is_valid()); | 147 CHECK(first_page->is_valid()); |
| 117 CHECK(first_page->next_page() == faked_space.anchor()); | 148 CHECK(first_page->next_page() == faked_space.anchor()); |
| 118 total_pages++; | 149 total_pages++; |
| 119 | 150 |
| 120 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) { | 151 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) { |
| 121 CHECK(p->owner() == &faked_space); | 152 CHECK(p->owner() == &faked_space); |
| 122 } | 153 } |
| 123 | 154 |
| 124 // Again, we should get n or n - 1 pages. | 155 // Again, we should get n or n - 1 pages. |
| 125 Page* other = | 156 Page* other = |
| 126 isolate->memory_allocator()->AllocatePage(&faked_space, NOT_EXECUTABLE); | 157 memory_allocator->AllocatePage(&faked_space, NOT_EXECUTABLE); |
| 127 CHECK(other->is_valid()); | 158 CHECK(other->is_valid()); |
| 128 total_pages++; | 159 total_pages++; |
| 129 other->InsertAfter(first_page); | 160 other->InsertAfter(first_page); |
| 130 int page_count = 0; | 161 int page_count = 0; |
| 131 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) { | 162 for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) { |
| 132 CHECK(p->owner() == &faked_space); | 163 CHECK(p->owner() == &faked_space); |
| 133 page_count++; | 164 page_count++; |
| 134 } | 165 } |
| 135 CHECK(total_pages == page_count); | 166 CHECK(total_pages == page_count); |
| 136 | 167 |
| 137 Page* second_page = first_page->next_page(); | 168 Page* second_page = first_page->next_page(); |
| 138 CHECK(second_page->is_valid()); | 169 CHECK(second_page->is_valid()); |
| 139 isolate->memory_allocator()->Free(first_page); | 170 memory_allocator->Free(first_page); |
| 140 isolate->memory_allocator()->Free(second_page); | 171 memory_allocator->Free(second_page); |
| 141 isolate->memory_allocator()->TearDown(); | 172 memory_allocator->TearDown(); |
| 173 delete memory_allocator; |
| 142 } | 174 } |
| 143 | 175 |
| 144 | 176 |
| 145 TEST(NewSpace) { | 177 TEST(NewSpace) { |
| 146 OS::Setup(); | 178 OS::Setup(); |
| 147 CHECK(HEAP->ConfigureHeapDefault()); | 179 Isolate* isolate = Isolate::Current(); |
| 148 CHECK(Isolate::Current()->memory_allocator()->Setup( | 180 isolate->InitializeLoggingAndCounters(); |
| 149 HEAP->MaxReserved(), HEAP->MaxExecutableSize())); | 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); |
| 150 | 187 |
| 151 NewSpace new_space(HEAP); | 188 NewSpace new_space(heap); |
| 152 | 189 |
| 153 CHECK(new_space.Setup(HEAP->ReservedSemiSpaceSize(), | 190 CHECK(new_space.Setup(HEAP->ReservedSemiSpaceSize(), |
| 154 HEAP->ReservedSemiSpaceSize())); | 191 HEAP->ReservedSemiSpaceSize())); |
| 155 CHECK(new_space.HasBeenSetup()); | 192 CHECK(new_space.HasBeenSetup()); |
| 156 | 193 |
| 157 while (new_space.Available() >= Page::kMaxHeapObjectSize) { | 194 while (new_space.Available() >= Page::kMaxHeapObjectSize) { |
| 158 Object* obj = | 195 Object* obj = |
| 159 new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); | 196 new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
| 160 CHECK(new_space.Contains(HeapObject::cast(obj))); | 197 CHECK(new_space.Contains(HeapObject::cast(obj))); |
| 161 } | 198 } |
| 162 | 199 |
| 163 new_space.TearDown(); | 200 new_space.TearDown(); |
| 164 Isolate::Current()->memory_allocator()->TearDown(); | 201 memory_allocator->TearDown(); |
| 202 delete memory_allocator; |
| 165 } | 203 } |
| 166 | 204 |
| 167 | 205 |
| 168 TEST(OldSpace) { | 206 TEST(OldSpace) { |
| 169 OS::Setup(); | 207 OS::Setup(); |
| 170 CHECK(HEAP->ConfigureHeapDefault()); | 208 Isolate* isolate = Isolate::Current(); |
| 171 CHECK(Isolate::Current()->memory_allocator()->Setup( | 209 isolate->InitializeLoggingAndCounters(); |
| 172 HEAP->MaxReserved(), HEAP->MaxExecutableSize())); | 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); |
| 173 | 216 |
| 174 OldSpace* s = new OldSpace(HEAP, | 217 OldSpace* s = new OldSpace(heap, |
| 175 HEAP->MaxOldGenerationSize(), | 218 heap->MaxOldGenerationSize(), |
| 176 OLD_POINTER_SPACE, | 219 OLD_POINTER_SPACE, |
| 177 NOT_EXECUTABLE); | 220 NOT_EXECUTABLE); |
| 178 CHECK(s != NULL); | 221 CHECK(s != NULL); |
| 179 | 222 |
| 180 CHECK(s->Setup()); | 223 CHECK(s->Setup()); |
| 181 | 224 |
| 182 while (s->Available() > 0) { | 225 while (s->Available() > 0) { |
| 183 s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); | 226 s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked(); |
| 184 } | 227 } |
| 185 | 228 |
| 186 s->TearDown(); | 229 s->TearDown(); |
| 187 delete s; | 230 delete s; |
| 188 Isolate::Current()->memory_allocator()->TearDown(); | 231 memory_allocator->TearDown(); |
| 232 delete memory_allocator; |
| 189 } | 233 } |
| 190 | 234 |
| 191 | 235 |
| 192 TEST(LargeObjectSpace) { | 236 TEST(LargeObjectSpace) { |
| 193 OS::Setup(); | 237 v8::V8::Initialize(); |
| 194 CHECK(HEAP->Setup(false)); | |
| 195 | 238 |
| 196 LargeObjectSpace* lo = HEAP->lo_space(); | 239 LargeObjectSpace* lo = HEAP->lo_space(); |
| 197 CHECK(lo != NULL); | 240 CHECK(lo != NULL); |
| 198 | 241 |
| 199 int lo_size = Page::kPageSize; | 242 int lo_size = Page::kPageSize; |
| 200 | 243 |
| 201 Object* obj = lo->AllocateRawData(lo_size)->ToObjectUnchecked(); | 244 Object* obj = lo->AllocateRawData(lo_size)->ToObjectUnchecked(); |
| 202 CHECK(obj->IsHeapObject()); | 245 CHECK(obj->IsHeapObject()); |
| 203 | 246 |
| 204 HeapObject* ho = HeapObject::cast(obj); | 247 HeapObject* ho = HeapObject::cast(obj); |
| 205 | 248 |
| 206 CHECK(lo->Contains(HeapObject::cast(obj))); | 249 CHECK(lo->Contains(HeapObject::cast(obj))); |
| 207 | 250 |
| 208 CHECK(lo->FindObject(ho->address()) == obj); | 251 CHECK(lo->FindObject(ho->address()) == obj); |
| 209 | 252 |
| 210 CHECK(lo->Contains(ho)); | 253 CHECK(lo->Contains(ho)); |
| 211 | 254 |
| 212 while (true) { | 255 while (true) { |
| 213 intptr_t available = lo->Available(); | 256 intptr_t available = lo->Available(); |
| 214 { MaybeObject* maybe_obj = lo->AllocateRawData(lo_size); | 257 { MaybeObject* maybe_obj = lo->AllocateRawData(lo_size); |
| 215 if (!maybe_obj->ToObject(&obj)) break; | 258 if (!maybe_obj->ToObject(&obj)) break; |
| 216 } | 259 } |
| 217 CHECK(lo->Available() < available); | 260 CHECK(lo->Available() < available); |
| 218 }; | 261 }; |
| 219 | 262 |
| 220 CHECK(!lo->IsEmpty()); | 263 CHECK(!lo->IsEmpty()); |
| 221 | 264 |
| 222 CHECK(lo->AllocateRawData(lo_size)->IsFailure()); | 265 CHECK(lo->AllocateRawData(lo_size)->IsFailure()); |
| 223 | |
| 224 HEAP->TearDown(); | |
| 225 } | 266 } |
| OLD | NEW |