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" |
| 20 | #include "dlmalloc.h" |
| 21 | #include "globals.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "space_bitmap-inl.h" |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 23 | #include "UniquePtr.h" |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | class SpaceBitmapTest : public CommonTest { |
| 30 | public: |
| 31 | }; |
| 32 | |
| 33 | TEST_F(SpaceBitmapTest, Init) { |
| 34 | byte* heap_begin = reinterpret_cast<byte*>(0x10000000); |
| 35 | size_t heap_capacity = 16 * MB; |
| 36 | UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test-bitmap", |
| 37 | heap_begin, heap_capacity)); |
| 38 | EXPECT_TRUE(space_bitmap.get() != NULL); |
| 39 | } |
| 40 | |
| 41 | class BitmapVerify { |
| 42 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin, const mirror::Object* end) |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 44 | : bitmap_(bitmap), |
| 45 | begin_(begin), |
| 46 | end_(end) {} |
| 47 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | void operator ()(const mirror::Object* obj) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 49 | EXPECT_TRUE(obj >= begin_); |
| 50 | EXPECT_TRUE(obj <= end_); |
| 51 | EXPECT_TRUE(bitmap_->Test(obj) == ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0)); |
| 52 | } |
| 53 | |
| 54 | SpaceBitmap* bitmap_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | const mirror::Object* begin_; |
| 56 | const mirror::Object* end_; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | TEST_F(SpaceBitmapTest, ScanRange) { |
| 60 | byte* heap_begin = reinterpret_cast<byte*>(0x10000000); |
| 61 | size_t heap_capacity = 16 * MB; |
| 62 | |
| 63 | UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test-bitmap", |
| 64 | heap_begin, heap_capacity)); |
| 65 | EXPECT_TRUE(space_bitmap.get() != NULL); |
| 66 | |
| 67 | // Set all the odd bits in the first BitsPerWord * 3 to one. |
| 68 | for (size_t j = 0;j < kBitsPerWord * 3; ++j) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | const mirror::Object* obj = |
| 70 | reinterpret_cast<mirror::Object*>(heap_begin + j * SpaceBitmap::kAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 71 | if (reinterpret_cast<uintptr_t>(obj) & 0xF) { |
| 72 | space_bitmap->Set(obj); |
| 73 | } |
| 74 | } |
| 75 | // Try every possible starting bit in the first word. Then for each starting bit, try each |
| 76 | // possible length up to a maximum of kBitsPerWord * 2 - 1 bits. |
| 77 | // This handles all the cases, having runs which start and end on the same word, and different |
| 78 | // words. |
| 79 | for (size_t i = 0; i < static_cast<size_t>(kBitsPerWord); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 80 | mirror::Object* start = |
| 81 | reinterpret_cast<mirror::Object*>(heap_begin + i * SpaceBitmap::kAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 82 | 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] | 83 | mirror::Object* end = |
| 84 | reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 85 | BitmapVerify(space_bitmap.get(), start, end); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } // namespace art |