1 /*
2  * Copyright (C) 2017 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 #ifndef ART_RUNTIME_BIT_MEMORY_REGION_H_
18 #define ART_RUNTIME_BIT_MEMORY_REGION_H_
19 
20 #include "memory_region.h"
21 
22 namespace art {
23 
24 // Bit memory region is a bit offset subregion of a normal memoryregion. This is useful for
25 // abstracting away the bit start offset to avoid needing passing as an argument everywhere.
26 class BitMemoryRegion FINAL : public ValueObject {
27  public:
28   BitMemoryRegion() = default;
BitMemoryRegion(MemoryRegion region,size_t bit_offset,size_t bit_size)29   ALWAYS_INLINE BitMemoryRegion(MemoryRegion region, size_t bit_offset, size_t bit_size) {
30     bit_start_ = bit_offset % kBitsPerByte;
31     const size_t start = bit_offset / kBitsPerByte;
32     const size_t end = (bit_offset + bit_size + kBitsPerByte - 1) / kBitsPerByte;
33     region_ = region.Subregion(start, end - start);
34   }
35 
pointer()36   void* pointer() const { return region_.pointer(); }
size()37   size_t size() const { return region_.size(); }
BitOffset()38   size_t BitOffset() const { return bit_start_; }
size_in_bits()39   size_t size_in_bits() const {
40     return region_.size_in_bits();
41   }
42 
Subregion(size_t bit_offset,size_t bit_size)43   ALWAYS_INLINE BitMemoryRegion Subregion(size_t bit_offset, size_t bit_size) const {
44     return BitMemoryRegion(region_, bit_start_ + bit_offset, bit_size);
45   }
46 
47   // Load a single bit in the region. The bit at offset 0 is the least
48   // significant bit in the first byte.
LoadBit(uintptr_t bit_offset)49   ALWAYS_INLINE bool LoadBit(uintptr_t bit_offset) const {
50     return region_.LoadBit(bit_offset + bit_start_);
51   }
52 
StoreBit(uintptr_t bit_offset,bool value)53   ALWAYS_INLINE void StoreBit(uintptr_t bit_offset, bool value) const {
54     region_.StoreBit(bit_offset + bit_start_, value);
55   }
56 
LoadBits(uintptr_t bit_offset,size_t length)57   ALWAYS_INLINE uint32_t LoadBits(uintptr_t bit_offset, size_t length) const {
58     return region_.LoadBits(bit_offset + bit_start_, length);
59   }
60 
61   // Store at a bit offset from inside the bit memory region.
StoreBits(uintptr_t bit_offset,uint32_t value,size_t length)62   ALWAYS_INLINE void StoreBits(uintptr_t bit_offset, uint32_t value, size_t length) {
63     region_.StoreBits(bit_offset + bit_start_, value, length);
64   }
65 
66  private:
67   MemoryRegion region_;
68   size_t bit_start_ = 0;
69 };
70 
71 }  // namespace art
72 
73 #endif  // ART_RUNTIME_BIT_MEMORY_REGION_H_
74