buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Nicolas Geoffray | 0e33643 | 2014-02-26 18:24:38 +0000 | [diff] [blame] | 17 | #include "arena_bit_vector.h" |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 18 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 19 | #include "base/allocator.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 20 | #include "base/arena_allocator.h" |
| 21 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 24 | template <bool kCount> |
| 25 | class ArenaBitVectorAllocatorKindImpl; |
| 26 | |
| 27 | template <> |
| 28 | class ArenaBitVectorAllocatorKindImpl<false> { |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 29 | public: |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 30 | // Not tracking allocations, ignore the supplied kind and arbitrarily provide kArenaAllocSTL. |
| 31 | explicit ArenaBitVectorAllocatorKindImpl(ArenaAllocKind kind ATTRIBUTE_UNUSED) {} |
| 32 | ArenaBitVectorAllocatorKindImpl(const ArenaBitVectorAllocatorKindImpl&) = default; |
| 33 | ArenaBitVectorAllocatorKindImpl& operator=(const ArenaBitVectorAllocatorKindImpl&) = default; |
| 34 | ArenaAllocKind Kind() { return kArenaAllocGrowableBitMap; } |
| 35 | }; |
| 36 | |
| 37 | template <bool kCount> |
| 38 | class ArenaBitVectorAllocatorKindImpl { |
| 39 | public: |
| 40 | explicit ArenaBitVectorAllocatorKindImpl(ArenaAllocKind kind) : kind_(kind) { } |
| 41 | ArenaBitVectorAllocatorKindImpl(const ArenaBitVectorAllocatorKindImpl&) = default; |
| 42 | ArenaBitVectorAllocatorKindImpl& operator=(const ArenaBitVectorAllocatorKindImpl&) = default; |
| 43 | ArenaAllocKind Kind() { return kind_; } |
| 44 | |
| 45 | private: |
| 46 | ArenaAllocKind kind_; |
| 47 | }; |
| 48 | |
| 49 | using ArenaBitVectorAllocatorKind = |
| 50 | ArenaBitVectorAllocatorKindImpl<kArenaAllocatorCountAllocations>; |
| 51 | |
| 52 | template <typename ArenaAlloc> |
| 53 | class ArenaBitVectorAllocator FINAL : public Allocator, private ArenaBitVectorAllocatorKind { |
| 54 | public: |
| 55 | static ArenaBitVectorAllocator* Create(ArenaAlloc* arena, ArenaAllocKind kind) { |
| 56 | void* storage = arena->template Alloc<ArenaBitVectorAllocator>(kind); |
| 57 | return new (storage) ArenaBitVectorAllocator(arena, kind); |
| 58 | } |
| 59 | |
| 60 | ~ArenaBitVectorAllocator() { |
| 61 | LOG(FATAL) << "UNREACHABLE"; |
| 62 | UNREACHABLE(); |
| 63 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 64 | |
| 65 | virtual void* Alloc(size_t size) { |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 66 | return arena_->Alloc(size, this->Kind()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | virtual void Free(void*) {} // Nop. |
| 70 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 71 | private: |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 72 | ArenaBitVectorAllocator(ArenaAlloc* arena, ArenaAllocKind kind) |
| 73 | : ArenaBitVectorAllocatorKind(kind), arena_(arena) { } |
| 74 | |
Ian Rogers | e77493c | 2014-08-20 15:08:45 -0700 | [diff] [blame] | 75 | ArenaAlloc* const arena_; |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 76 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(ArenaBitVectorAllocator); |
| 78 | }; |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 79 | |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 80 | ArenaBitVector::ArenaBitVector(ArenaAllocator* arena, |
| 81 | unsigned int start_bits, |
| 82 | bool expandable, |
| 83 | ArenaAllocKind kind) |
| 84 | : BitVector(start_bits, |
| 85 | expandable, |
| 86 | ArenaBitVectorAllocator<ArenaAllocator>::Create(arena, kind)) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 89 | ArenaBitVector::ArenaBitVector(ScopedArenaAllocator* arena, |
| 90 | unsigned int start_bits, |
| 91 | bool expandable, |
| 92 | ArenaAllocKind kind) |
| 93 | : BitVector(start_bits, |
| 94 | expandable, |
| 95 | ArenaBitVectorAllocator<ScopedArenaAllocator>::Create(arena, kind)) { |
Ian Rogers | b48b9eb | 2014-02-28 16:20:21 -0800 | [diff] [blame] | 96 | } |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 97 | |
buzbee | 862a760 | 2013-04-05 10:58:54 -0700 | [diff] [blame] | 98 | } // namespace art |