Index: test/cctest/test-heap.cc |
diff --git a/test/cctest/test-heap.cc b/test/cctest/test-heap.cc |
index a37da264d498e1ef438e36f1c98ce36da109cb2a..742902f6569b73aed7b0d281cd4f7f4dc4eef81b 100644 |
--- a/test/cctest/test-heap.cc |
+++ b/test/cctest/test-heap.cc |
@@ -1784,6 +1784,104 @@ TEST(TestSizeOfObjects) { |
} |
+TEST(TestAlignmentCalculations) { |
+ // Maximum fill amounts should be consistent. |
+ int maximum_double_misalignment = kDoubleSize - kPointerSize; |
+ int max_word_fill = Heap::GetMaximumFillToAlign(kWordAligned); |
+ CHECK_EQ(0, max_word_fill); |
+ int max_double_fill = Heap::GetMaximumFillToAlign(kDoubleAligned); |
+ CHECK_EQ(maximum_double_misalignment, max_double_fill); |
+ int max_double_unaligned_fill = Heap::GetMaximumFillToAlign(kDoubleUnaligned); |
+ CHECK_EQ(maximum_double_misalignment, max_double_unaligned_fill); |
+ |
+ Address base = reinterpret_cast<Address>(NULL); |
+ int fill = 0; |
+ |
+ // Word alignment never requires fill. |
+ fill = Heap::GetFillToAlign(base, kWordAligned); |
+ CHECK_EQ(0, fill); |
+ fill = Heap::GetFillToAlign(base + kPointerSize, kWordAligned); |
+ CHECK_EQ(0, fill); |
+ |
+ // No fill is required when address is double aligned. |
+ fill = Heap::GetFillToAlign(base, kDoubleAligned); |
+ CHECK_EQ(0, fill); |
+ // Fill is required if address is not double aligned. |
+ fill = Heap::GetFillToAlign(base + kPointerSize, kDoubleAligned); |
+ CHECK_EQ(maximum_double_misalignment, fill); |
+ // kDoubleUnaligned has the opposite fill amounts. |
+ fill = Heap::GetFillToAlign(base, kDoubleUnaligned); |
+ CHECK_EQ(maximum_double_misalignment, fill); |
+ fill = Heap::GetFillToAlign(base + kPointerSize, kDoubleUnaligned); |
+ CHECK_EQ(0, fill); |
+} |
+ |
+ |
+static HeapObject* AllocateAligned(int size, AllocationAlignment alignment) { |
+ Heap* heap = CcTest::heap(); |
+ AllocationResult allocation = |
+ heap->new_space()->AllocateRawAligned(size, alignment); |
+ HeapObject* obj = NULL; |
+ allocation.To(&obj); |
+ heap->CreateFillerObjectAt(obj->address(), size); |
+ return obj; |
+} |
+ |
+ |
+TEST(TestAlignedAllocation) { |
+ // Double misalignment is 4 on 32-bit platforms, 0 on 64-bit ones. |
+ const intptr_t double_misalignment = kDoubleSize - kPointerSize; |
+ if (double_misalignment) { |
+ // Allocate a pointer sized object that must be double aligned. |
+ Address* top_addr = CcTest::heap()->new_space()->allocation_top_address(); |
+ Address start = *top_addr; |
+ HeapObject* obj1 = AllocateAligned(kPointerSize, kDoubleAligned); |
+ CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment, 0)); |
+ // Allocate a second pointer sized object. These two allocations should |
+ // cause exactly one filler object to be created. |
Hannes Payer (out of office)
2015/05/27 14:12:45
Let's also check if the filler is there.
bbudge
2015/05/28 09:28:33
Done.
|
+ HeapObject* obj2 = AllocateAligned(kPointerSize, kDoubleAligned); |
+ CHECK(IsAddressAligned(obj2->address(), kDoubleAlignment, 0)); |
+ CHECK_EQ(2 * kPointerSize + double_misalignment, *top_addr - start); |
+ |
+ // Similarly for kDoubleUnaligned. |
+ start = *top_addr; |
+ obj1 = AllocateAligned(kPointerSize, kDoubleUnaligned); |
+ CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment, kPointerSize)); |
Hannes Payer (out of office)
2015/05/27 14:12:45
This address should not be double aligned.
bbudge
2015/05/28 09:28:33
address + offset is aligned
(confusing, I'm not a
|
+ obj2 = AllocateAligned(kPointerSize, kDoubleUnaligned); |
+ CHECK(IsAddressAligned(obj2->address(), kDoubleAlignment, kPointerSize)); |
+ CHECK_EQ(2 * kPointerSize + double_misalignment, *top_addr - start); |
+ } |
+} |
+ |
+ |
+static HeapObject* AllocateUnalignedAndFill(int size, int allocated, |
+ AllocationAlignment alignment) { |
+ Heap* heap = CcTest::heap(); |
+ AllocationResult allocation = |
+ heap->new_space()->AllocateRawUnaligned(allocated); |
+ HeapObject* obj = NULL; |
+ allocation.To(&obj); |
+ obj = heap->AlignWithFiller(obj, kPointerSize, allocated, kDoubleAligned); |
+ heap->CreateFillerObjectAt(obj->address(), size); |
+ return obj; |
+} |
+ |
+ |
+TEST(TestAlignedOverAllocation) { |
+ // Double misalignment is 4 on 32-bit platforms, 0 on 64-bit ones. |
+ const intptr_t double_misalignment = kDoubleSize - kPointerSize; |
+ if (double_misalignment) { |
+ Address* top_addr = CcTest::heap()->new_space()->allocation_top_address(); |
+ Address start = *top_addr; |
+ HeapObject* obj1 = AllocateUnalignedAndFill( |
Hannes Payer (out of office)
2015/05/27 14:12:45
Here, I would rather test the free-list allocation
bbudge
2015/05/28 09:28:33
Done.
|
+ kPointerSize, kPointerSize + double_misalignment, kDoubleAligned); |
+ CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment, 0)); |
+ CHECK((start == obj1->address()) || |
+ (start == obj1->address() + double_misalignment)); |
+ } |
+} |
+ |
+ |
TEST(TestSizeOfObjectsVsHeapIteratorPrecision) { |
CcTest::InitializeVM(); |
HeapIterator iterator(CcTest::heap()); |