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 | #include "src/sksl/SkSLPool.h" |
| 9 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 10 | #include "include/private/SkMutex.h" |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 11 | #include "src/sksl/ir/SkSLIRNode.h" |
| 12 | |
| 13 | #define VLOG(...) // printf(__VA_ARGS__) |
| 14 | |
| 15 | namespace SkSL { |
| 16 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame^] | 17 | static constexpr int kSmallNodeSize = 120; |
| 18 | static constexpr int kNodesInPool = 512; |
| 19 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 20 | namespace { struct IRNodeData { |
| 21 | union { |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame^] | 22 | uint8_t fBuffer[kSmallNodeSize]; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 23 | IRNodeData* fFreeListNext; |
| 24 | }; |
| 25 | }; } |
| 26 | |
| 27 | struct PoolData { |
| 28 | // This holds the first free node in the pool. It will be null when the pool is exhausted. |
| 29 | IRNodeData* fFreeListHead = fNodes; |
| 30 | |
| 31 | // This points to end of our pooled data, and implies the number of nodes. |
| 32 | IRNodeData* fNodesEnd = nullptr; |
| 33 | |
| 34 | // Our pooled data lives here. (We allocate lots of nodes here, not just one.) |
| 35 | IRNodeData fNodes[1]; |
| 36 | |
| 37 | // Accessors. |
| 38 | ptrdiff_t nodeCount() { return fNodesEnd - fNodes; } |
| 39 | |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 40 | int nodeIndex(IRNodeData* node) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 41 | SkASSERT(node >= fNodes); |
| 42 | SkASSERT(node < fNodesEnd); |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 43 | return SkToInt(node - fNodes); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 44 | } |
| 45 | }; |
| 46 | |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 47 | #if defined(SK_BUILD_FOR_IOS) && \ |
| 48 | (!defined(__IPHONE_9_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0) |
| 49 | |
| 50 | #include <pthread.h> |
| 51 | |
| 52 | static pthread_key_t get_pthread_key() { |
| 53 | static pthread_key_t sKey = []{ |
| 54 | pthread_key_t key; |
| 55 | int result = pthread_key_create(&key, /*destructor=*/nullptr); |
| 56 | if (result != 0) { |
| 57 | SK_ABORT("pthread_key_create failure: %d", result); |
| 58 | } |
| 59 | return key; |
| 60 | }(); |
| 61 | return sKey; |
| 62 | } |
| 63 | |
| 64 | static PoolData* get_thread_local_pool_data() { |
| 65 | return static_cast<PoolData*>(pthread_getspecific(get_pthread_key())); |
| 66 | } |
| 67 | |
| 68 | static void set_thread_local_pool_data(PoolData* poolData) { |
| 69 | pthread_setspecific(get_pthread_key(), poolData); |
| 70 | } |
| 71 | |
| 72 | #else |
| 73 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 74 | static thread_local PoolData* sPoolData = nullptr; |
| 75 | |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 76 | static PoolData* get_thread_local_pool_data() { |
| 77 | return sPoolData; |
| 78 | } |
| 79 | |
| 80 | static void set_thread_local_pool_data(PoolData* poolData) { |
| 81 | sPoolData = poolData; |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 86 | static Pool* sRecycledPool; // GUARDED_BY recycled_pool_mutex |
| 87 | static SkMutex& recycled_pool_mutex() { |
| 88 | static SkMutex* mutex = new SkMutex; |
| 89 | return *mutex; |
| 90 | } |
| 91 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 92 | static PoolData* create_pool_data(int nodesInPool) { |
| 93 | // Create a PoolData structure with extra space at the end for additional IRNode data. |
| 94 | int numExtraIRNodes = nodesInPool - 1; |
| 95 | PoolData* poolData = static_cast<PoolData*>(malloc(sizeof(PoolData) + |
| 96 | (sizeof(IRNodeData) * numExtraIRNodes))); |
| 97 | |
| 98 | // Initialize each pool node as a free node. The free nodes form a singly-linked list, each |
| 99 | // pointing to the next free node in sequence. |
| 100 | for (int index = 0; index < nodesInPool - 1; ++index) { |
| 101 | poolData->fNodes[index].fFreeListNext = &poolData->fNodes[index + 1]; |
| 102 | } |
| 103 | poolData->fNodes[nodesInPool - 1].fFreeListNext = nullptr; |
| 104 | poolData->fNodesEnd = &poolData->fNodes[nodesInPool]; |
| 105 | |
| 106 | return poolData; |
| 107 | } |
| 108 | |
| 109 | Pool::~Pool() { |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 110 | if (get_thread_local_pool_data() == fData) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 111 | SkDEBUGFAIL("SkSL pool is being destroyed while it is still attached to the thread"); |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 112 | set_thread_local_pool_data(nullptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 113 | } |
| 114 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 115 | this->checkForLeaks(); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 116 | |
| 117 | VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fData); |
| 118 | free(fData); |
| 119 | } |
| 120 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 121 | std::unique_ptr<Pool> Pool::Create() { |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 122 | SkAutoMutexExclusive lock(recycled_pool_mutex()); |
| 123 | std::unique_ptr<Pool> pool; |
| 124 | if (sRecycledPool) { |
| 125 | pool = std::unique_ptr<Pool>(sRecycledPool); |
| 126 | sRecycledPool = nullptr; |
| 127 | VLOG("REUSE Pool:0x%016llX\n", (uint64_t)pool->fData); |
| 128 | } else { |
| 129 | pool = std::unique_ptr<Pool>(new Pool); |
| 130 | pool->fData = create_pool_data(kNodesInPool); |
| 131 | pool->fData->fFreeListHead = &pool->fData->fNodes[0]; |
| 132 | VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fData); |
| 133 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 134 | return pool; |
| 135 | } |
| 136 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 137 | void Pool::Recycle(std::unique_ptr<Pool> pool) { |
| 138 | if (pool) { |
| 139 | pool->checkForLeaks(); |
| 140 | } |
| 141 | |
| 142 | SkAutoMutexExclusive lock(recycled_pool_mutex()); |
| 143 | if (sRecycledPool) { |
| 144 | delete sRecycledPool; |
| 145 | } |
| 146 | |
| 147 | VLOG("STASH Pool:0x%016llX\n", pool ? (uint64_t)pool->fData : 0ull); |
| 148 | sRecycledPool = pool.release(); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void Pool::attachToThread() { |
| 152 | VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fData); |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 153 | SkASSERT(get_thread_local_pool_data() == nullptr); |
| 154 | set_thread_local_pool_data(fData); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 155 | } |
| 156 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 157 | void Pool::detachFromThread() { |
| 158 | VLOG("DETACH Pool:0x%016llX\n", (uint64_t)get_thread_local_pool_data()); |
| 159 | SkASSERT(get_thread_local_pool_data() != nullptr); |
| 160 | set_thread_local_pool_data(nullptr); |
| 161 | } |
| 162 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame^] | 163 | void* Pool::AllocIRNode(size_t size) { |
John Stiles | 3898bb5 | 2020-10-27 17:03:14 +0000 | [diff] [blame] | 164 | // Is a pool attached? |
| 165 | PoolData* poolData = get_thread_local_pool_data(); |
| 166 | if (poolData) { |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame^] | 167 | // Can the requested size fit in a pool node? |
| 168 | if (size <= kSmallNodeSize) { |
| 169 | // Does the pool contain a free node? |
| 170 | IRNodeData* node = poolData->fFreeListHead; |
| 171 | if (node) { |
| 172 | // Yes. Take a node from the freelist. |
| 173 | poolData->fFreeListHead = node->fFreeListNext; |
| 174 | VLOG("ALLOC Pool:0x%016llX Index:%04d 0x%016llX\n", |
| 175 | (uint64_t)poolData, poolData->nodeIndex(node), (uint64_t)node); |
| 176 | return node->fBuffer; |
| 177 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame^] | 181 | // The pool can't be used for this allocation. Allocate nodes using the system allocator. |
| 182 | void* ptr = ::operator new(size); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 183 | VLOG("ALLOC Pool:0x%016llX Index:____ malloc 0x%016llX\n", |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 184 | (uint64_t)poolData, (uint64_t)ptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 185 | return ptr; |
| 186 | } |
| 187 | |
| 188 | void Pool::FreeIRNode(void* node_v) { |
| 189 | // Is a pool attached? |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 190 | PoolData* poolData = get_thread_local_pool_data(); |
| 191 | if (poolData) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 192 | // Did this node come from our pool? |
| 193 | auto* node = static_cast<IRNodeData*>(node_v); |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 194 | if (node >= &poolData->fNodes[0] && node < poolData->fNodesEnd) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 195 | // Yes. Push it back onto the freelist. |
| 196 | VLOG("FREE Pool:0x%016llX Index:%04d 0x%016llX\n", |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 197 | (uint64_t)poolData, poolData->nodeIndex(node), (uint64_t)node); |
| 198 | node->fFreeListNext = poolData->fFreeListHead; |
| 199 | poolData->fFreeListHead = node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | } |
| 203 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame^] | 204 | // We couldn't associate this node with our pool. Free it using the system allocator. |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 205 | VLOG("FREE Pool:0x%016llX Index:____ free 0x%016llX\n", |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 206 | (uint64_t)poolData, (uint64_t)node_v); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 207 | ::operator delete(node_v); |
| 208 | } |
| 209 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 210 | void Pool::checkForLeaks() { |
| 211 | #ifdef SK_DEBUG |
| 212 | ptrdiff_t nodeCount = fData->nodeCount(); |
| 213 | std::vector<bool> freed(nodeCount); |
| 214 | for (IRNodeData* node = fData->fFreeListHead; node; node = node->fFreeListNext) { |
| 215 | ptrdiff_t nodeIndex = fData->nodeIndex(node); |
| 216 | freed[nodeIndex] = true; |
| 217 | } |
| 218 | bool foundLeaks = false; |
| 219 | for (int index = 0; index < nodeCount; ++index) { |
| 220 | if (!freed[index]) { |
| 221 | IRNode* leak = reinterpret_cast<IRNode*>(fData->fNodes[index].fBuffer); |
| 222 | SkDebugf("Node %d leaked: %s\n", index, leak->description().c_str()); |
| 223 | foundLeaks = true; |
| 224 | } |
| 225 | } |
| 226 | if (foundLeaks) { |
| 227 | SkDEBUGFAIL("leaking SkSL pool nodes; if they are later freed, this will likely be fatal"); |
| 228 | } |
| 229 | #endif |
| 230 | } |
| 231 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 232 | } // namespace SkSL |