blob: 806f301ec1fd51de4e41961e0ebd61f9cfb13109 [file] [log] [blame]
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "space_bitmap.h"
18
19#include "common_test.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070020#include "globals.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "space_bitmap-inl.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070022#include "UniquePtr.h"
23
24#include <stdint.h>
25
26namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace accounting {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070029
30class SpaceBitmapTest : public CommonTest {
31 public:
32};
33
34TEST_F(SpaceBitmapTest, Init) {
35 byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
36 size_t heap_capacity = 16 * MB;
Ian Rogersa40307e2013-02-22 11:32:44 -080037 UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap",
Mathieu Chartiercc236d72012-07-20 10:29:05 -070038 heap_begin, heap_capacity));
39 EXPECT_TRUE(space_bitmap.get() != NULL);
40}
41
42class BitmapVerify {
43 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin, const mirror::Object* end)
Mathieu Chartiercc236d72012-07-20 10:29:05 -070045 : bitmap_(bitmap),
46 begin_(begin),
47 end_(end) {}
48
Brian Carlstromdf629502013-07-17 22:39:56 -070049 void operator()(const mirror::Object* obj) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070050 EXPECT_TRUE(obj >= begin_);
51 EXPECT_TRUE(obj <= end_);
52 EXPECT_TRUE(bitmap_->Test(obj) == ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0));
53 }
54
55 SpaceBitmap* bitmap_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 const mirror::Object* begin_;
57 const mirror::Object* end_;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070058};
59
60TEST_F(SpaceBitmapTest, ScanRange) {
61 byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
62 size_t heap_capacity = 16 * MB;
63
Ian Rogersa40307e2013-02-22 11:32:44 -080064 UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap",
Mathieu Chartiercc236d72012-07-20 10:29:05 -070065 heap_begin, heap_capacity));
66 EXPECT_TRUE(space_bitmap.get() != NULL);
67
68 // Set all the odd bits in the first BitsPerWord * 3 to one.
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070069 for (size_t j = 0; j < kBitsPerWord * 3; ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 const mirror::Object* obj =
71 reinterpret_cast<mirror::Object*>(heap_begin + j * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070072 if (reinterpret_cast<uintptr_t>(obj) & 0xF) {
73 space_bitmap->Set(obj);
74 }
75 }
76 // Try every possible starting bit in the first word. Then for each starting bit, try each
77 // possible length up to a maximum of kBitsPerWord * 2 - 1 bits.
78 // This handles all the cases, having runs which start and end on the same word, and different
79 // words.
80 for (size_t i = 0; i < static_cast<size_t>(kBitsPerWord); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mirror::Object* start =
82 reinterpret_cast<mirror::Object*>(heap_begin + i * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070083 for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 mirror::Object* end =
85 reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070086 BitmapVerify(space_bitmap.get(), start, end);
87 }
88 }
89}
90
Ian Rogers1d54e732013-05-02 21:10:01 -070091} // namespace accounting
92} // namespace gc
Mathieu Chartiercc236d72012-07-20 10:29:05 -070093} // namespace art