| Flutter Impeller |

pool.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_RENDERER_POOL_H_
6 #define FLUTTER_IMPELLER_RENDERER_POOL_H_
7
8 #include <cstdint>
9 #include <memory>
10 #include <mutex>
11
12 namespace impeller {
13
14 /// @brief A thread-safe pool with a limited byte size.
15 /// @tparam T The type that the pool will contain.
16 template <typename T>
17 class Pool {
18 public:
19 explicit Pool(uint32_t limit_bytes) : limit_bytes_(limit_bytes) {}
20
21 std::shared_ptr<T> Grab() {
22 std::scoped_lock lock(mutex_);
23 if (pool_.empty()) {
24 return T::Create();
25 }
26 std::shared_ptr<T> result = std::move(pool_.back());
27 pool_.pop_back();
28 size_ -= result->GetSize();
29 return result;
30 }
31
32 void Recycle(std::shared_ptr<T> object) {
33 std::scoped_lock lock(mutex_);
34 size_t object_size = object->GetSize();
35 if (size_ + object_size <= limit_bytes_ &&
36 object_size < (limit_bytes_ / 2)) {
37 object->Reset();
38 size_ += object_size;
39 pool_.emplace_back(std::move(object));
40 }
41 }
42
43 uint32_t GetSize() const {
44 std::scoped_lock lock(mutex_);
45 return size_;
46 }
47
48 private:
49 std::vector<std::shared_ptr<T>> pool_;
50 const uint32_t limit_bytes_;
51 uint32_t size_ = 0;
52 // Note: This would perform better as a lockless ring buffer.
53 mutable std::mutex mutex_;
54 };
55
56 } // namespace impeller
57
58 #endif // FLUTTER_IMPELLER_RENDERER_POOL_H_
impeller::Pool::Pool
Pool(uint32_t limit_bytes)
Definition: pool.h:19
impeller::Pool::GetSize
uint32_t GetSize() const
Definition: pool.h:43
impeller::Pool::Grab
std::shared_ptr< T > Grab()
Definition: pool.h:21
impeller::Pool
A thread-safe pool with a limited byte size.
Definition: pool.h:17
impeller::Pool::Recycle
void Recycle(std::shared_ptr< T > object)
Definition: pool.h:32
impeller
Definition: aiks_blend_unittests.cc:18