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 | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 10 | #include "include/private/SkSLDefines.h" |
Ethan Nicholas | 7b776b5 | 2020-12-01 13:38:32 -0500 | [diff] [blame] | 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 | static thread_local MemoryPool* sMemPool = nullptr; |
| 17 | |
| 18 | static MemoryPool* get_thread_local_memory_pool() { |
| 19 | return sMemPool; |
| 20 | } |
| 21 | |
| 22 | static void set_thread_local_memory_pool(MemoryPool* memPool) { |
| 23 | sMemPool = memPool; |
| 24 | } |
| 25 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 26 | Pool::~Pool() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 27 | if (get_thread_local_memory_pool() == fMemPool.get()) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 28 | 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] | 29 | set_thread_local_memory_pool(nullptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 30 | } |
| 31 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 32 | fMemPool->reportLeaks(); |
| 33 | SkASSERT(fMemPool->isEmpty()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 34 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 35 | VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 36 | } |
| 37 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 38 | std::unique_ptr<Pool> Pool::Create() { |
John Stiles | dd13dba | 2020-10-29 10:45:34 -0400 | [diff] [blame] | 39 | auto pool = std::unique_ptr<Pool>(new Pool); |
| 40 | pool->fMemPool = MemoryPool::Make(/*preallocSize=*/65536, /*minAllocSize=*/32768); |
| 41 | VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 42 | return pool; |
| 43 | } |
| 44 | |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 45 | bool Pool::IsAttached() { |
| 46 | return get_thread_local_memory_pool(); |
| 47 | } |
| 48 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 49 | void Pool::attachToThread() { |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 50 | VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get()); |
| 51 | SkASSERT(get_thread_local_memory_pool() == nullptr); |
| 52 | set_thread_local_memory_pool(fMemPool.get()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 53 | } |
| 54 | |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 55 | void Pool::detachFromThread() { |
John Stiles | e5d729c | 2020-10-29 10:26:28 -0400 | [diff] [blame] | 56 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 57 | VLOG("DETACH Pool:0x%016llX\n", (uint64_t)memPool); |
| 58 | SkASSERT(memPool == fMemPool.get()); |
| 59 | memPool->resetScratchSpace(); |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 60 | set_thread_local_memory_pool(nullptr); |
John Stiles | 2d68ea3 | 2020-10-22 15:42:27 -0400 | [diff] [blame] | 61 | } |
| 62 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 63 | void* Pool::AllocMemory(size_t size) { |
John Stiles | 3898bb5 | 2020-10-27 17:03:14 +0000 | [diff] [blame] | 64 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 65 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 66 | if (memPool) { |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 67 | void* ptr = memPool->allocate(size); |
| 68 | VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr); |
| 69 | return ptr; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 70 | } |
| 71 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 72 | // There's no pool attached. Allocate memory using the system allocator. |
| 73 | void* ptr = ::operator new(size); |
| 74 | VLOG("ALLOC Pool:__________________ 0x%016llX\n", (uint64_t)ptr); |
| 75 | return ptr; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 76 | } |
| 77 | |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 78 | void Pool::FreeMemory(void* ptr) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 79 | // Is a pool attached? |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 80 | MemoryPool* memPool = get_thread_local_memory_pool(); |
| 81 | if (memPool) { |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 82 | VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr); |
| 83 | memPool->release(ptr); |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 84 | return; |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 85 | } |
| 86 | |
John Stiles | 23e6866 | 2020-10-29 10:17:15 -0400 | [diff] [blame] | 87 | // There's no pool attached. Free it using the system allocator. |
John Stiles | b69a9d4 | 2020-11-19 14:50:56 -0500 | [diff] [blame] | 88 | VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)ptr); |
| 89 | ::operator delete(ptr); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 90 | } |
| 91 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 92 | } // namespace SkSL |