diff options
Diffstat (limited to 'runtime/bit_memory_region.h')
| -rw-r--r-- | runtime/bit_memory_region.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/runtime/bit_memory_region.h b/runtime/bit_memory_region.h new file mode 100644 index 0000000000..90a198193e --- /dev/null +++ b/runtime/bit_memory_region.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ART_RUNTIME_BIT_MEMORY_REGION_H_ +#define ART_RUNTIME_BIT_MEMORY_REGION_H_ + +#include "memory_region.h" + +namespace art { + +// Bit memory region is a bit offset subregion of a normal memoryregion. This is useful for +// abstracting away the bit start offset to avoid needing passing as an argument everywhere. +class BitMemoryRegion FINAL : public ValueObject { + public: + BitMemoryRegion() = default; + BitMemoryRegion(MemoryRegion region, size_t bit_offset, size_t bit_size) { + bit_start_ = bit_offset % kBitsPerByte; + const size_t start = bit_offset / kBitsPerByte; + const size_t end = (bit_offset + bit_size + kBitsPerByte - 1) / kBitsPerByte; + region_ = region.Subregion(start, end - start); + } + + void* pointer() const { return region_.pointer(); } + size_t size() const { return region_.size(); } + size_t BitOffset() const { return bit_start_; } + size_t size_in_bits() const { + return region_.size_in_bits(); + } + + // Load a single bit in the region. The bit at offset 0 is the least + // significant bit in the first byte. + ALWAYS_INLINE bool LoadBit(uintptr_t bit_offset) const { + return region_.LoadBit(bit_offset + bit_start_); + } + + ALWAYS_INLINE void StoreBit(uintptr_t bit_offset, bool value) const { + region_.StoreBit(bit_offset + bit_start_, value); + } + + ALWAYS_INLINE uint32_t LoadBits(uintptr_t bit_offset, size_t length) const { + return region_.LoadBits(bit_offset + bit_start_, length); + } + + // Store at a bit offset from inside the bit memory region. + ALWAYS_INLINE void StoreBits(uintptr_t bit_offset, uint32_t value, size_t length) { + region_.StoreBits(bit_offset + bit_start_, value, length); + } + + private: + MemoryRegion region_; + size_t bit_start_ = 0; +}; + +} // namespace art + +#endif // ART_RUNTIME_BIT_MEMORY_REGION_H_ |