xref: /aosp_15_r20/external/skia/src/sksl/SkSLPool.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkTypes.h"
9 #include "src/sksl/SkSLMemoryPool.h"
10 #include "src/sksl/SkSLPool.h"
11 
12 #define SkVLOG(...) // SkDEBUGF(__VA_ARGS__)
13 
14 namespace SkSL {
15 
16 static thread_local MemoryPool* sMemPool = nullptr;
17 
get_thread_local_memory_pool()18 static MemoryPool* get_thread_local_memory_pool() {
19     return sMemPool;
20 }
21 
set_thread_local_memory_pool(MemoryPool * memPool)22 static void set_thread_local_memory_pool(MemoryPool* memPool) {
23     sMemPool = memPool;
24 }
25 
26 Pool::Pool() = default;
27 
~Pool()28 Pool::~Pool() {
29     if (get_thread_local_memory_pool() == fMemPool.get()) {
30         SkDEBUGFAIL("SkSL pool is being destroyed while it is still attached to the thread");
31         set_thread_local_memory_pool(nullptr);
32     }
33 
34     SkVLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get());
35 }
36 
Create()37 std::unique_ptr<Pool> Pool::Create() {
38     auto pool = std::unique_ptr<Pool>(new Pool);
39     pool->fMemPool = MemoryPool::Make();
40     SkVLOG("CREATE Pool:0x%016llX\n", (uint64_t)pool->fMemPool.get());
41     return pool;
42 }
43 
IsAttached()44 bool Pool::IsAttached() {
45     return get_thread_local_memory_pool();
46 }
47 
attachToThread()48 void Pool::attachToThread() {
49     SkVLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get());
50     SkASSERT(get_thread_local_memory_pool() == nullptr);
51     set_thread_local_memory_pool(fMemPool.get());
52 }
53 
detachFromThread()54 void Pool::detachFromThread() {
55     SkVLOG("DETACH Pool:0x%016llX\n", (uint64_t)memPool);
56     SkASSERT(get_thread_local_memory_pool() == fMemPool.get());
57     set_thread_local_memory_pool(nullptr);
58 }
59 
AllocMemory(size_t size)60 void* Pool::AllocMemory(size_t size) {
61     // Is a pool attached?
62     MemoryPool* memPool = get_thread_local_memory_pool();
63     if (memPool) {
64         void* ptr = memPool->allocate(size);
65         SkVLOG("ALLOC  Pool:0x%016llX  0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr);
66         return ptr;
67     }
68 
69     // There's no pool attached. Allocate memory using the system allocator.
70     void* ptr = ::operator new(size);
71     SkVLOG("ALLOC  Pool:__________________  0x%016llX\n", (uint64_t)ptr);
72     return ptr;
73 }
74 
FreeMemory(void * ptr)75 void Pool::FreeMemory(void* ptr) {
76     // Is a pool attached?
77     MemoryPool* memPool = get_thread_local_memory_pool();
78     if (memPool) {
79         SkVLOG("FREE   Pool:0x%016llX  0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr);
80         memPool->release(ptr);
81         return;
82     }
83 
84     // There's no pool attached. Free it using the system allocator.
85     SkVLOG("FREE   Pool:__________________  0x%016llX\n", (uint64_t)ptr);
86     ::operator delete(ptr);
87 }
88 
89 }  // namespace SkSL
90