blob: 90a198193e80cde11550476f1609f04c60fc845c [file] [log] [blame]
Mathieu Chartier12f1b992017-01-19 18:00:45 -08001/*
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
22namespace 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.
26class BitMemoryRegion FINAL : public ValueObject {
27 public:
28 BitMemoryRegion() = default;
29 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
36 void* pointer() const { return region_.pointer(); }
37 size_t size() const { return region_.size(); }
38 size_t BitOffset() const { return bit_start_; }
39 size_t size_in_bits() const {
40 return region_.size_in_bits();
41 }
42
43 // Load a single bit in the region. The bit at offset 0 is the least
44 // significant bit in the first byte.
45 ALWAYS_INLINE bool LoadBit(uintptr_t bit_offset) const {
46 return region_.LoadBit(bit_offset + bit_start_);
47 }
48
49 ALWAYS_INLINE void StoreBit(uintptr_t bit_offset, bool value) const {
50 region_.StoreBit(bit_offset + bit_start_, value);
51 }
52
53 ALWAYS_INLINE uint32_t LoadBits(uintptr_t bit_offset, size_t length) const {
54 return region_.LoadBits(bit_offset + bit_start_, length);
55 }
56
57 // Store at a bit offset from inside the bit memory region.
58 ALWAYS_INLINE void StoreBits(uintptr_t bit_offset, uint32_t value, size_t length) {
59 region_.StoreBits(bit_offset + bit_start_, value, length);
60 }
61
62 private:
63 MemoryRegion region_;
64 size_t bit_start_ = 0;
65};
66
67} // namespace art
68
69#endif // ART_RUNTIME_BIT_MEMORY_REGION_H_