Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [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 | |
Yilong Li | f35ede4 | 2021-04-30 13:05:50 -0700 | [diff] [blame] | 16 | #include "android/base/Allocator.h" |
| 17 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 18 | #include <unordered_set> |
| 19 | |
| 20 | #include <inttypes.h> |
| 21 | #include <stddef.h> |
| 22 | #include <string.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/deallocate buffers that have size within |
| 29 | // the specified range. |
Yilong Li | f35ede4 | 2021-04-30 13:05:50 -0700 | [diff] [blame] | 30 | class Pool : public Allocator { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 31 | public: |
| 32 | // minSize/maxSize: the target range of sizes for which we want to |
| 33 | // make allocations fast. the greater the range, the more space |
| 34 | // traded off. |
| 35 | // chunksPerSize: the target maximum number of live objects of |
| 36 | // each size that are expected. the higher it is, the more space |
| 37 | // traded off. |
| 38 | // |
| 39 | // Rough space cost formula: |
| 40 | // O(chunksPerSize * log2(maxSize / minSize) * maxSize) |
Lingfeng Yang | fc62bd5 | 2020-11-03 09:04:39 -0800 | [diff] [blame] | 41 | Pool(size_t minSize = 4, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 42 | size_t maxSize = 4096, |
Lingfeng Yang | fc62bd5 | 2020-11-03 09:04:39 -0800 | [diff] [blame] | 43 | size_t chunksPerSize = 1024); |
| 44 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 45 | // All memory allocated by this pool |
| 46 | // is automatically deleted when the pool |
| 47 | // is deconstructed. |
| 48 | ~Pool(); |
| 49 | |
Yilong Li | f35ede4 | 2021-04-30 13:05:50 -0700 | [diff] [blame] | 50 | void* alloc(size_t wantedSize) override; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 51 | void free(void* ptr); |
| 52 | |
| 53 | // Convenience function to free everything currently allocated. |
| 54 | void freeAll(); |
| 55 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 56 | private: |
| 57 | class Impl; |
| 58 | Impl* mImpl = nullptr; |
| 59 | |
| 60 | std::unordered_set<void*> mFallbackPtrs; |
| 61 | }; |
| 62 | |
| 63 | } // namespace base |
| 64 | } // namespace android |