blob: b4d2b3f379198c959c13a23673e226df0cc1ecc2 [file] [log] [blame]
John Stiles5c7bb322020-10-22 11:09:15 -04001/*
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 Nicholasdaed2592021-03-04 14:30:25 -050010#include "include/private/SkSLDefines.h"
Ethan Nicholas7b776b52020-12-01 13:38:32 -050011
John Stiles5c7bb322020-10-22 11:09:15 -040012#define VLOG(...) // printf(__VA_ARGS__)
13
14namespace SkSL {
15
Ethan Nicholas7b776b52020-12-01 13:38:32 -050016static thread_local MemoryPool* sMemPool = nullptr;
17
18static MemoryPool* get_thread_local_memory_pool() {
19 return sMemPool;
20}
21
22static void set_thread_local_memory_pool(MemoryPool* memPool) {
23 sMemPool = memPool;
24}
25
John Stiles5c7bb322020-10-22 11:09:15 -040026Pool::~Pool() {
John Stiles23e68662020-10-29 10:17:15 -040027 if (get_thread_local_memory_pool() == fMemPool.get()) {
John Stiles5c7bb322020-10-22 11:09:15 -040028 SkDEBUGFAIL("SkSL pool is being destroyed while it is still attached to the thread");
John Stiles23e68662020-10-29 10:17:15 -040029 set_thread_local_memory_pool(nullptr);
John Stiles5c7bb322020-10-22 11:09:15 -040030 }
31
John Stiles23e68662020-10-29 10:17:15 -040032 fMemPool->reportLeaks();
33 SkASSERT(fMemPool->isEmpty());
John Stiles5c7bb322020-10-22 11:09:15 -040034
John Stiles23e68662020-10-29 10:17:15 -040035 VLOG("DELETE Pool:0x%016llX\n", (uint64_t)fMemPool.get());
John Stiles5c7bb322020-10-22 11:09:15 -040036}
37
John Stiles2d68ea32020-10-22 15:42:27 -040038std::unique_ptr<Pool> Pool::Create() {
John Stilesdd13dba2020-10-29 10:45:34 -040039 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 Stiles5c7bb322020-10-22 11:09:15 -040042 return pool;
43}
44
Ethan Nicholas624a5292021-04-16 14:54:43 -040045bool Pool::IsAttached() {
46 return get_thread_local_memory_pool();
47}
48
John Stiles5c7bb322020-10-22 11:09:15 -040049void Pool::attachToThread() {
John Stiles23e68662020-10-29 10:17:15 -040050 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 Stiles5c7bb322020-10-22 11:09:15 -040053}
54
John Stiles2d68ea32020-10-22 15:42:27 -040055void Pool::detachFromThread() {
John Stilese5d729c2020-10-29 10:26:28 -040056 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 Stiles23e68662020-10-29 10:17:15 -040060 set_thread_local_memory_pool(nullptr);
John Stiles2d68ea32020-10-22 15:42:27 -040061}
62
John Stilesb69a9d42020-11-19 14:50:56 -050063void* Pool::AllocMemory(size_t size) {
John Stiles3898bb52020-10-27 17:03:14 +000064 // Is a pool attached?
John Stiles23e68662020-10-29 10:17:15 -040065 MemoryPool* memPool = get_thread_local_memory_pool();
66 if (memPool) {
John Stilesb69a9d42020-11-19 14:50:56 -050067 void* ptr = memPool->allocate(size);
68 VLOG("ALLOC Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr);
69 return ptr;
John Stiles5c7bb322020-10-22 11:09:15 -040070 }
71
John Stilesb69a9d42020-11-19 14:50:56 -050072 // 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 Stiles5c7bb322020-10-22 11:09:15 -040076}
77
John Stilesb69a9d42020-11-19 14:50:56 -050078void Pool::FreeMemory(void* ptr) {
John Stiles5c7bb322020-10-22 11:09:15 -040079 // Is a pool attached?
John Stiles23e68662020-10-29 10:17:15 -040080 MemoryPool* memPool = get_thread_local_memory_pool();
81 if (memPool) {
John Stilesb69a9d42020-11-19 14:50:56 -050082 VLOG("FREE Pool:0x%016llX 0x%016llX\n", (uint64_t)memPool, (uint64_t)ptr);
83 memPool->release(ptr);
John Stiles23e68662020-10-29 10:17:15 -040084 return;
John Stiles5c7bb322020-10-22 11:09:15 -040085 }
86
John Stiles23e68662020-10-29 10:17:15 -040087 // There's no pool attached. Free it using the system allocator.
John Stilesb69a9d42020-11-19 14:50:56 -050088 VLOG("FREE Pool:__________________ 0x%016llX\n", (uint64_t)ptr);
89 ::operator delete(ptr);
John Stiles5c7bb322020-10-22 11:09:15 -040090}
91
John Stiles5c7bb322020-10-22 11:09:15 -040092} // namespace SkSL