blob: ba4e2ac8f01754a821dd6619992958c2b4123012 [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080019#include <stdint.h>
20
21#include "common_runtime_test.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070022#include "globals.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "space_bitmap-inl.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070024#include "UniquePtr.h"
25
Mathieu Chartiercc236d72012-07-20 10:29:05 -070026namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace accounting {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070029
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class SpaceBitmapTest : public CommonRuntimeTest {};
Mathieu Chartiercc236d72012-07-20 10:29:05 -070031
32TEST_F(SpaceBitmapTest, Init) {
33 byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
34 size_t heap_capacity = 16 * MB;
Ian Rogersa40307e2013-02-22 11:32:44 -080035 UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap",
Mathieu Chartiercc236d72012-07-20 10:29:05 -070036 heap_begin, heap_capacity));
37 EXPECT_TRUE(space_bitmap.get() != NULL);
38}
39
40class BitmapVerify {
41 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin, const mirror::Object* end)
Mathieu Chartiercc236d72012-07-20 10:29:05 -070043 : bitmap_(bitmap),
44 begin_(begin),
45 end_(end) {}
46
Brian Carlstromdf629502013-07-17 22:39:56 -070047 void operator()(const mirror::Object* obj) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070048 EXPECT_TRUE(obj >= begin_);
49 EXPECT_TRUE(obj <= end_);
Brian Carlstrom42748892013-07-18 18:04:08 -070050 EXPECT_EQ(bitmap_->Test(obj), ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0));
Mathieu Chartiercc236d72012-07-20 10:29:05 -070051 }
52
53 SpaceBitmap* bitmap_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054 const mirror::Object* begin_;
55 const mirror::Object* end_;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070056};
57
58TEST_F(SpaceBitmapTest, ScanRange) {
59 byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
60 size_t heap_capacity = 16 * MB;
61
Ian Rogersa40307e2013-02-22 11:32:44 -080062 UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap",
Mathieu Chartiercc236d72012-07-20 10:29:05 -070063 heap_begin, heap_capacity));
64 EXPECT_TRUE(space_bitmap.get() != NULL);
65
66 // Set all the odd bits in the first BitsPerWord * 3 to one.
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070067 for (size_t j = 0; j < kBitsPerWord * 3; ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 const mirror::Object* obj =
69 reinterpret_cast<mirror::Object*>(heap_begin + j * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070070 if (reinterpret_cast<uintptr_t>(obj) & 0xF) {
71 space_bitmap->Set(obj);
72 }
73 }
74 // Try every possible starting bit in the first word. Then for each starting bit, try each
75 // possible length up to a maximum of kBitsPerWord * 2 - 1 bits.
76 // This handles all the cases, having runs which start and end on the same word, and different
77 // words.
78 for (size_t i = 0; i < static_cast<size_t>(kBitsPerWord); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 mirror::Object* start =
80 reinterpret_cast<mirror::Object*>(heap_begin + i * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070081 for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Object* end =
83 reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070084 BitmapVerify(space_bitmap.get(), start, end);
85 }
86 }
87}
88
Ian Rogers1d54e732013-05-02 21:10:01 -070089} // namespace accounting
90} // namespace gc
Mathieu Chartiercc236d72012-07-20 10:29:05 -070091} // namespace art