Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <stdlib.h>
6 :
7 : #include "src/globals.h"
8 : #include "src/heap/marking.h"
9 : #include "testing/gtest/include/gtest/gtest.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 :
15 15128 : TEST(Marking, TransitionWhiteBlackWhite) {
16 : Bitmap* bitmap = reinterpret_cast<Bitmap*>(
17 1 : calloc(Bitmap::kSize / kTaggedSize, kTaggedSize));
18 : const int kLocationsSize = 3;
19 : int position[kLocationsSize] = {
20 1 : Bitmap::kBitsPerCell - 2, Bitmap::kBitsPerCell - 1, Bitmap::kBitsPerCell};
21 4 : for (int i = 0; i < kLocationsSize; i++) {
22 3 : MarkBit mark_bit = bitmap->MarkBitFromIndex(position[i]);
23 3 : CHECK(Marking::IsWhite(mark_bit));
24 3 : CHECK(!Marking::IsImpossible(mark_bit));
25 : Marking::WhiteToBlack<AccessMode::NON_ATOMIC>(mark_bit);
26 3 : CHECK(Marking::IsBlack(mark_bit));
27 3 : CHECK(!Marking::IsImpossible(mark_bit));
28 : Marking::MarkWhite(mark_bit);
29 3 : CHECK(Marking::IsWhite(mark_bit));
30 3 : CHECK(!Marking::IsImpossible(mark_bit));
31 : }
32 1 : free(bitmap);
33 1 : }
34 :
35 15128 : TEST(Marking, TransitionWhiteGreyBlack) {
36 : Bitmap* bitmap = reinterpret_cast<Bitmap*>(
37 1 : calloc(Bitmap::kSize / kTaggedSize, kTaggedSize));
38 : const int kLocationsSize = 3;
39 : int position[kLocationsSize] = {
40 1 : Bitmap::kBitsPerCell - 2, Bitmap::kBitsPerCell - 1, Bitmap::kBitsPerCell};
41 4 : for (int i = 0; i < kLocationsSize; i++) {
42 3 : MarkBit mark_bit = bitmap->MarkBitFromIndex(position[i]);
43 3 : CHECK(Marking::IsWhite(mark_bit));
44 3 : CHECK(!Marking::IsBlackOrGrey(mark_bit));
45 3 : CHECK(!Marking::IsImpossible(mark_bit));
46 : Marking::WhiteToGrey<AccessMode::NON_ATOMIC>(mark_bit);
47 3 : CHECK(Marking::IsGrey(mark_bit));
48 3 : CHECK(Marking::IsBlackOrGrey(mark_bit));
49 3 : CHECK(!Marking::IsImpossible(mark_bit));
50 : Marking::GreyToBlack<AccessMode::NON_ATOMIC>(mark_bit);
51 3 : CHECK(Marking::IsBlack(mark_bit));
52 3 : CHECK(Marking::IsBlackOrGrey(mark_bit));
53 3 : CHECK(!Marking::IsImpossible(mark_bit));
54 : Marking::MarkWhite(mark_bit);
55 3 : CHECK(Marking::IsWhite(mark_bit));
56 3 : CHECK(!Marking::IsImpossible(mark_bit));
57 : }
58 1 : free(bitmap);
59 1 : }
60 :
61 15128 : TEST(Marking, SetAndClearRange) {
62 : Bitmap* bitmap = reinterpret_cast<Bitmap*>(
63 1 : calloc(Bitmap::kSize / kTaggedSize, kTaggedSize));
64 4 : for (int i = 0; i < 3; i++) {
65 3 : bitmap->SetRange(i, Bitmap::kBitsPerCell + i);
66 3 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0xFFFFFFFFu << i);
67 3 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], (1u << i) - 1);
68 3 : bitmap->ClearRange(i, Bitmap::kBitsPerCell + i);
69 3 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0x0u);
70 3 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], 0x0u);
71 : }
72 1 : free(bitmap);
73 1 : }
74 :
75 15128 : TEST(Marking, ClearMultipleRanges) {
76 : Bitmap* bitmap = reinterpret_cast<Bitmap*>(
77 1 : calloc(Bitmap::kSize / kTaggedSize, kTaggedSize));
78 1 : CHECK(bitmap->AllBitsClearInRange(0, Bitmap::kBitsPerCell * 3));
79 1 : bitmap->SetRange(0, Bitmap::kBitsPerCell * 3);
80 1 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0xFFFFFFFFu);
81 1 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], 0xFFFFFFFFu);
82 1 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[2], 0xFFFFFFFFu);
83 1 : CHECK(bitmap->AllBitsSetInRange(0, Bitmap::kBitsPerCell * 3));
84 1 : bitmap->ClearRange(Bitmap::kBitsPerCell / 2, Bitmap::kBitsPerCell);
85 : bitmap->ClearRange(Bitmap::kBitsPerCell,
86 1 : Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2);
87 : bitmap->ClearRange(Bitmap::kBitsPerCell * 2 + 8,
88 1 : Bitmap::kBitsPerCell * 2 + 16);
89 1 : bitmap->ClearRange(Bitmap::kBitsPerCell * 2 + 24, Bitmap::kBitsPerCell * 3);
90 1 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0xFFFFu);
91 1 : CHECK(bitmap->AllBitsSetInRange(0, Bitmap::kBitsPerCell / 2));
92 1 : CHECK(bitmap->AllBitsClearInRange(Bitmap::kBitsPerCell / 2,
93 : Bitmap::kBitsPerCell));
94 1 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], 0xFFFF0000u);
95 1 : CHECK(
96 : bitmap->AllBitsSetInRange(Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2,
97 : 2 * Bitmap::kBitsPerCell));
98 1 : CHECK(bitmap->AllBitsClearInRange(
99 : Bitmap::kBitsPerCell, Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2));
100 1 : CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[2], 0xFF00FFu);
101 1 : CHECK(bitmap->AllBitsSetInRange(2 * Bitmap::kBitsPerCell,
102 : 2 * Bitmap::kBitsPerCell + 8));
103 1 : CHECK(bitmap->AllBitsClearInRange(2 * Bitmap::kBitsPerCell + 24,
104 : Bitmap::kBitsPerCell * 3));
105 1 : free(bitmap);
106 1 : }
107 : } // namespace internal
108 9075 : } // namespace v8
|