space_bitmap_test.cc revision cc236d74772dda5a4161d9bc5f497fd3d956eb87
1cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier/*
2cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * Copyright (C) 2012 The Android Open Source Project
3cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier *
4cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License");
5cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * you may not use this file except in compliance with the License.
6cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * You may obtain a copy of the License at
7cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier *
8cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier *      http://www.apache.org/licenses/LICENSE-2.0
9cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier *
10cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * Unless required by applicable law or agreed to in writing, software
11cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS,
12cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * See the License for the specific language governing permissions and
14cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier * limitations under the License.
15cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier */
16cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
17cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier#include "space_bitmap.h"
18cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
19cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier#include "common_test.h"
20cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier#include "dlmalloc.h"
21cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier#include "globals.h"
22cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier#include "UniquePtr.h"
23cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
24cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier#include <stdint.h>
25cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
26cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartiernamespace art {
27cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
28cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartierclass SpaceBitmapTest : public CommonTest {
29cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier public:
30cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier};
31cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
32cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu ChartierTEST_F(SpaceBitmapTest, Init) {
33cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
34cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  size_t heap_capacity = 16 * MB;
35cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test-bitmap",
36cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier                                                          heap_begin, heap_capacity));
37cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  EXPECT_TRUE(space_bitmap.get() != NULL);
38cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier}
39cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
40cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartierclass BitmapVerify {
41cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier public:
42cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  BitmapVerify(SpaceBitmap* bitmap, const Object* begin, const Object* end)
43cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    : bitmap_(bitmap),
44cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier      begin_(begin),
45cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier      end_(end) {}
46cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
47cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  void operator ()(const Object* obj) {
48cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    EXPECT_TRUE(obj >= begin_);
49cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    EXPECT_TRUE(obj <= end_);
50cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    EXPECT_TRUE(bitmap_->Test(obj) == ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0));
51cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  }
52cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
53cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  SpaceBitmap* bitmap_;
54cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  const Object* begin_;
55cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  const Object* end_;
56cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier};
57cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
58cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu ChartierTEST_F(SpaceBitmapTest, ScanRange) {
59cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
60cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  size_t heap_capacity = 16 * MB;
61cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
62cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test-bitmap",
63cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier                                                          heap_begin, heap_capacity));
64cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  EXPECT_TRUE(space_bitmap.get() != NULL);
65cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
66cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  // Set all the odd bits in the first BitsPerWord * 3 to one.
67cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  for (size_t j = 0;j < kBitsPerWord * 3; ++j) {
68cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    const Object* obj = reinterpret_cast<Object*>(heap_begin + j * SpaceBitmap::kAlignment);
69cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    if (reinterpret_cast<uintptr_t>(obj) & 0xF) {
70cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier      space_bitmap->Set(obj);
71cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    }
72cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  }
73cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  // Try every possible starting bit in the first word. Then for each starting bit, try each
74cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  // possible length up to a maximum of kBitsPerWord * 2 - 1 bits.
75cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  // This handles all the cases, having runs which start and end on the same word, and different
76cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  // words.
77cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  for (size_t i = 0; i < static_cast<size_t>(kBitsPerWord); ++i) {
78cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    Object* start = reinterpret_cast<Object*>(heap_begin + i * SpaceBitmap::kAlignment);
79cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) {
80cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier      Object* end = reinterpret_cast<Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment);
81cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier      BitmapVerify(space_bitmap.get(), start, end);
82cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier    }
83cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  }
84cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier}
85cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
86cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier}  // namespace art
87