Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #pragma once |
| 15 | |
| 16 | #include "base/AlignedBuf.h" |
Yilong Li | f35ede4 | 2021-04-30 13:05:50 -0700 | [diff] [blame] | 17 | #include "base/Allocator.h" |
Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 18 | |
| 19 | #include <vector> |
| 20 | #include <unordered_set> |
| 21 | |
| 22 | #include <inttypes.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace base { |
| 26 | |
| 27 | // Class to make it easier to set up memory regions where it is fast |
| 28 | // to allocate buffers AND we don't care about freeing individual pieces, |
| 29 | // BUT it's necessary to preserve previous pointer values in between the first |
| 30 | // alloc() after a freeAll(), and the freeAll() itself, allowing some sloppy use of |
| 31 | // malloc in the first pass while we find out how much data was needed. |
Yilong Li | f35ede4 | 2021-04-30 13:05:50 -0700 | [diff] [blame] | 32 | class BumpPool : public Allocator { |
Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 33 | public: |
| 34 | BumpPool(size_t startingBytes = 4096) : mStorage(startingBytes / sizeof(uint64_t)) { } |
| 35 | // All memory allocated by this pool |
| 36 | // is automatically deleted when the pool |
| 37 | // is deconstructed. |
| 38 | ~BumpPool() { } |
| 39 | |
Yilong Li | f35ede4 | 2021-04-30 13:05:50 -0700 | [diff] [blame] | 40 | void* alloc(size_t wantedSize) override { |
Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 41 | size_t wantedSizeRoundedUp = |
| 42 | sizeof(uint64_t) * ((wantedSize + sizeof(uint64_t) - 1) / (sizeof(uint64_t))); |
| 43 | |
| 44 | mTotalWantedThisGeneration += wantedSizeRoundedUp; |
Lingfeng Yang | 58383b6 | 2021-01-22 20:39:08 -0800 | [diff] [blame] | 45 | if (mAllocPos + wantedSizeRoundedUp > mStorage.size() * sizeof(uint64_t)) { |
Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 46 | mNeedRealloc = true; |
| 47 | void* fallbackPtr = malloc(wantedSizeRoundedUp); |
| 48 | mFallbackPtrs.insert(fallbackPtr); |
| 49 | return fallbackPtr; |
| 50 | } |
Lingfeng Yang | 58383b6 | 2021-01-22 20:39:08 -0800 | [diff] [blame] | 51 | void* allocPtr = (void*)(((unsigned char*)mStorage.data()) + mAllocPos); |
Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 52 | mAllocPos += wantedSizeRoundedUp; |
| 53 | return allocPtr; |
| 54 | } |
| 55 | |
| 56 | void freeAll() { |
| 57 | mAllocPos = 0; |
| 58 | if (mNeedRealloc) { |
| 59 | mStorage.resize((mTotalWantedThisGeneration * 2) / sizeof(uint64_t)); |
| 60 | mNeedRealloc = false; |
| 61 | for (auto ptr : mFallbackPtrs) { |
| 62 | free(ptr); |
| 63 | } |
| 64 | mFallbackPtrs.clear(); |
| 65 | } |
| 66 | mTotalWantedThisGeneration = 0; |
| 67 | } |
Lingfeng Yang | 4bc4537 | 2020-10-26 11:50:25 -0700 | [diff] [blame] | 68 | private: |
| 69 | AlignedBuf<uint64_t, 8> mStorage; |
| 70 | std::unordered_set<void*> mFallbackPtrs; |
| 71 | size_t mAllocPos = 0; |
| 72 | size_t mTotalWantedThisGeneration = 0; |
| 73 | bool mNeedRealloc = false; |
| 74 | }; |
| 75 | |
| 76 | } // namespace base |
| 77 | } // namespace android |