1 /* 2 * Copyright (C) 2013 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_ARENA_BIT_VECTOR_H_ 18 #define ART_LIBARTBASE_BASE_ARENA_BIT_VECTOR_H_ 19 20 #include <algorithm> 21 22 #include "arena_object.h" 23 #include "base/arena_allocator.h" 24 #include "bit_vector.h" 25 26 namespace art { 27 28 class ScopedArenaAllocator; 29 30 /* 31 * A BitVector implementation that uses Arena allocation. All constructors of ArenaBitVector start 32 * with an empty ArenaBitVector. 33 */ 34 class ArenaBitVector : public BitVector, public ArenaObject<kArenaAllocGrowableBitMap> { 35 public: 36 template <typename Allocator> 37 static ArenaBitVector* Create(Allocator* allocator, 38 uint32_t start_bits, 39 bool expandable, 40 ArenaAllocKind kind = kArenaAllocGrowableBitMap) { 41 void* storage = allocator->template Alloc<ArenaBitVector>(kind); 42 return new (storage) ArenaBitVector(allocator, start_bits, expandable, kind); 43 } 44 45 ArenaBitVector(ArenaAllocator* allocator, 46 uint32_t start_bits, 47 bool expandable, 48 ArenaAllocKind kind = kArenaAllocGrowableBitMap); 49 ArenaBitVector(ScopedArenaAllocator* allocator, 50 uint32_t start_bits, 51 bool expandable, 52 ArenaAllocKind kind = kArenaAllocGrowableBitMap); ~ArenaBitVector()53 ~ArenaBitVector() {} 54 55 ArenaBitVector(ArenaBitVector&&) = default; 56 ArenaBitVector(const ArenaBitVector&) = delete; 57 58 template <typename StorageType = size_t, typename Allocator> 59 static BitVectorView<StorageType> CreateFixedSize( 60 Allocator* allocator, size_t bits, ArenaAllocKind kind = kArenaAllocGrowableBitMap) { 61 static_assert(std::is_same_v<Allocator, ArenaAllocator> || 62 std::is_same_v<Allocator, ScopedArenaAllocator>); 63 size_t num_elements = BitVectorView<StorageType>::BitsToWords(bits); 64 StorageType* storage = allocator->template AllocArray<StorageType>(num_elements, kind); 65 BitVectorView<StorageType> result(storage, bits); 66 if (std::is_same_v<Allocator, ScopedArenaAllocator>) { 67 result.ClearAllBits(); 68 } else { 69 DCHECK(!result.IsAnyBitSet()); 70 } 71 return result; 72 } 73 }; 74 75 } // namespace art 76 77 #endif // ART_LIBARTBASE_BASE_ARENA_BIT_VECTOR_H_ 78