Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 20 | #include "globals.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "space_bitmap-inl.h" |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 22 | #include "UniquePtr.h" |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | namespace gc { |
| 28 | namespace accounting { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 29 | |
| 30 | class SpaceBitmapTest : public CommonTest { |
| 31 | public: |
| 32 | }; |
| 33 | |
| 34 | TEST_F(SpaceBitmapTest, Init) { |
| 35 | byte* heap_begin = reinterpret_cast<byte*>(0x10000000); |
| 36 | size_t heap_capacity = 16 * MB; |
Ian Rogers | a40307e | 2013-02-22 11:32:44 -0800 | [diff] [blame] | 37 | UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap", |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 38 | heap_begin, heap_capacity)); |
| 39 | EXPECT_TRUE(space_bitmap.get() != NULL); |
| 40 | } |
| 41 | |
| 42 | class BitmapVerify { |
| 43 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin, const mirror::Object* end) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 45 | : bitmap_(bitmap), |
| 46 | begin_(begin), |
| 47 | end_(end) {} |
| 48 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 49 | void operator()(const mirror::Object* obj) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 50 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | const mirror::Object* begin_; |
| 57 | const mirror::Object* end_; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | TEST_F(SpaceBitmapTest, ScanRange) { |
| 61 | byte* heap_begin = reinterpret_cast<byte*>(0x10000000); |
| 62 | size_t heap_capacity = 16 * MB; |
| 63 | |
Ian Rogers | a40307e | 2013-02-22 11:32:44 -0800 | [diff] [blame] | 64 | UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap", |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 65 | 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 Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame^] | 69 | for (size_t j = 0; j < kBitsPerWord * 3; ++j) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | const mirror::Object* obj = |
| 71 | reinterpret_cast<mirror::Object*>(heap_begin + j * SpaceBitmap::kAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 72 | 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | mirror::Object* start = |
| 82 | reinterpret_cast<mirror::Object*>(heap_begin + i * SpaceBitmap::kAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 83 | for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 84 | mirror::Object* end = |
| 85 | reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 86 | BitmapVerify(space_bitmap.get(), start, end); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 91 | } // namespace accounting |
| 92 | } // namespace gc |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 93 | } // namespace art |