1 /**
2 * Copyright (c) 2021-2022 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 panda::mem {
25
TEST_F(BitmapTest,ClearRange)26 TEST_F(BitmapTest, ClearRange)
27 {
28 auto heap_begin = HEAP_STARTING_ADDRESS;
29 constexpr size_t HEAP_CAPACITY = 16_MB;
30 auto bm_ptr =
31 std::make_unique<BitmapWordType[]>((HEAP_CAPACITY >> Bitmap::LOG_BITSPERWORD) / DEFAULT_ALIGNMENT_IN_BYTES);
32 MemBitmap<DEFAULT_ALIGNMENT_IN_BYTES> bm(ToVoidPtr(heap_begin), HEAP_CAPACITY, bm_ptr.get());
33
34 using mem_range = std::pair<object_pointer_type, object_pointer_type>;
35 constexpr mem_range FIRST_RANGE {0, 10_KB + DEFAULT_ALIGNMENT_IN_BYTES};
36 constexpr mem_range SECOND_RANGE {DEFAULT_ALIGNMENT_IN_BYTES, DEFAULT_ALIGNMENT_IN_BYTES};
37 constexpr mem_range THIRD_RANGE {DEFAULT_ALIGNMENT_IN_BYTES, 2 * DEFAULT_ALIGNMENT_IN_BYTES};
38 constexpr mem_range FOURTH_RANGE {DEFAULT_ALIGNMENT_IN_BYTES, 5 * DEFAULT_ALIGNMENT_IN_BYTES};
39 constexpr mem_range FIFTH_RANGE {1_KB + DEFAULT_ALIGNMENT_IN_BYTES, 2_KB + 5 * DEFAULT_ALIGNMENT_IN_BYTES};
40 constexpr mem_range SIXTH_RANGE {0, HEAP_CAPACITY};
41
42 std::vector<mem_range> ranges {FIRST_RANGE, SECOND_RANGE, THIRD_RANGE, FOURTH_RANGE, FIFTH_RANGE, SIXTH_RANGE};
43
44 for (const auto &range : ranges) {
45 bm.IterateOverChunks([&bm](void *mem) { bm.Set(mem); });
46 bm.ClearRange(ToVoidPtr(heap_begin + range.first), ToVoidPtr(heap_begin + range.second));
47
48 auto test_true_fn = [&bm](void *mem) { EXPECT_TRUE(bm.Test(mem)) << "address: " << mem << std::endl; };
49 auto test_false_fn = [&bm](void *mem) { EXPECT_FALSE(bm.Test(mem)) << "address: " << mem << std::endl; };
50 bm.IterateOverChunkInRange(ToVoidPtr(heap_begin), ToVoidPtr(heap_begin + range.first), test_true_fn);
51 bm.IterateOverChunkInRange(ToVoidPtr(heap_begin + range.first), ToVoidPtr(heap_begin + range.second),
52 test_false_fn);
53 // for SIXTH_RANGE, range.second is not in the heap, so we skip this test
54 if (range.second < bm.MemSizeInBytes()) {
55 bm.IterateOverChunkInRange(ToVoidPtr(heap_begin + range.second),
56 ToVoidPtr(heap_begin + bm.MemSizeInBytes()), test_true_fn);
57 }
58 }
59 }
60
61 } // namespace panda::mem
62