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 | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 10 | #include <bitset> |
| 11 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 12 | #include "include/private/SkMutex.h" |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 13 | |
| 14 | #define VLOG(...) // printf(__VA_ARGS__) |
| 15 | |
| 16 | namespace SkSL { |
| 17 | |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 18 | #if defined(SK_BUILD_FOR_IOS) && \ |
| 19 | (!defined(__IPHONE_9_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0) |
| 20 | |
| 21 | #include <pthread.h> |
| 22 | |
| 23 | static pthread_key_t get_pthread_key() { |
| 24 | static pthread_key_t sKey = []{ |
| 25 | pthread_key_t key; |
| 26 | int result = pthread_key_create(&key, /*destructor=*/nullptr); |
| 27 | if (result != 0) { |
| 28 | SK_ABORT("pthread_key_create failure: %d", result); |
| 29 | } |
| 30 | return key; |
| 31 | }(); |
| 32 | return sKey; |
| 33 | } |
| 34 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 35 | static MemoryPool* get_thread_local_memory_pool() { |
| 36 | return static_cast<MemoryPool*>(pthread_getspecific(get_pthread_key())); |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 37 | } |
| 38 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 39 | static void set_thread_local_memory_pool(MemoryPool* poolData) { |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 40 | pthread_setspecific(get_pthread_key(), poolData); |
| 41 | } |
| 42 | |
| 43 | #else |
| 44 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 45 | static thread_local MemoryPool* sMemPool = nullptr; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 46 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 47 | static MemoryPool* get_thread_local_memory_pool() { |
| 48 | return sMemPool; |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 49 | } |
| 50 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 51 | static void set_thread_local_memory_pool(MemoryPool* memPool) { |
| 52 | sMemPool = memPool; |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | #endif |
| 56 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 57 | static Pool* sRecycledPool; // GUARDED_BY recycled_pool_mutex |
| 58 | static SkMutex& recycled_pool_mutex() { |
| 59 | static SkMutex* mutex = new SkMutex; |
| 60 | return *mutex; |
| 61 | } |
| 62 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 63 | Pool::~Pool() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 64 | if (get_thread_local_memory_pool() == fMemPool.get()) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 65 | SkDEBUGFAIL("SkSL pool is being destroyed while it is still attached to the thread"); |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 66 | set_thread_local_memory_pool(nullptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 67 | } |
| 68 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 69 | fMemPool->reportLeaks(); |
| 70 | SkASSERT(fMemPool->isEmpty()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 71 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 72 | VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 73 | } |
| 74 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 75 | std::unique_ptr<Pool> Pool::Create() { |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 76 | SkAutoMutexExclusive lock(recycled_pool_mutex()); |
| 77 | std::unique_ptr<Pool> pool; |
| 78 | if (sRecycledPool) { |
| 79 | pool = std::unique_ptr<Pool>(sRecycledPool); |
| 80 | sRecycledPool = nullptr; |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 81 | VLOG("REUSE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get()); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 82 | } else { |
| 83 | pool = std::unique_ptr<Pool>(new Pool); |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 84 | pool->fMemPool = MemoryPool::Make(/*preallocSize=*/65536, /*minAllocSize=*/32768); |
| 85 | VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get()); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 86 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 87 | return pool; |
| 88 | } |
| 89 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 90 | void Pool::Recycle(std::unique_ptr<Pool> pool) { |
| 91 | if (pool) { |
John Stiles | e5d729c | 2020-10-29 10:26:28 -0400 | [diff] [blame^] | 92 | if (get_thread_local_memory_pool() == pool->fMemPool.get()) { |
| 93 | SkDEBUGFAIL("SkSL pool is being recycled while it is still attached to the thread"); |
| 94 | } |
| 95 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 96 | pool->fMemPool->reportLeaks(); |
| 97 | SkASSERT(pool->fMemPool->isEmpty()); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | SkAutoMutexExclusive lock(recycled_pool_mutex()); |
| 101 | if (sRecycledPool) { |
| 102 | delete sRecycledPool; |
| 103 | } |
| 104 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 105 | VLOG("STASH Pool:0x%016llX\n", pool ? (uint64_t)pool->fMemPool.get() : 0ull); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 106 | sRecycledPool = pool.release(); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void Pool::attachToThread() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 110 | VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
| 111 | SkASSERT(get_thread_local_memory_pool() == nullptr); |
| 112 | set_thread_local_memory_pool(fMemPool.get()); |
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 | void Pool::detachFromThread() { |
John Stiles | e5d729c | 2020-10-29 10:26:28 -0400 | [diff] [blame^] | 116 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 117 | VLOG("DETACH Pool:0x%016llX\n", (uint64_t)memPool); |
| 118 | SkASSERT(memPool == fMemPool.get()); |
| 119 | memPool->resetScratchSpace(); |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 120 | set_thread_local_memory_pool(nullptr); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 121 | } |
| 122 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame] | 123 | void* Pool::AllocIRNode(size_t size) { |
John Stiles | 3898bb5 | 2020-10-27 17:03:14 +0000 | [diff] [blame] | 124 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 125 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 126 | if (memPool) { |
| 127 | void* node = memPool->allocate(size); |
| 128 | VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node); |
| 129 | return node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 130 | } |
| 131 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 132 | // There's no pool attached. Allocate nodes using the system allocator. |
| 133 | void* node = ::operator new(size); |
| 134 | VLOG("ALLOC Pool:__________________ 0x%016llX\n", (uint64_t)node); |
| 135 | return node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 136 | } |
| 137 | |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 138 | void Pool::FreeIRNode(void* node) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 139 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 140 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 141 | if (memPool) { |
| 142 | VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node); |
| 143 | memPool->release(node); |
| 144 | return; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 145 | } |
| 146 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 147 | // There's no pool attached. Free it using the system allocator. |
| 148 | VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)node); |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 149 | ::operator delete(node); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 150 | } |
| 151 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 152 | } // namespace SkSL |