blob: 5a829e4f664cc815c818da023b163ab00215c0df [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"
20#include "dlmalloc.h"
21#include "globals.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "space_bitmap-inl.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070023#include "UniquePtr.h"
24
25#include <stdint.h>
26
27namespace art {
28
29class SpaceBitmapTest : public CommonTest {
30 public:
31};
32
33TEST_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
41class BitmapVerify {
42 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin, const mirror::Object* end)
Mathieu Chartiercc236d72012-07-20 10:29:05 -070044 : bitmap_(bitmap),
45 begin_(begin),
46 end_(end) {}
47
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 void operator ()(const mirror::Object* obj) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -070049 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 Rogers2dd0e2c2013-01-24 12:42:14 -080055 const mirror::Object* begin_;
56 const mirror::Object* end_;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070057};
58
59TEST_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 Rogers2dd0e2c2013-01-24 12:42:14 -080069 const mirror::Object* obj =
70 reinterpret_cast<mirror::Object*>(heap_begin + j * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070071 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 Rogers2dd0e2c2013-01-24 12:42:14 -080080 mirror::Object* start =
81 reinterpret_cast<mirror::Object*>(heap_begin + i * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070082 for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 mirror::Object* end =
84 reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070085 BitmapVerify(space_bitmap.get(), start, end);
86 }
87 }
88}
89
90} // namespace art