1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdlib>
17 #include <memory>
18 #include <vector>
19
20 #include "gtest/gtest.h"
21 #include "bitmap_test_base.h"
22 #include "runtime/mem/gc/bitmap.h"
23
24 namespace ark::mem {
25
TEST_F(BitmapTest,ClearRange)26 TEST_F(BitmapTest, ClearRange)
27 {
28 auto heapBegin = HEAP_STARTING_ADDRESS;
29 constexpr size_t HEAP_CAPACITY = 16_MB;
30 // NOLINTBEGIN(modernize-avoid-c-arrays)
31 auto bmPtr =
32 std::make_unique<BitmapWordType[]>((HEAP_CAPACITY >> Bitmap::LOG_BITSPERWORD) / DEFAULT_ALIGNMENT_IN_BYTES);
33 // NOLINTEND(modernize-avoid-c-arrays)
34 MemBitmap<DEFAULT_ALIGNMENT_IN_BYTES> bm(ToVoidPtr(heapBegin), HEAP_CAPACITY, bmPtr.get());
35
36 using MemRangeTest = std::pair<ObjectPointerType, ObjectPointerType>;
37 constexpr MemRangeTest FIRST_RANGE {0, 10_KB + DEFAULT_ALIGNMENT_IN_BYTES};
38 constexpr MemRangeTest SECOND_RANGE {DEFAULT_ALIGNMENT_IN_BYTES, DEFAULT_ALIGNMENT_IN_BYTES};
39 constexpr MemRangeTest THIRD_RANGE {DEFAULT_ALIGNMENT_IN_BYTES, 2 * DEFAULT_ALIGNMENT_IN_BYTES};
40 constexpr MemRangeTest FOURTH_RANGE {DEFAULT_ALIGNMENT_IN_BYTES, 5 * DEFAULT_ALIGNMENT_IN_BYTES};
41 constexpr MemRangeTest FIFTH_RANGE {1_KB + DEFAULT_ALIGNMENT_IN_BYTES, 2_KB + 5 * DEFAULT_ALIGNMENT_IN_BYTES};
42 constexpr MemRangeTest SIXTH_RANGE {0, HEAP_CAPACITY};
43
44 std::vector<MemRangeTest> ranges {FIRST_RANGE, SECOND_RANGE, THIRD_RANGE, FOURTH_RANGE, FIFTH_RANGE, SIXTH_RANGE};
45
46 for (const auto &range : ranges) {
47 bm.IterateOverChunks([&bm](void *mem) { bm.Set(mem); });
48 bm.ClearRange(ToVoidPtr(heapBegin + range.first), ToVoidPtr(heapBegin + range.second));
49
50 auto testTrueFn = [&bm](void *mem) { EXPECT_TRUE(bm.Test(mem)) << "address: " << mem << std::endl; };
51 auto testFalseFn = [&bm](void *mem) { EXPECT_FALSE(bm.Test(mem)) << "address: " << mem << std::endl; };
52 bm.IterateOverChunkInRange(ToVoidPtr(heapBegin), ToVoidPtr(heapBegin + range.first), testTrueFn);
53 bm.IterateOverChunkInRange(ToVoidPtr(heapBegin + range.first), ToVoidPtr(heapBegin + range.second),
54 testFalseFn);
55 // for SIXTH_RANGE, range.second is not in the heap, so we skip this test
56 if (range.second < bm.MemSizeInBytes()) {
57 bm.IterateOverChunkInRange(ToVoidPtr(heapBegin + range.second), ToVoidPtr(heapBegin + bm.MemSizeInBytes()),
58 testTrueFn);
59 }
60 }
61 }
62
63 } // namespace ark::mem
64