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 | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 10 | #define VLOG(...) // printf(__VA_ARGS__) |
| 11 | |
| 12 | namespace SkSL { |
| 13 | |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 14 | #if defined(SK_BUILD_FOR_IOS) && \ |
| 15 | (!defined(__IPHONE_9_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0) |
| 16 | |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | static pthread_key_t get_pthread_key() { |
| 20 | static pthread_key_t sKey = []{ |
| 21 | pthread_key_t key; |
| 22 | int result = pthread_key_create(&key, /*destructor=*/nullptr); |
| 23 | if (result != 0) { |
| 24 | SK_ABORT("pthread_key_create failure: %d", result); |
| 25 | } |
| 26 | return key; |
| 27 | }(); |
| 28 | return sKey; |
| 29 | } |
| 30 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 31 | static MemoryPool* get_thread_local_memory_pool() { |
| 32 | return static_cast<MemoryPool*>(pthread_getspecific(get_pthread_key())); |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 33 | } |
| 34 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 35 | static void set_thread_local_memory_pool(MemoryPool* poolData) { |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 36 | pthread_setspecific(get_pthread_key(), poolData); |
| 37 | } |
| 38 | |
| 39 | #else |
| 40 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 41 | static thread_local MemoryPool* sMemPool = nullptr; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 42 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 43 | static MemoryPool* get_thread_local_memory_pool() { |
| 44 | return sMemPool; |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 45 | } |
| 46 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 47 | static void set_thread_local_memory_pool(MemoryPool* memPool) { |
| 48 | sMemPool = memPool; |
John Stiles | 0bb9ec5 | 2020-10-22 11:35:18 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | #endif |
| 52 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 53 | Pool::~Pool() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 54 | if (get_thread_local_memory_pool() == fMemPool.get()) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 55 | 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] | 56 | set_thread_local_memory_pool(nullptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 57 | } |
| 58 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 59 | fMemPool->reportLeaks(); |
| 60 | SkASSERT(fMemPool->isEmpty()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 61 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 62 | VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 63 | } |
| 64 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 65 | std::unique_ptr<Pool> Pool::Create() { |
John Stiles | dd13dba | 2020-10-29 10:45:34 -0400 | [diff] [blame^] | 66 | auto pool = std::unique_ptr<Pool>(new Pool); |
| 67 | pool->fMemPool = MemoryPool::Make(/*preallocSize=*/65536, /*minAllocSize=*/32768); |
| 68 | VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 69 | return pool; |
| 70 | } |
| 71 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 72 | void Pool::attachToThread() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 73 | VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
| 74 | SkASSERT(get_thread_local_memory_pool() == nullptr); |
| 75 | set_thread_local_memory_pool(fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 76 | } |
| 77 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 78 | void Pool::detachFromThread() { |
John Stiles | e5d729c | 2020-10-29 10:26:28 -0400 | [diff] [blame] | 79 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 80 | VLOG("DETACH Pool:0x%016llX\n", (uint64_t)memPool); |
| 81 | SkASSERT(memPool == fMemPool.get()); |
| 82 | memPool->resetScratchSpace(); |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 83 | set_thread_local_memory_pool(nullptr); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 84 | } |
| 85 | |
John Stiles | 270b5c0 | 2020-10-27 17:49:37 -0400 | [diff] [blame] | 86 | void* Pool::AllocIRNode(size_t size) { |
John Stiles | 3898bb5 | 2020-10-27 17:03:14 +0000 | [diff] [blame] | 87 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 88 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 89 | if (memPool) { |
| 90 | void* node = memPool->allocate(size); |
| 91 | VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node); |
| 92 | return node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 93 | } |
| 94 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 95 | // There's no pool attached. Allocate nodes using the system allocator. |
| 96 | void* node = ::operator new(size); |
| 97 | VLOG("ALLOC Pool:__________________ 0x%016llX\n", (uint64_t)node); |
| 98 | return node; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 99 | } |
| 100 | |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 101 | void Pool::FreeIRNode(void* node) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 102 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 103 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 104 | if (memPool) { |
| 105 | VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)node); |
| 106 | memPool->release(node); |
| 107 | return; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 108 | } |
| 109 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 110 | // There's no pool attached. Free it using the system allocator. |
| 111 | VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)node); |
John Stiles | 15bfe38 | 2020-10-27 19:07:05 -0400 | [diff] [blame] | 112 | ::operator delete(node); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 113 | } |
| 114 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 115 | } // namespace SkSL |