John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_POOL |
| 9 | #define SKSL_POOL |
| 10 | |
| 11 | #include <memory> |
| 12 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 13 | #include "src/sksl/SkSLMemoryPool.h" |
| 14 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 15 | namespace SkSL { |
| 16 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 17 | /** |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 18 | * Efficiently allocates memory in an SkSL program. Optimized for allocate/release performance over |
| 19 | * memory efficiency. |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 20 | * |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 21 | * All allocated memory must be released back to the pool before it can be destroyed or recycled. |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 22 | */ |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 23 | |
| 24 | class Pool { |
| 25 | public: |
| 26 | ~Pool(); |
| 27 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 28 | // Creates a pool to store objects during program creation. Call attachToThread() to start using |
| 29 | // the pool for its allocations. When your program is complete, call pool->detachFromThread() to |
| 30 | // take ownership of the pool and its allocations. Before freeing any of the program's |
| 31 | // allocations, make sure to reattach the pool by calling pool->attachToThread() again. |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 32 | static std::unique_ptr<Pool> Create(); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 33 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 34 | // Attaches a pool to the current thread. |
| 35 | // It is an error to call this while a pool is already attached. |
| 36 | void attachToThread(); |
| 37 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 38 | // Once you are done creating or destroying objects in the pool, detach it from the thread. |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 39 | // It is an error to call this while no pool is attached. |
| 40 | void detachFromThread(); |
| 41 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 42 | // Allocates memory from the thread pool. If the pool is exhausted, an additional block of pool |
| 43 | // storage will be created to hold the data. |
| 44 | static void* AllocMemory(size_t size); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 45 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 46 | // Releases memory that was created by AllocMemory. All objects in the pool must be freed before |
| 47 | // the pool can be destroyed. |
| 48 | static void FreeMemory(void* ptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 49 | |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 50 | static bool IsAttached(); |
| 51 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 52 | private: |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 53 | Pool() = default; // use Create to make a pool |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 54 | std::unique_ptr<SkSL::MemoryPool> fMemPool; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 55 | }; |
| 56 | |
John Stiles | 5d1b5a2 | 2021-02-01 19:08:45 -0500 | [diff] [blame] | 57 | /** |
| 58 | * If your class inherits from Poolable, its objects will be allocated from the pool. |
| 59 | */ |
| 60 | class Poolable { |
| 61 | public: |
| 62 | // Override operator new and delete to allow us to use a memory pool. |
| 63 | static void* operator new(const size_t size) { |
| 64 | return Pool::AllocMemory(size); |
| 65 | } |
| 66 | |
| 67 | static void operator delete(void* ptr) { |
| 68 | Pool::FreeMemory(ptr); |
| 69 | } |
| 70 | }; |
| 71 | |
John Stiles | 0cac5ed | 2021-08-06 11:40:39 -0400 | [diff] [blame^] | 72 | /** |
| 73 | * Temporarily attaches a pool to the current thread within a scope. |
| 74 | */ |
| 75 | class AutoAttachPoolToThread { |
| 76 | public: |
| 77 | AutoAttachPoolToThread(Pool* p) : fPool(p) { |
| 78 | if (fPool) { |
| 79 | fPool->attachToThread(); |
| 80 | } |
| 81 | } |
| 82 | ~AutoAttachPoolToThread() { |
| 83 | if (fPool) { |
| 84 | fPool->detachFromThread(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | Pool* fPool = nullptr; |
| 90 | }; |
| 91 | |
| 92 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 93 | } // namespace SkSL |
| 94 | |
| 95 | #endif |