blob: f40491b14313ebbd24c0b92e8eeb2f96e4f62fc5 [file] [log] [blame]
Lingfeng Yang4bc45372020-10-26 11:50:25 -07001// 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 Lif35ede42021-04-30 13:05:50 -070017#include "base/Allocator.h"
Lingfeng Yang4bc45372020-10-26 11:50:25 -070018
19#include <vector>
20#include <unordered_set>
21
22#include <inttypes.h>
23
24namespace android {
25namespace 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 Lif35ede42021-04-30 13:05:50 -070032class BumpPool : public Allocator {
Lingfeng Yang4bc45372020-10-26 11:50:25 -070033public:
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 Lif35ede42021-04-30 13:05:50 -070040 void* alloc(size_t wantedSize) override {
Lingfeng Yang4bc45372020-10-26 11:50:25 -070041 size_t wantedSizeRoundedUp =
42 sizeof(uint64_t) * ((wantedSize + sizeof(uint64_t) - 1) / (sizeof(uint64_t)));
43
44 mTotalWantedThisGeneration += wantedSizeRoundedUp;
Lingfeng Yang58383b62021-01-22 20:39:08 -080045 if (mAllocPos + wantedSizeRoundedUp > mStorage.size() * sizeof(uint64_t)) {
Lingfeng Yang4bc45372020-10-26 11:50:25 -070046 mNeedRealloc = true;
47 void* fallbackPtr = malloc(wantedSizeRoundedUp);
48 mFallbackPtrs.insert(fallbackPtr);
49 return fallbackPtr;
50 }
Lingfeng Yang58383b62021-01-22 20:39:08 -080051 void* allocPtr = (void*)(((unsigned char*)mStorage.data()) + mAllocPos);
Lingfeng Yang4bc45372020-10-26 11:50:25 -070052 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 Yang4bc45372020-10-26 11:50:25 -070068private:
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