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 | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 92 | pool->fMemPool->reportLeaks(); |
| 93 | SkASSERT(pool->fMemPool->isEmpty()); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | SkAutoMutexExclusive lock(recycled_pool_mutex()); |
| 97 | if (sRecycledPool) { |
| 98 | delete sRecycledPool; |
| 99 | } |
| 100 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 101 | 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] | 102 | sRecycledPool = pool.release(); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void Pool::attachToThread() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 106 | VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
| 107 | SkASSERT(get_thread_local_memory_pool() == nullptr); |
| 108 | set_thread_local_memory_pool(fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 109 | } |
| 110 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 111 | void Pool::detachFromThread() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 112 | VLOG("DETACH Pool:0x%016llX\n", (uint64_t)get_thread_local_memory_pool()); |
| 113 | SkASSERT(get_thread_local_memory_pool() != nullptr); |
| 114 | set_thread_local_memory_pool(nullptr); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 115 | } |
| 116 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame] | 117 | void* Pool::AllocIRNode(size_t size) { |
John Stiles | 3898bb5 | 2020-10-27 17:03:14 +0000 | [diff] [blame] | 118 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 119 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 120 | if (memPool) { |
| 121 | void* node = memPool->allocate(size); |
| 122 | VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node); |
| 123 | return node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 124 | } |
| 125 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 126 | // There's no pool attached. Allocate nodes using the system allocator. |
| 127 | void* node = ::operator new(size); |
| 128 | VLOG("ALLOC Pool:__________________ 0x%016llX\n", (uint64_t)node); |
| 129 | return node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 130 | } |
| 131 | |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 132 | void Pool::FreeIRNode(void* node) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 133 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 134 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 135 | if (memPool) { |
| 136 | VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node); |
| 137 | memPool->release(node); |
| 138 | return; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 139 | } |
| 140 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame^] | 141 | // There's no pool attached. Free it using the system allocator. |
| 142 | VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)node); |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 143 | ::operator delete(node); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 144 | } |
| 145 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 146 | } // namespace SkSL |