13ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// Copyright 2011 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include <stdlib.h>
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "v8.h"
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "cctest.h"
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockusing namespace v8::internal;
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
353ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#if 0
367f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdochstatic void VerifyRegionMarking(Address page_start) {
373ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#ifdef ENABLE_CARDMARKING_WRITE_BARRIER
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Page* p = Page::FromAddress(page_start);
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
407f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch  p->SetRegionMarks(Page::kAllRegionsCleanMarks);
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (Address addr = p->ObjectAreaStart();
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block       addr < p->ObjectAreaEnd();
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block       addr += kPointerSize) {
457f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch    CHECK(!Page::FromAddress(addr)->IsRegionDirty(addr));
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (Address addr = p->ObjectAreaStart();
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block       addr < p->ObjectAreaEnd();
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block       addr += kPointerSize) {
517f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch    Page::FromAddress(addr)->MarkRegionDirty(addr);
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (Address addr = p->ObjectAreaStart();
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block       addr < p->ObjectAreaEnd();
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block       addr += kPointerSize) {
577f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch    CHECK(Page::FromAddress(addr)->IsRegionDirty(addr));
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
593ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#endif
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
613ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#endif
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
643ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch// TODO(gc) you can no longer allocate pages like this. Details are hidden.
653ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#if 0
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(Page) {
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  byte* mem = NewArray<byte>(2*Page::kPageSize);
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(mem != NULL);
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Address start = reinterpret_cast<Address>(mem);
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Address page_start = RoundUp(start, Page::kPageSize);
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Page* p = Page::FromAddress(page_start);
7444f0eee88ff00398ff7f715fab053374d808c90dSteve Block  // Initialized Page has heap pointer, normally set by memory_allocator.
7544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  p->heap_ = HEAP;
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->address() == page_start);
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->is_valid());
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  p->opaque_header = 0;
806ded16be15dd865a9b21ea304d5273c8be299c87Steve Block  p->SetIsLargeObjectPage(false);
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!p->next_page()->is_valid());
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->ObjectAreaStart() == page_start + Page::kObjectStartOffset);
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->ObjectAreaEnd() == page_start + Page::kPageSize);
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->Offset(page_start + Page::kObjectStartOffset) ==
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block        Page::kObjectStartOffset);
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->Offset(page_start + Page::kPageSize) == Page::kPageSize);
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->OffsetToAddress(Page::kObjectStartOffset) == p->ObjectAreaStart());
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(p->OffsetToAddress(Page::kPageSize) == p->ObjectAreaEnd());
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
937f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch  // test region marking
947f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch  VerifyRegionMarking(page_start);
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  DeleteArray(mem);
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
983ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch#endif
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
10169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdochnamespace v8 {
10269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdochnamespace internal {
10369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
10469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch// Temporarily sets a given allocator in an isolate.
10569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdochclass TestMemoryAllocatorScope {
10669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch public:
10769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  TestMemoryAllocatorScope(Isolate* isolate, MemoryAllocator* allocator)
10869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch      : isolate_(isolate),
10969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch        old_allocator_(isolate->memory_allocator_) {
11069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch    isolate->memory_allocator_ = allocator;
11169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  }
11269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
11369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  ~TestMemoryAllocatorScope() {
11469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch    isolate_->memory_allocator_ = old_allocator_;
11569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  }
11669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
11769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch private:
11869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  Isolate* isolate_;
11969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  MemoryAllocator* old_allocator_;
12069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
12169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  DISALLOW_COPY_AND_ASSIGN(TestMemoryAllocatorScope);
12269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch};
12369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
12469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch} }  // namespace v8::internal
12569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
12669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(MemoryAllocator) {
1283ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  OS::SetUp();
12944f0eee88ff00398ff7f715fab053374d808c90dSteve Block  Isolate* isolate = Isolate::Current();
13069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  isolate->InitializeLoggingAndCounters();
13169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  Heap* heap = isolate->heap();
1323ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(isolate->heap()->ConfigureHeapDefault());
1333ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
13469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
1353ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(memory_allocator->SetUp(heap->MaxReserved(),
13669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                                heap->MaxExecutableSize()));
13769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
1383ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  int total_pages = 0;
13969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  OldSpace faked_space(heap,
14069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                       heap->MaxReserved(),
14144f0eee88ff00398ff7f715fab053374d808c90dSteve Block                       OLD_POINTER_SPACE,
14244f0eee88ff00398ff7f715fab053374d808c90dSteve Block                       NOT_EXECUTABLE);
1433ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Page* first_page =
1443ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      memory_allocator->AllocatePage(&faked_space, NOT_EXECUTABLE);
1453ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch
1463ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  first_page->InsertAfter(faked_space.anchor()->prev_page());
147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(first_page->is_valid());
1483ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(first_page->next_page() == faked_space.anchor());
1493ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  total_pages++;
150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
1513ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
1523ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    CHECK(p->owner() == &faked_space);
153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
155b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  // Again, we should get n or n - 1 pages.
1563ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Page* other =
1573ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch      memory_allocator->AllocatePage(&faked_space, NOT_EXECUTABLE);
1583ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(other->is_valid());
1593ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  total_pages++;
1603ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  other->InsertAfter(first_page);
161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int page_count = 0;
1623ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  for (Page* p = first_page; p != faked_space.anchor(); p = p->next_page()) {
1633ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    CHECK(p->owner() == &faked_space);
164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    page_count++;
165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(total_pages == page_count);
167a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
168a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  Page* second_page = first_page->next_page();
169a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(second_page->is_valid());
1703ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  memory_allocator->Free(first_page);
1713ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  memory_allocator->Free(second_page);
17269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  memory_allocator->TearDown();
17369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  delete memory_allocator;
174a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
175a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
176a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
177a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(NewSpace) {
1783ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  OS::SetUp();
17969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  Isolate* isolate = Isolate::Current();
18069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  isolate->InitializeLoggingAndCounters();
18169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  Heap* heap = isolate->heap();
18269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  CHECK(heap->ConfigureHeapDefault());
18369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
1843ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(memory_allocator->SetUp(heap->MaxReserved(),
18569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                                heap->MaxExecutableSize()));
18669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
18869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  NewSpace new_space(heap);
189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
1903ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(new_space.SetUp(HEAP->ReservedSemiSpaceSize(),
1913ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch                        HEAP->ReservedSemiSpaceSize()));
1923ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(new_space.HasBeenSetUp());
193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
1943ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  while (new_space.Available() >= Page::kMaxNonCodeHeapObjectSize) {
1955913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    Object* obj =
1963ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        new_space.AllocateRaw(Page::kMaxNonCodeHeapObjectSize)->
1973ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch        ToObjectUnchecked();
198a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CHECK(new_space.Contains(HeapObject::cast(obj)));
199a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
200a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
201a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  new_space.TearDown();
20269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  memory_allocator->TearDown();
20369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  delete memory_allocator;
204a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
206a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
207a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(OldSpace) {
2083ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  OS::SetUp();
20969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  Isolate* isolate = Isolate::Current();
21069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  isolate->InitializeLoggingAndCounters();
21169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  Heap* heap = isolate->heap();
21269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  CHECK(heap->ConfigureHeapDefault());
21369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  MemoryAllocator* memory_allocator = new MemoryAllocator(isolate);
2143ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(memory_allocator->SetUp(heap->MaxReserved(),
21569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                                heap->MaxExecutableSize()));
21669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  TestMemoryAllocatorScope test_scope(isolate, memory_allocator);
21769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
21869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  OldSpace* s = new OldSpace(heap,
21969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                             heap->MaxOldGenerationSize(),
220a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block                             OLD_POINTER_SPACE,
221a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block                             NOT_EXECUTABLE);
222a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(s != NULL);
223a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
2243ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(s->SetUp());
225a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
226a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (s->Available() > 0) {
2273ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    s->AllocateRaw(Page::kMaxNonCodeHeapObjectSize)->ToObjectUnchecked();
228a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
229a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
230a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  s->TearDown();
231a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  delete s;
23269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  memory_allocator->TearDown();
23369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  delete memory_allocator;
234a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
235a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
236a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
237a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockTEST(LargeObjectSpace) {
23869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  v8::V8::Initialize();
239a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
24044f0eee88ff00398ff7f715fab053374d808c90dSteve Block  LargeObjectSpace* lo = HEAP->lo_space();
241a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(lo != NULL);
242a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
243a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  int lo_size = Page::kPageSize;
244a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
2453ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  Object* obj = lo->AllocateRaw(lo_size, NOT_EXECUTABLE)->ToObjectUnchecked();
246a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(obj->IsHeapObject());
247a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
248a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  HeapObject* ho = HeapObject::cast(obj);
249a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
250a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(lo->Contains(HeapObject::cast(obj)));
251a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
252a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(lo->FindObject(ho->address()) == obj);
253a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
254a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(lo->Contains(ho));
255a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
256a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  while (true) {
257f87a203d89e1bbb6708282e0b64dbd13d59b723dBen Murdoch    intptr_t available = lo->Available();
2583ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch    { MaybeObject* maybe_obj = lo->AllocateRaw(lo_size, NOT_EXECUTABLE);
2595913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck      if (!maybe_obj->ToObject(&obj)) break;
2605913587db4c6bab03d97bfe44b06289fd6d7270dJohn Reck    }
261a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    CHECK(lo->Available() < available);
262a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
263a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
264a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  CHECK(!lo->IsEmpty());
265a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
2663ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdoch  CHECK(lo->AllocateRaw(lo_size, NOT_EXECUTABLE)->IsFailure());
267a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
268