2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
2010-05-27 12:30:45 +00:00
|
|
|
static void VerifyRegionMarking(Address page_start) {
|
2008-08-22 13:33:59 +00:00
|
|
|
Page* p = Page::FromAddress(page_start);
|
|
|
|
|
2010-05-27 12:30:45 +00:00
|
|
|
p->SetRegionMarks(Page::kAllRegionsCleanMarks);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
for (Address addr = p->ObjectAreaStart();
|
|
|
|
addr < p->ObjectAreaEnd();
|
|
|
|
addr += kPointerSize) {
|
2010-05-27 12:30:45 +00:00
|
|
|
CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Address addr = p->ObjectAreaStart();
|
|
|
|
addr < p->ObjectAreaEnd();
|
|
|
|
addr += kPointerSize) {
|
2010-05-27 12:30:45 +00:00
|
|
|
Page::FromAddress(addr)->MarkRegionDirty(addr);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Address addr = p->ObjectAreaStart();
|
|
|
|
addr < p->ObjectAreaEnd();
|
|
|
|
addr += kPointerSize) {
|
2010-05-27 12:30:45 +00:00
|
|
|
CHECK(Page::FromAddress(addr)->IsRegionDirty(addr));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Page) {
|
|
|
|
byte* mem = NewArray<byte>(2*Page::kPageSize);
|
|
|
|
CHECK(mem != NULL);
|
|
|
|
|
|
|
|
Address start = reinterpret_cast<Address>(mem);
|
|
|
|
Address page_start = RoundUp(start, Page::kPageSize);
|
|
|
|
|
|
|
|
Page* p = Page::FromAddress(page_start);
|
2011-03-18 20:35:07 +00:00
|
|
|
// Initialized Page has heap pointer, normally set by memory_allocator.
|
|
|
|
p->heap_ = HEAP;
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(p->address() == page_start);
|
|
|
|
CHECK(p->is_valid());
|
|
|
|
|
|
|
|
p->opaque_header = 0;
|
2010-04-22 14:42:21 +00:00
|
|
|
p->SetIsLargeObjectPage(false);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(!p->next_page()->is_valid());
|
|
|
|
|
|
|
|
CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset);
|
|
|
|
CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize);
|
|
|
|
|
|
|
|
CHECK(p->Offset(page_start + Page::kObjectStartOffset) ==
|
|
|
|
Page::kObjectStartOffset);
|
|
|
|
CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize);
|
|
|
|
|
|
|
|
CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart());
|
|
|
|
CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd());
|
|
|
|
|
2010-05-27 12:30:45 +00:00
|
|
|
// test region marking
|
|
|
|
VerifyRegionMarking(page_start);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
DeleteArray(mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-04 15:18:18 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// Temporarily sets a given allocator in an isolate.
|
|
|
|
class TestMemoryAllocatorScope {
|
|
|
|
public:
|
|
|
|
TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator)
|
|
|
|
: isolate_(isolate),
|
|
|
|
old_allocator_(isolate->memory_allocator_) {
|
|
|
|
isolate->memory_allocator_ = allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestMemoryAllocatorScope() {
|
|
|
|
isolate_->memory_allocator_ = old_allocator_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Isolate* isolate_;
|
|
|
|
MemoryAllocator* old_allocator_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope);
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
TEST(MemoryAllocator) {
|
2011-03-18 20:35:07 +00:00
|
|
|
OS::Setup();
|
|
|
|
Isolate* isolate = Isolate::Current();
|
2011-08-04 15:18:18 +00:00
|
|
|
isolate->InitializeLoggingAndCounters();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
CHECK(heap->ConfigureHeapDefault());
|
|
|
|
MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
|
|
|
|
CHECK(memory_allocator->Setup(heap->MaxReserved(),
|
|
|
|
heap->MaxExecutableSize()));
|
|
|
|
TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
|
|
|
|
|
|
|
|
OldSpace faked_space(heap,
|
|
|
|
heap->MaxReserved(),
|
2011-03-18 20:35:07 +00:00
|
|
|
OLD_POINTER_SPACE,
|
|
|
|
NOT_EXECUTABLE);
|
2008-08-22 13:33:59 +00:00
|
|
|
int total_pages = 0;
|
2010-12-07 11:31:57 +00:00
|
|
|
int requested = MemoryAllocator::kPagesPerChunk;
|
2008-08-22 13:33:59 +00:00
|
|
|
int allocated;
|
2010-12-07 11:31:57 +00:00
|
|
|
// If we request n pages, we should get n or n - 1.
|
2011-08-04 15:18:18 +00:00
|
|
|
Page* first_page = memory_allocator->AllocatePages(
|
|
|
|
requested, &allocated, &faked_space);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(first_page->is_valid());
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(allocated == requested || allocated == requested - 1);
|
2008-08-22 13:33:59 +00:00
|
|
|
total_pages += allocated;
|
|
|
|
|
|
|
|
Page* last_page = first_page;
|
|
|
|
for (Page* p = first_page; p->is_valid(); p = p->next_page()) {
|
2011-08-04 15:18:18 +00:00
|
|
|
CHECK(memory_allocator->IsPageInSpace(p, &faked_space));
|
2008-08-22 13:33:59 +00:00
|
|
|
last_page = p;
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Again, we should get n or n - 1 pages.
|
2011-08-04 15:18:18 +00:00
|
|
|
Page* others = memory_allocator->AllocatePages(
|
|
|
|
requested, &allocated, &faked_space);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(others->is_valid());
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(allocated == requested || allocated == requested - 1);
|
2008-08-22 13:33:59 +00:00
|
|
|
total_pages += allocated;
|
|
|
|
|
2011-08-04 15:18:18 +00:00
|
|
|
memory_allocator->SetNextPage(last_page, others);
|
2008-08-22 13:33:59 +00:00
|
|
|
int page_count = 0;
|
|
|
|
for (Page* p = first_page; p->is_valid(); p = p->next_page()) {
|
2011-08-04 15:18:18 +00:00
|
|
|
CHECK(memory_allocator->IsPageInSpace(p, &faked_space));
|
2008-08-22 13:33:59 +00:00
|
|
|
page_count++;
|
|
|
|
}
|
|
|
|
CHECK(total_pages == page_count);
|
|
|
|
|
|
|
|
Page* second_page = first_page->next_page();
|
|
|
|
CHECK(second_page->is_valid());
|
|
|
|
|
|
|
|
// Freeing pages at the first chunk starting at or after the second page
|
2010-12-07 11:31:57 +00:00
|
|
|
// should free the entire second chunk. It will return the page it was passed
|
|
|
|
// (since the second page was in the first chunk).
|
2011-08-04 15:18:18 +00:00
|
|
|
Page* free_return = memory_allocator->FreePages(second_page);
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(free_return == second_page);
|
2011-08-04 15:18:18 +00:00
|
|
|
memory_allocator->SetNextPage(first_page, free_return);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
// Freeing pages in the first chunk starting at the first page should free
|
|
|
|
// the first chunk and return an invalid page.
|
2011-08-04 15:18:18 +00:00
|
|
|
Page* invalid_page = memory_allocator->FreePages(first_page);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(!invalid_page->is_valid());
|
|
|
|
|
2011-08-04 15:18:18 +00:00
|
|
|
memory_allocator->TearDown();
|
|
|
|
delete memory_allocator;
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NewSpace) {
|
2011-03-18 20:35:07 +00:00
|
|
|
OS::Setup();
|
2011-08-04 15:18:18 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
|
|
|
isolate->InitializeLoggingAndCounters();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
CHECK(heap->ConfigureHeapDefault());
|
|
|
|
MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
|
|
|
|
CHECK(memory_allocator->Setup(heap->MaxReserved(),
|
|
|
|
heap->MaxExecutableSize()));
|
|
|
|
TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-08-04 15:18:18 +00:00
|
|
|
NewSpace new_space(heap);
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
void* chunk =
|
2011-08-04 15:18:18 +00:00
|
|
|
memory_allocator->ReserveInitialChunk(4 * heap->ReservedSemiSpaceSize());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(chunk != NULL);
|
|
|
|
Address start = RoundUp(static_cast<Address>(chunk),
|
2011-08-04 15:18:18 +00:00
|
|
|
2 * heap->ReservedSemiSpaceSize());
|
|
|
|
CHECK(new_space.Setup(start, 2 * heap->ReservedSemiSpaceSize()));
|
2008-10-17 09:13:27 +00:00
|
|
|
CHECK(new_space.HasBeenSetup());
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2008-10-17 09:13:27 +00:00
|
|
|
while (new_space.Available() >= Page::kMaxHeapObjectSize) {
|
2010-10-25 15:22:03 +00:00
|
|
|
Object* obj =
|
|
|
|
new_space.AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked();
|
2008-10-17 09:13:27 +00:00
|
|
|
CHECK(new_space.Contains(HeapObject::cast(obj)));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2008-10-17 09:13:27 +00:00
|
|
|
new_space.TearDown();
|
2011-08-04 15:18:18 +00:00
|
|
|
memory_allocator->TearDown();
|
|
|
|
delete memory_allocator;
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(OldSpace) {
|
2011-03-18 20:35:07 +00:00
|
|
|
OS::Setup();
|
2011-08-04 15:18:18 +00:00
|
|
|
Isolate* isolate = Isolate::Current();
|
|
|
|
isolate->InitializeLoggingAndCounters();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
CHECK(heap->ConfigureHeapDefault());
|
|
|
|
MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
|
|
|
|
CHECK(memory_allocator->Setup(heap->MaxReserved(),
|
|
|
|
heap->MaxExecutableSize()));
|
|
|
|
TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
|
|
|
|
|
|
|
|
OldSpace* s = new OldSpace(heap,
|
|
|
|
heap->MaxOldGenerationSize(),
|
2008-09-05 12:34:09 +00:00
|
|
|
OLD_POINTER_SPACE,
|
|
|
|
NOT_EXECUTABLE);
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(s != NULL);
|
|
|
|
|
2011-08-04 15:18:18 +00:00
|
|
|
void* chunk = memory_allocator->ReserveInitialChunk(
|
|
|
|
4 * heap->ReservedSemiSpaceSize());
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(chunk != NULL);
|
|
|
|
Address start = static_cast<Address>(chunk);
|
2011-08-04 15:18:18 +00:00
|
|
|
size_t size = RoundUp(start, 2 * heap->ReservedSemiSpaceSize()) - start;
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
CHECK(s->Setup(start, size));
|
|
|
|
|
|
|
|
while (s->Available() > 0) {
|
2010-10-25 15:22:03 +00:00
|
|
|
s->AllocateRaw(Page::kMaxHeapObjectSize)->ToObjectUnchecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->TearDown();
|
|
|
|
delete s;
|
2011-08-04 15:18:18 +00:00
|
|
|
memory_allocator->TearDown();
|
|
|
|
delete memory_allocator;
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(LargeObjectSpace) {
|
2011-08-04 15:18:18 +00:00
|
|
|
v8::V8::Initialize();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
LargeObjectSpace* lo = HEAP->lo_space();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(lo != NULL);
|
|
|
|
|
|
|
|
Map* faked_map = reinterpret_cast<Map*>(HeapObject::FromAddress(0));
|
|
|
|
int lo_size = Page::kPageSize;
|
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
Object* obj = lo->AllocateRaw(lo_size)->ToObjectUnchecked();
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK(obj->IsHeapObject());
|
|
|
|
|
|
|
|
HeapObject* ho = HeapObject::cast(obj);
|
|
|
|
ho->set_map(faked_map);
|
|
|
|
|
|
|
|
CHECK(lo->Contains(HeapObject::cast(obj)));
|
|
|
|
|
|
|
|
CHECK(lo->FindObject(ho->address()) == obj);
|
|
|
|
|
|
|
|
CHECK(lo->Contains(ho));
|
|
|
|
|
|
|
|
while (true) {
|
2010-09-30 07:22:53 +00:00
|
|
|
intptr_t available = lo->Available();
|
2010-10-25 15:22:03 +00:00
|
|
|
{ MaybeObject* maybe_obj = lo->AllocateRaw(lo_size);
|
|
|
|
if (!maybe_obj->ToObject(&obj)) break;
|
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
HeapObject::cast(obj)->set_map(faked_map);
|
|
|
|
CHECK(lo->Available() < available);
|
|
|
|
};
|
|
|
|
|
|
|
|
CHECK(!lo->IsEmpty());
|
|
|
|
|
2010-10-25 15:22:03 +00:00
|
|
|
CHECK(lo->AllocateRaw(lo_size)->IsFailure());
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|