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