Home | History | Annotate | Download | only in accounting
      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 "globals.h"
     21 #include "space_bitmap-inl.h"
     22 #include "UniquePtr.h"
     23 
     24 #include <stdint.h>
     25 
     26 namespace art {
     27 namespace gc {
     28 namespace accounting {
     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;
     37   UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap",
     38                                                           heap_begin, heap_capacity));
     39   EXPECT_TRUE(space_bitmap.get() != NULL);
     40 }
     41 
     42 class BitmapVerify {
     43  public:
     44   BitmapVerify(SpaceBitmap* bitmap, const mirror::Object* begin, const mirror::Object* end)
     45     : bitmap_(bitmap),
     46       begin_(begin),
     47       end_(end) {}
     48 
     49   void operator()(const mirror::Object* obj) {
     50     EXPECT_TRUE(obj >= begin_);
     51     EXPECT_TRUE(obj <= end_);
     52     EXPECT_EQ(bitmap_->Test(obj), ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0));
     53   }
     54 
     55   SpaceBitmap* bitmap_;
     56   const mirror::Object* begin_;
     57   const mirror::Object* end_;
     58 };
     59 
     60 TEST_F(SpaceBitmapTest, ScanRange) {
     61   byte* heap_begin = reinterpret_cast<byte*>(0x10000000);
     62   size_t heap_capacity = 16 * MB;
     63 
     64   UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test bitmap",
     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.
     69   for (size_t j = 0; j < kBitsPerWord * 3; ++j) {
     70     const mirror::Object* obj =
     71         reinterpret_cast<mirror::Object*>(heap_begin + j * SpaceBitmap::kAlignment);
     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) {
     81     mirror::Object* start =
     82         reinterpret_cast<mirror::Object*>(heap_begin + i * SpaceBitmap::kAlignment);
     83     for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) {
     84       mirror::Object* end =
     85           reinterpret_cast<mirror::Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment);
     86       BitmapVerify(space_bitmap.get(), start, end);
     87     }
     88   }
     89 }
     90 
     91 }  // namespace accounting
     92 }  // namespace gc
     93 }  // namespace art
     94