1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "src/sksl/SkSLPool.h" 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#include "include/private/SkSLDefines.h" 11cb93a386Sopenharmony_ci 12cb93a386Sopenharmony_ci#define VLOG(...) // printf(__VA_ARGS__) 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_cinamespace SkSL { 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_cistatic thread_local MemoryPool* sMemPool = nullptr; 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_cistatic MemoryPool* get_thread_local_memory_pool() { 19cb93a386Sopenharmony_ci return sMemPool; 20cb93a386Sopenharmony_ci} 21cb93a386Sopenharmony_ci 22cb93a386Sopenharmony_cistatic void set_thread_local_memory_pool(MemoryPool* memPool) { 23cb93a386Sopenharmony_ci sMemPool = memPool; 24cb93a386Sopenharmony_ci} 25cb93a386Sopenharmony_ci 26cb93a386Sopenharmony_ciPool::~Pool() { 27cb93a386Sopenharmony_ci if (get_thread_local_memory_pool() == fMemPool.get()) { 28cb93a386Sopenharmony_ci SkDEBUGFAIL("SkSL pool is being destroyed while it is still attached to the thread"); 29cb93a386Sopenharmony_ci set_thread_local_memory_pool(nullptr); 30cb93a386Sopenharmony_ci } 31cb93a386Sopenharmony_ci 32cb93a386Sopenharmony_ci fMemPool->reportLeaks(); 33cb93a386Sopenharmony_ci SkASSERT(fMemPool->isEmpty()); 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ci VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get()); 36cb93a386Sopenharmony_ci} 37cb93a386Sopenharmony_ci 38cb93a386Sopenharmony_cistd::unique_ptr<Pool> Pool::Create() { 39cb93a386Sopenharmony_ci auto pool = std::unique_ptr<Pool>(new Pool); 40cb93a386Sopenharmony_ci pool->fMemPool = MemoryPool::Make(/*preallocSize=*/65536, /*minAllocSize=*/32768); 41cb93a386Sopenharmony_ci VLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get()); 42cb93a386Sopenharmony_ci return pool; 43cb93a386Sopenharmony_ci} 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_cibool Pool::IsAttached() { 46cb93a386Sopenharmony_ci return get_thread_local_memory_pool(); 47cb93a386Sopenharmony_ci} 48cb93a386Sopenharmony_ci 49cb93a386Sopenharmony_civoid Pool::attachToThread() { 50cb93a386Sopenharmony_ci VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get()); 51cb93a386Sopenharmony_ci SkASSERT(get_thread_local_memory_pool() == nullptr); 52cb93a386Sopenharmony_ci set_thread_local_memory_pool(fMemPool.get()); 53cb93a386Sopenharmony_ci} 54cb93a386Sopenharmony_ci 55cb93a386Sopenharmony_civoid Pool::detachFromThread() { 56cb93a386Sopenharmony_ci MemoryPool* memPool = get_thread_local_memory_pool(); 57cb93a386Sopenharmony_ci VLOG("DETACH Pool:0x%016llX\n", (uint64_t)memPool); 58cb93a386Sopenharmony_ci SkASSERT(memPool == fMemPool.get()); 59cb93a386Sopenharmony_ci memPool->resetScratchSpace(); 60cb93a386Sopenharmony_ci set_thread_local_memory_pool(nullptr); 61cb93a386Sopenharmony_ci} 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_civoid* Pool::AllocMemory(size_t size) { 64cb93a386Sopenharmony_ci // Is a pool attached? 65cb93a386Sopenharmony_ci MemoryPool* memPool = get_thread_local_memory_pool(); 66cb93a386Sopenharmony_ci if (memPool) { 67cb93a386Sopenharmony_ci void* ptr = memPool->allocate(size); 68cb93a386Sopenharmony_ci VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr); 69cb93a386Sopenharmony_ci return ptr; 70cb93a386Sopenharmony_ci } 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci // There's no pool attached. Allocate memory using the system allocator. 73cb93a386Sopenharmony_ci void* ptr = ::operator new(size); 74cb93a386Sopenharmony_ci VLOG("ALLOC Pool:__________________ 0x%016llX\n", (uint64_t)ptr); 75cb93a386Sopenharmony_ci return ptr; 76cb93a386Sopenharmony_ci} 77cb93a386Sopenharmony_ci 78cb93a386Sopenharmony_civoid Pool::FreeMemory(void* ptr) { 79cb93a386Sopenharmony_ci // Is a pool attached? 80cb93a386Sopenharmony_ci MemoryPool* memPool = get_thread_local_memory_pool(); 81cb93a386Sopenharmony_ci if (memPool) { 82cb93a386Sopenharmony_ci VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr); 83cb93a386Sopenharmony_ci memPool->release(ptr); 84cb93a386Sopenharmony_ci return; 85cb93a386Sopenharmony_ci } 86cb93a386Sopenharmony_ci 87cb93a386Sopenharmony_ci // There's no pool attached. Free it using the system allocator. 88cb93a386Sopenharmony_ci VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)ptr); 89cb93a386Sopenharmony_ci ::operator delete(ptr); 90cb93a386Sopenharmony_ci} 91cb93a386Sopenharmony_ci 92cb93a386Sopenharmony_ci} // namespace SkSL 93