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 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 19 | #include "allocator.h" |
| 20 | #include "arena_allocator.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 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> |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 53 | class ArenaBitVectorAllocator final : public Allocator, private ArenaBitVectorAllocatorKind { |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 54 | public: |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 55 | static ArenaBitVectorAllocator* Create(ArenaAlloc* allocator, ArenaAllocKind kind) { |
| 56 | void* storage = allocator->template Alloc<ArenaBitVectorAllocator>(kind); |
| 57 | return new (storage) ArenaBitVectorAllocator(allocator, kind); |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ~ArenaBitVectorAllocator() { |
| 61 | LOG(FATAL) << "UNREACHABLE"; |
| 62 | UNREACHABLE(); |
| 63 | } |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 64 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame^] | 65 | void* Alloc(size_t size) override { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 66 | return allocator_->Alloc(size, this->Kind()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame^] | 69 | void Free(void*) override {} // Nop. |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 70 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 71 | private: |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 72 | ArenaBitVectorAllocator(ArenaAlloc* allocator, ArenaAllocKind kind) |
| 73 | : ArenaBitVectorAllocatorKind(kind), allocator_(allocator) { } |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 74 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 75 | ArenaAlloc* const allocator_; |
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 | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 80 | ArenaBitVector::ArenaBitVector(ArenaAllocator* allocator, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 81 | unsigned int start_bits, |
| 82 | bool expandable, |
| 83 | ArenaAllocKind kind) |
| 84 | : BitVector(start_bits, |
| 85 | expandable, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 86 | ArenaBitVectorAllocator<ArenaAllocator>::Create(allocator, kind)) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 89 | ArenaBitVector::ArenaBitVector(ScopedArenaAllocator* allocator, |
Vladimir Marko | f6a35de | 2016-03-21 12:01:50 +0000 | [diff] [blame] | 90 | unsigned int start_bits, |
| 91 | bool expandable, |
| 92 | ArenaAllocKind kind) |
| 93 | : BitVector(start_bits, |
| 94 | expandable, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 95 | ArenaBitVectorAllocator<ScopedArenaAllocator>::Create(allocator, 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 |