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_LIBARTBASE_BASE_BIT_MEMORY_REGION_H_
18 #define ART_LIBARTBASE_BASE_BIT_MEMORY_REGION_H_
19 
20 #include "memory_region.h"
21 
22 #include "bit_utils.h"
23 #include "memory_tool.h"
24 
25 #include <array>
26 #include <cstdint>
27 
28 namespace art {
29 
30 // Bit memory region is a bit offset subregion of a normal memoryregion. This is useful for
31 // abstracting away the bit start offset to avoid needing passing as an argument everywhere.
32 class BitMemoryRegion final : public ValueObject {
33  public:
34   // Ensure all loads are naturally-aligned by aligning down the region's data pointer according to
35   // the largest data type that will be loaded via LoadBits (as StackMap BitTable uses over 8
36   // varints in the header, this is uint64_t).
37   using MaxSingleLoadType = uint64_t;
38   static constexpr size_t kMaxSingleLoadBytes = sizeof(MaxSingleLoadType);
39 
40   BitMemoryRegion() = default;
BitMemoryRegion(uint8_t * data,ssize_t bit_start,size_t bit_size)41   ALWAYS_INLINE BitMemoryRegion(uint8_t* data, ssize_t bit_start, size_t bit_size) {
42     // Normalize the data pointer. Note that bit_start may be negative.
43     data_ = AlignDown(data + (bit_start >> kBitsPerByteLog2), kMaxSingleLoadBytes);
44     bit_start_ = bit_start + kBitsPerByte * (data - data_);
45     bit_size_ = bit_size;
46   }
BitMemoryRegion(MemoryRegion region)47   ALWAYS_INLINE explicit BitMemoryRegion(MemoryRegion region)
48     : BitMemoryRegion(region.begin(), /* bit_start */ 0, region.size_in_bits()) {
49   }
BitMemoryRegion(MemoryRegion region,size_t bit_offset,size_t bit_length)50   ALWAYS_INLINE BitMemoryRegion(MemoryRegion region, size_t bit_offset, size_t bit_length)
51     : BitMemoryRegion(region) {
52     *this = Subregion(bit_offset, bit_length);
53   }
54 
IsValid()55   ALWAYS_INLINE bool IsValid() const { return data_ != nullptr; }
56 
data()57   const uint8_t* data() const {
58     DCHECK_ALIGNED(bit_start_, kBitsPerByte);
59     return data_ + bit_start_ / kBitsPerByte;
60   }
61 
size_in_bits()62   size_t size_in_bits() const {
63     return bit_size_;
64   }
65 
Resize(size_t bit_size)66   void Resize(size_t bit_size) {
67     bit_size_ = bit_size;
68   }
69 
Subregion(size_t bit_offset,size_t bit_length)70   ALWAYS_INLINE BitMemoryRegion Subregion(size_t bit_offset, size_t bit_length) const {
71     DCHECK_LE(bit_offset, bit_size_);
72     DCHECK_LE(bit_length, bit_size_ - bit_offset);
73     BitMemoryRegion result = *this;
74     result.bit_start_ += bit_offset;
75     result.bit_size_ = bit_length;
76     return result;
77   }
78 
Subregion(size_t bit_offset)79   ALWAYS_INLINE BitMemoryRegion Subregion(size_t bit_offset) const {
80     DCHECK_LE(bit_offset, bit_size_);
81     BitMemoryRegion result = *this;
82     result.bit_start_ += bit_offset;
83     result.bit_size_ -= bit_offset;
84     return result;
85   }
86 
87   // Load a single bit in the region. The bit at offset 0 is the least
88   // significant bit in the first byte.
LoadBit(size_t bit_offset)89   ALWAYS_INLINE bool LoadBit(size_t bit_offset) const {
90     DCHECK_LT(bit_offset, bit_size_);
91     size_t index = (bit_start_ + bit_offset) / kBitsPerByte;
92     size_t shift = (bit_start_ + bit_offset) % kBitsPerByte;
93     return ((data_[index] >> shift) & 1) != 0;
94   }
95 
StoreBit(size_t bit_offset,bool value)96   ALWAYS_INLINE void StoreBit(size_t bit_offset, bool value) {
97     DCHECK_LT(bit_offset, bit_size_);
98     size_t index = (bit_start_ + bit_offset) / kBitsPerByte;
99     size_t shift = (bit_start_ + bit_offset) % kBitsPerByte;
100     data_[index] &= ~(1 << shift);  // Clear bit.
101     data_[index] |= (value ? 1 : 0) << shift;  // Set bit.
102     DCHECK_EQ(value, LoadBit(bit_offset));
103   }
104 
105   // Load `bit_length` bits from `data` starting at given `bit_offset`.
106   // The least significant bit is stored in the smallest memory offset.
107   template<typename Result = size_t>
108   ATTRIBUTE_NO_SANITIZE_ADDRESS  // We might touch extra bytes due to the alignment.
109   ATTRIBUTE_NO_SANITIZE_HWADDRESS  // The hwasan uses different attribute.
LoadBits(size_t bit_offset,size_t bit_length)110   ALWAYS_INLINE Result LoadBits(size_t bit_offset, size_t bit_length) const {
111     static_assert(std::is_integral_v<Result>, "Result must be integral");
112     static_assert(std::is_unsigned_v<Result>, "Result must be unsigned");
113     static_assert(sizeof(Result) <= kMaxSingleLoadBytes);
114     DCHECK(IsAligned<sizeof(Result)>(data_));
115     DCHECK_LE(bit_offset, bit_size_);
116     DCHECK_LE(bit_length, bit_size_ - bit_offset);
117     DCHECK_LE(bit_length, BitSizeOf<Result>());
118     if (bit_length == 0) {
119       return 0;
120     }
121     // Load naturally-aligned value which contains the least significant bit.
122     Result* data = reinterpret_cast<Result*>(data_);
123     size_t width = BitSizeOf<Result>();
124     size_t index = (bit_start_ + bit_offset) / width;
125     size_t shift = (bit_start_ + bit_offset) % width;
126     Result value = data[index] >> shift;
127     // Load extra value containing the most significant bit (it might be the same one).
128     // We can not just load the following value as that could potentially cause SIGSEGV.
129     Result extra = data[index + (shift + (bit_length - 1)) / width];
130     // Mask to clear unwanted bits (the 1s are needed to avoid avoid undefined shift).
131     Result clear = (std::numeric_limits<Result>::max() << 1) << (bit_length - 1);
132     // Prepend the extra value.  We add explicit '& (width - 1)' so that the shift is defined.
133     // It is a no-op for `shift != 0` and if `shift == 0` then `value == extra` because of
134     // bit_length <= width causing the `value` and `extra` to be read from the same location.
135     // The '& (width - 1)' is implied by the shift instruction on ARM and removed by compiler.
136     return (value | (extra << ((width - shift) & (width - 1)))) & ~clear;
137   }
138 
139   // Store `bit_length` bits in `data` starting at given `bit_offset`.
140   // The least significant bit is stored in the smallest memory offset.
StoreBits(size_t bit_offset,size_t value,size_t bit_length)141   ALWAYS_INLINE void StoreBits(size_t bit_offset, size_t value, size_t bit_length) {
142     DCHECK_LE(bit_offset, bit_size_);
143     DCHECK_LE(bit_length, bit_size_ - bit_offset);
144     DCHECK_LE(bit_length, BitSizeOf<size_t>());
145     DCHECK_LE(value, MaxInt<size_t>(bit_length));
146     if (bit_length == 0) {
147       return;
148     }
149     // Write data byte by byte to avoid races with other threads
150     // on bytes that do not overlap with this region.
151     size_t mask = std::numeric_limits<size_t>::max() >> (BitSizeOf<size_t>() - bit_length);
152     size_t index = (bit_start_ + bit_offset) / kBitsPerByte;
153     size_t shift = (bit_start_ + bit_offset) % kBitsPerByte;
154     data_[index] &= ~(mask << shift);  // Clear bits.
155     data_[index] |= (value << shift);  // Set bits.
156     size_t finished_bits = kBitsPerByte - shift;
157     for (int i = 1; finished_bits < bit_length; i++, finished_bits += kBitsPerByte) {
158       data_[index + i] &= ~(mask >> finished_bits);  // Clear bits.
159       data_[index + i] |= (value >> finished_bits);  // Set bits.
160     }
161     DCHECK_EQ(value, LoadBits(bit_offset, bit_length));
162   }
163 
164   // Copy bits from other bit region.
CopyBits(const BitMemoryRegion & src)165   ALWAYS_INLINE void CopyBits(const BitMemoryRegion& src) {
166     DCHECK_EQ(size_in_bits(), src.size_in_bits());
167     // Hopefully, the loads of the unused `value` shall be optimized away.
168     VisitChunks([this, &src](size_t offset, size_t num_bits, [[maybe_unused]] size_t value)
169                     ALWAYS_INLINE {
170                       StoreChunk(offset, src.LoadBits(offset, num_bits), num_bits);
171                       return true;
172                     });
173   }
174 
175   // And bits from other bit region.
AndBits(const BitMemoryRegion & src)176   ALWAYS_INLINE void AndBits(const BitMemoryRegion& src) {
177     DCHECK_EQ(size_in_bits(), src.size_in_bits());
178     VisitChunks([this, &src](size_t offset, size_t num_bits, size_t value) ALWAYS_INLINE {
179       StoreChunk(offset, value & src.LoadBits(offset, num_bits), num_bits);
180       return true;
181     });
182   }
183 
184   // Or bits from other bit region.
OrBits(const BitMemoryRegion & src)185   ALWAYS_INLINE void OrBits(const BitMemoryRegion& src) {
186     DCHECK_EQ(size_in_bits(), src.size_in_bits());
187     VisitChunks([this, &src](size_t offset, size_t num_bits, size_t value) ALWAYS_INLINE {
188       StoreChunk(offset, value | src.LoadBits(offset, num_bits), num_bits);
189       return true;
190     });
191   }
192 
193   // Xor bits from other bit region.
XorBits(const BitMemoryRegion & src)194   ALWAYS_INLINE void XorBits(const BitMemoryRegion& src) {
195     DCHECK_EQ(size_in_bits(), src.size_in_bits());
196     VisitChunks([this, &src](size_t offset, size_t num_bits, size_t value) ALWAYS_INLINE {
197       StoreChunk(offset, value ^ src.LoadBits(offset, num_bits), num_bits);
198       return true;
199     });
200   }
201 
202   // Count the number of set bits within this region.
PopCount()203   ALWAYS_INLINE size_t PopCount() const {
204     size_t result = 0u;
205     VisitChunks([&]([[maybe_unused]] size_t offset, [[maybe_unused]] size_t num_bits, size_t value)
206                     ALWAYS_INLINE {
207                       result += POPCOUNT(value);
208                       return true;
209                     });
210     return result;
211   }
212 
213   // Count the number of set bits within the given bit range.
PopCount(size_t bit_offset,size_t bit_length)214   ALWAYS_INLINE size_t PopCount(size_t bit_offset, size_t bit_length) const {
215     return Subregion(bit_offset, bit_length).PopCount();
216   }
217 
218   // Check if this region has all bits clear.
HasAllBitsClear()219   ALWAYS_INLINE bool HasAllBitsClear() const {
220     return VisitChunks(
221         []([[maybe_unused]] size_t offset, [[maybe_unused]] size_t num_bits, size_t value)
222             ALWAYS_INLINE { return value == 0u; });
223   }
224 
225   // Check if this region has any bit set.
HasSomeBitSet()226   ALWAYS_INLINE bool HasSomeBitSet() const {
227     return !HasAllBitsClear();
228   }
229 
230   // Check if there is any bit set within the given bit range.
HasSomeBitSet(size_t bit_offset,size_t bit_length)231   ALWAYS_INLINE bool HasSomeBitSet(size_t bit_offset, size_t bit_length) const {
232     return Subregion(bit_offset, bit_length).HasSomeBitSet();
233   }
234 
Compare(const BitMemoryRegion & lhs,const BitMemoryRegion & rhs)235   static int Compare(const BitMemoryRegion& lhs, const BitMemoryRegion& rhs) {
236     if (lhs.size_in_bits() != rhs.size_in_bits()) {
237       return (lhs.size_in_bits() < rhs.size_in_bits()) ? -1 : 1;
238     }
239     int result = 0;
240     bool equals = lhs.VisitChunks(
241         [&](size_t offset, size_t num_bits, size_t lhs_value) ALWAYS_INLINE {
242           size_t rhs_value = rhs.LoadBits(offset, num_bits);
243           if (lhs_value == rhs_value) {
244             return true;
245           }
246           // We have found a difference. To avoid the comparison being dependent on how the region
247           // is split into chunks, check the lowest bit that differs. (Android is little-endian.)
248           int bit = CTZ(lhs_value ^ rhs_value);
249           result = ((rhs_value >> bit) & 1u) != 0u ? 1 : -1;
250           return false;  // Stop iterating.
251         });
252     DCHECK_EQ(equals, result == 0);
253     return result;
254   }
255 
Equals(const BitMemoryRegion & lhs,const BitMemoryRegion & rhs)256   static bool Equals(const BitMemoryRegion& lhs, const BitMemoryRegion& rhs) {
257     if (lhs.size_in_bits() != rhs.size_in_bits()) {
258       return false;
259     }
260     return lhs.VisitChunks([&rhs](size_t offset, size_t num_bits, size_t lhs_value) ALWAYS_INLINE {
261       return lhs_value == rhs.LoadBits(offset, num_bits);
262     });
263   }
264 
265  private:
266   // Visit the region in aligned `size_t` chunks. The first and last chunk may have fewer bits.
267   //
268   // Returns `true` if the iteration visited all chunks successfully, i.e. none of the
269   // calls to `visitor(offset, num_bits, value)` returned `false`; otherwise `false`.
270   template <typename VisitorType>
271   ATTRIBUTE_NO_SANITIZE_ADDRESS  // We might touch extra bytes due to the alignment.
272   ATTRIBUTE_NO_SANITIZE_HWADDRESS  // The hwasan uses different attribute.
VisitChunks(VisitorType && visitor)273   ALWAYS_INLINE bool VisitChunks(VisitorType&& visitor) const {
274     constexpr size_t kChunkSize = BitSizeOf<size_t>();
275     size_t remaining_bits = bit_size_;
276     if (remaining_bits == 0) {
277       return true;
278     }
279     DCHECK(IsAligned<sizeof(size_t)>(data_));
280     const size_t* data = reinterpret_cast<const size_t*>(data_);
281     size_t offset = 0u;
282     size_t bit_start = bit_start_;
283     data += bit_start / kChunkSize;
284     if ((bit_start % kChunkSize) != 0u) {
285       size_t leading_bits = kChunkSize - (bit_start % kChunkSize);
286       size_t value = (*data) >> (bit_start % kChunkSize);
287       if (leading_bits > remaining_bits) {
288         leading_bits = remaining_bits;
289         value = value & ~(std::numeric_limits<size_t>::max() << remaining_bits);
290       }
291       if (!visitor(offset, leading_bits, value)) {
292         return false;
293       }
294       offset += leading_bits;
295       remaining_bits -= leading_bits;
296       ++data;
297     }
298     while (remaining_bits >= kChunkSize) {
299       size_t value = *data;
300       if (!visitor(offset, kChunkSize, value)) {
301         return false;
302       }
303       offset += kChunkSize;
304       remaining_bits -= kChunkSize;
305       ++data;
306     }
307     if (remaining_bits != 0u) {
308       size_t value = (*data) & ~(std::numeric_limits<size_t>::max() << remaining_bits);
309       if (!visitor(offset, remaining_bits, value)) {
310         return false;
311       }
312     }
313     return true;
314   }
315 
StoreChunk(size_t bit_offset,size_t value,size_t bit_length)316   ALWAYS_INLINE void StoreChunk(size_t bit_offset, size_t value, size_t bit_length) {
317     if (bit_length == BitSizeOf<size_t>()) {
318       DCHECK_ALIGNED(bit_start_ + bit_offset, BitSizeOf<size_t>());
319       uint8_t* data = data_ + (bit_start_ + bit_offset) / kBitsPerByte;
320       DCHECK_ALIGNED(data, sizeof(size_t));
321       reinterpret_cast<size_t*>(data)[0] = value;
322     } else {
323       StoreBits(bit_offset, value, bit_length);
324     }
325   }
326 
327   uint8_t* data_ = nullptr;  // The pointer is aligned down to kMaxSingleLoadBytes.
328   size_t bit_start_ = 0;
329   size_t bit_size_ = 0;
330 };
331 
332 // Minimum number of bits used for varint. A varint represents either a value stored "inline" or
333 // the number of bytes that are required to encode the value.
334 constexpr uint32_t kVarintBits = 4;
335 // Maximum value which is stored "inline". We use the rest of the values to encode the number of
336 // bytes required to encode the value when the value is greater than kVarintMax.
337 // We encode any value less than or equal to 11 inline. We use 12, 13, 14 and 15
338 // to represent that the value is encoded in 1, 2, 3 and 4 bytes respectively.
339 //
340 // For example if we want to encode 1, 15, 16, 7, 11, 256:
341 //
342 // Low numbers (1, 7, 11) are encoded inline. 15 and 12 are set with 12 to show
343 // we need to load one byte for each to have their real values (15 and 12), and
344 // 256 is set with 13 to show we need to load two bytes. This is done to
345 // compress the values in the bit array and keep the size down. Where the actual value
346 // is read from depends on the use case.
347 //
348 // Values greater than kVarintMax could be encoded as a separate list referred
349 // to as InterleavedVarints (see ReadInterleavedVarints / WriteInterleavedVarints).
350 // This is used when there are fixed number of fields like CodeInfo headers.
351 // In our example the interleaved encoding looks like below:
352 //
353 // Meaning: 1--- 15-- 12-- 7--- 11-- 256- 15------- 12------- 256----------------
354 // Bits:    0001 1100 1100 0111 1011 1101 0000 1111 0000 1100 0000 0001 0000 0000
355 //
356 // In other cases the value is recorded just following the size encoding. This is
357 // referred as consecutive encoding (See ReadVarint / WriteVarint). In our
358 // example the consecutively encoded varints looks like below:
359 //
360 // Meaning: 1--- 15-- 15------- 12-- 12------- 7--- 11-- 256- 256----------------
361 // Bits:    0001 1100 0000 1100 1100 0000 1100 0111 1011 1101 0000 0001 0000 0000
362 constexpr uint32_t kVarintMax = 11;
363 
364 class BitMemoryReader {
365  public:
366   BitMemoryReader(BitMemoryReader&&) = default;
BitMemoryReader(BitMemoryRegion data)367   explicit BitMemoryReader(BitMemoryRegion data)
368       : finished_region_(data.Subregion(0, 0) /* set the length to zero */ ) {
369   }
370   explicit BitMemoryReader(const uint8_t* data, ssize_t bit_offset = 0)
371       : finished_region_(const_cast<uint8_t*>(data), bit_offset, /* bit_length */ 0) {
372   }
373 
data()374   const uint8_t* data() const { return finished_region_.data(); }
375 
GetReadRegion()376   BitMemoryRegion GetReadRegion() const { return finished_region_; }
377 
NumberOfReadBits()378   size_t NumberOfReadBits() const { return finished_region_.size_in_bits(); }
379 
ReadRegion(size_t bit_length)380   ALWAYS_INLINE BitMemoryRegion ReadRegion(size_t bit_length) {
381     size_t bit_offset = finished_region_.size_in_bits();
382     finished_region_.Resize(bit_offset + bit_length);
383     return finished_region_.Subregion(bit_offset, bit_length);
384   }
385 
386   template<typename Result = size_t>
ReadBits(size_t bit_length)387   ALWAYS_INLINE Result ReadBits(size_t bit_length) {
388     return ReadRegion(bit_length).LoadBits<Result>(/* bit_offset */ 0, bit_length);
389   }
390 
ReadBit()391   ALWAYS_INLINE bool ReadBit() {
392     return ReadRegion(/* bit_length */ 1).LoadBit(/* bit_offset */ 0);
393   }
394 
395   // Read variable-length bit-packed integer.
396   // The first four bits determine the variable length of the encoded integer:
397   //   Values 0..11 represent the result as-is, with no further following bits.
398   //   Values 12..15 mean the result is in the next 8/16/24/32-bits respectively.
ReadVarint()399   ALWAYS_INLINE uint32_t ReadVarint() {
400     uint32_t x = ReadBits(kVarintBits);
401     return (x <= kVarintMax) ? x : ReadBits((x - kVarintMax) * kBitsPerByte);
402   }
403 
404   // Read N 'interleaved' varints (different to just reading consecutive varints).
405   // All small values are stored first and the large values are stored after them.
406   // This requires fewer bit-reads compared to indidually storing the varints.
407   template<size_t N>
ReadInterleavedVarints()408   ALWAYS_INLINE std::array<uint32_t, N> ReadInterleavedVarints() {
409     static_assert(N * kVarintBits <= BitMemoryRegion::kMaxSingleLoadBytes * kBitsPerByte,
410                   "N too big");
411     std::array<uint32_t, N> values;
412     // StackMap BitTable uses over 8 varints in the header, so we need uint64_t.
413     uint64_t data = ReadBits<uint64_t>(N * kVarintBits);
414     for (size_t i = 0; i < N; i++) {
415       values[i] = BitFieldExtract(data, i * kVarintBits, kVarintBits);
416     }
417     // Do the second part in its own loop as that seems to produce better code in clang.
418     for (size_t i = 0; i < N; i++) {
419       if (UNLIKELY(values[i] > kVarintMax)) {
420         values[i] = ReadBits((values[i] - kVarintMax) * kBitsPerByte);
421       }
422     }
423     return values;
424   }
425 
426  private:
427   // Represents all of the bits which were read so far. There is no upper bound.
428   // Therefore, by definition, the "cursor" is always at the end of the region.
429   BitMemoryRegion finished_region_;
430 
431   DISALLOW_COPY_AND_ASSIGN(BitMemoryReader);
432 };
433 
434 template<typename Vector>
435 class BitMemoryWriter {
436  public:
437   explicit BitMemoryWriter(Vector* out, size_t bit_offset = 0)
out_(out)438       : out_(out), bit_start_(bit_offset), bit_offset_(bit_offset) {
439     DCHECK_EQ(NumberOfWrittenBits(), 0u);
440   }
441 
Truncate(size_t bit_offset)442   void Truncate(size_t bit_offset) {
443     DCHECK_GE(bit_offset, bit_start_);
444     DCHECK_LE(bit_offset, bit_offset_);
445     bit_offset_ = bit_offset;
446     DCHECK_LE(BitsToBytesRoundUp(bit_offset), out_->size());
447     out_->resize(BitsToBytesRoundUp(bit_offset));  // Shrink.
448   }
449 
GetWrittenRegion()450   BitMemoryRegion GetWrittenRegion() const {
451     return BitMemoryRegion(out_->data(), bit_start_, bit_offset_ - bit_start_);
452   }
453 
data()454   const uint8_t* data() const { return out_->data(); }
455 
NumberOfWrittenBits()456   size_t NumberOfWrittenBits() const { return bit_offset_ - bit_start_; }
457 
Allocate(size_t bit_length)458   ALWAYS_INLINE BitMemoryRegion Allocate(size_t bit_length) {
459     out_->resize(BitsToBytesRoundUp(bit_offset_ + bit_length));
460     BitMemoryRegion region(out_->data(), bit_offset_, bit_length);
461     DCHECK_LE(bit_length, std::numeric_limits<size_t>::max() - bit_offset_) << "Overflow";
462     bit_offset_ += bit_length;
463     return region;
464   }
465 
WriteRegion(const BitMemoryRegion & region)466   ALWAYS_INLINE void WriteRegion(const BitMemoryRegion& region) {
467     Allocate(region.size_in_bits()).CopyBits(region);
468   }
469 
WriteBits(uint32_t value,size_t bit_length)470   ALWAYS_INLINE void WriteBits(uint32_t value, size_t bit_length) {
471     Allocate(bit_length).StoreBits(/* bit_offset */ 0, value, bit_length);
472   }
473 
WriteBit(bool value)474   ALWAYS_INLINE void WriteBit(bool value) {
475     Allocate(1).StoreBit(/* bit_offset */ 0, value);
476   }
477 
478   template<size_t N>
WriteInterleavedVarints(std::array<uint32_t,N> values)479   ALWAYS_INLINE void WriteInterleavedVarints(std::array<uint32_t, N> values) {
480     // Write small values (or the number of bytes needed for the large values).
481     for (uint32_t value : values) {
482       if (value > kVarintMax) {
483         WriteBits(kVarintMax + BitsToBytesRoundUp(MinimumBitsToStore(value)), kVarintBits);
484       } else {
485         WriteBits(value, kVarintBits);
486       }
487     }
488     // Write large values.
489     for (uint32_t value : values) {
490       if (value > kVarintMax) {
491         WriteBits(value, BitsToBytesRoundUp(MinimumBitsToStore(value)) * kBitsPerByte);
492       }
493     }
494   }
495 
WriteVarint(uint32_t value)496   ALWAYS_INLINE void WriteVarint(uint32_t value) {
497     WriteInterleavedVarints<1>({value});
498   }
499 
WriteBytesAligned(const uint8_t * bytes,size_t length)500   void WriteBytesAligned(const uint8_t* bytes, size_t length) {
501     DCHECK_ALIGNED(bit_start_, kBitsPerByte);
502     DCHECK_ALIGNED(bit_offset_, kBitsPerByte);
503     DCHECK_EQ(BitsToBytesRoundUp(bit_offset_), out_->size());
504     out_->insert(out_->end(), bytes, bytes + length);
505     bit_offset_ += length * kBitsPerByte;
506   }
507 
ByteAlign()508   ALWAYS_INLINE void ByteAlign() {
509     DCHECK_ALIGNED(bit_start_, kBitsPerByte);
510     bit_offset_ = RoundUp(bit_offset_, kBitsPerByte);
511   }
512 
513  private:
514   Vector* out_;
515   size_t bit_start_;
516   size_t bit_offset_;
517 
518   DISALLOW_COPY_AND_ASSIGN(BitMemoryWriter);
519 };
520 
521 }  // namespace art
522 
523 #endif  // ART_LIBARTBASE_BASE_BIT_MEMORY_REGION_H_
524