1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2012 Google Inc.
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/gpu/GrMemoryPool.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/private/SkTPin.h"
11cb93a386Sopenharmony_ci#include "src/core/SkASAN.h"
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci#ifdef SK_DEBUG
14cb93a386Sopenharmony_ci    #include <atomic>
15cb93a386Sopenharmony_ci#endif
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci#include <tuple>
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci///////////////////////////////////////////////////////////////////////////////////////////////////
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_cistd::unique_ptr<GrMemoryPool> GrMemoryPool::Make(size_t preallocSize, size_t minAllocSize) {
22cb93a386Sopenharmony_ci    static_assert(sizeof(GrMemoryPool) < GrMemoryPool::kMinAllocationSize);
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    preallocSize = SkTPin(preallocSize, kMinAllocationSize,
25cb93a386Sopenharmony_ci                          (size_t) SkBlockAllocator::kMaxAllocationSize);
26cb93a386Sopenharmony_ci    minAllocSize = SkTPin(minAllocSize, kMinAllocationSize,
27cb93a386Sopenharmony_ci                          (size_t) SkBlockAllocator::kMaxAllocationSize);
28cb93a386Sopenharmony_ci    void* mem = operator new(preallocSize);
29cb93a386Sopenharmony_ci    return std::unique_ptr<GrMemoryPool>(new (mem) GrMemoryPool(preallocSize, minAllocSize));
30cb93a386Sopenharmony_ci}
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ciGrMemoryPool::GrMemoryPool(size_t preallocSize, size_t minAllocSize)
33cb93a386Sopenharmony_ci        : fAllocator(SkBlockAllocator::GrowthPolicy::kFixed, minAllocSize,
34cb93a386Sopenharmony_ci                     preallocSize - offsetof(GrMemoryPool, fAllocator) - sizeof(SkBlockAllocator)) {
35cb93a386Sopenharmony_ci    SkDEBUGCODE(fAllocationCount = 0;)
36cb93a386Sopenharmony_ci}
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ciGrMemoryPool::~GrMemoryPool() {
39cb93a386Sopenharmony_ci    this->reportLeaks();
40cb93a386Sopenharmony_ci    SkASSERT(0 == fAllocationCount);
41cb93a386Sopenharmony_ci    SkASSERT(this->isEmpty());
42cb93a386Sopenharmony_ci}
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_civoid GrMemoryPool::reportLeaks() const {
45cb93a386Sopenharmony_ci#ifdef SK_DEBUG
46cb93a386Sopenharmony_ci    int i = 0;
47cb93a386Sopenharmony_ci    int n = fAllocatedIDs.count();
48cb93a386Sopenharmony_ci    for (int id : fAllocatedIDs) {
49cb93a386Sopenharmony_ci        if (++i == 1) {
50cb93a386Sopenharmony_ci            SkDebugf("Leaked %d IDs (in no particular order): %d%s", n, id, (n == i) ? "\n" : "");
51cb93a386Sopenharmony_ci        } else if (i < 11) {
52cb93a386Sopenharmony_ci            SkDebugf(", %d%s", id, (n == i ? "\n" : ""));
53cb93a386Sopenharmony_ci        } else if (i == 11) {
54cb93a386Sopenharmony_ci            SkDebugf(", ...\n");
55cb93a386Sopenharmony_ci            break;
56cb93a386Sopenharmony_ci        }
57cb93a386Sopenharmony_ci    }
58cb93a386Sopenharmony_ci#endif
59cb93a386Sopenharmony_ci}
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_civoid* GrMemoryPool::allocate(size_t size) {
62cb93a386Sopenharmony_ci    static_assert(alignof(Header) <= kAlignment);
63cb93a386Sopenharmony_ci    SkDEBUGCODE(this->validate();)
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci    SkBlockAllocator::ByteRange alloc = fAllocator.allocate<kAlignment, sizeof(Header)>(size);
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    // Initialize GrMemoryPool's custom header at the start of the allocation
68cb93a386Sopenharmony_ci    Header* header = static_cast<Header*>(alloc.fBlock->ptr(alloc.fAlignedOffset - sizeof(Header)));
69cb93a386Sopenharmony_ci    header->fStart = alloc.fStart;
70cb93a386Sopenharmony_ci    header->fEnd = alloc.fEnd;
71cb93a386Sopenharmony_ci
72cb93a386Sopenharmony_ci    // Update live count within the block
73cb93a386Sopenharmony_ci    alloc.fBlock->setMetadata(alloc.fBlock->metadata() + 1);
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ci#if defined(SK_SANITIZE_ADDRESS)
76cb93a386Sopenharmony_ci    sk_asan_poison_memory_region(&header->fSentinel, sizeof(header->fSentinel));
77cb93a386Sopenharmony_ci#elif defined(SK_DEBUG)
78cb93a386Sopenharmony_ci    header->fSentinel = SkBlockAllocator::kAssignedMarker;
79cb93a386Sopenharmony_ci#endif
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci#if defined(SK_DEBUG)
82cb93a386Sopenharmony_ci    header->fID = []{
83cb93a386Sopenharmony_ci        static std::atomic<int> nextID{1};
84cb93a386Sopenharmony_ci        return nextID.fetch_add(1, std::memory_order_relaxed);
85cb93a386Sopenharmony_ci    }();
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci    // You can set a breakpoint here when a leaked ID is allocated to see the stack frame.
88cb93a386Sopenharmony_ci    fAllocatedIDs.add(header->fID);
89cb93a386Sopenharmony_ci    fAllocationCount++;
90cb93a386Sopenharmony_ci#endif
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_ci    // User-facing pointer is after the header padding
93cb93a386Sopenharmony_ci    return alloc.fBlock->ptr(alloc.fAlignedOffset);
94cb93a386Sopenharmony_ci}
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_civoid GrMemoryPool::release(void* p) {
97cb93a386Sopenharmony_ci    Header* header = reinterpret_cast<Header*>(reinterpret_cast<intptr_t>(p) - sizeof(Header));
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci#if defined(SK_SANITIZE_ADDRESS)
100cb93a386Sopenharmony_ci    sk_asan_unpoison_memory_region(&header->fSentinel, sizeof(header->fSentinel));
101cb93a386Sopenharmony_ci#elif defined(SK_DEBUG)
102cb93a386Sopenharmony_ci    SkASSERT(SkBlockAllocator::kAssignedMarker == header->fSentinel);
103cb93a386Sopenharmony_ci    header->fSentinel = SkBlockAllocator::kFreedMarker;
104cb93a386Sopenharmony_ci#endif
105cb93a386Sopenharmony_ci
106cb93a386Sopenharmony_ci#if defined(SK_DEBUG)
107cb93a386Sopenharmony_ci    fAllocatedIDs.remove(header->fID);
108cb93a386Sopenharmony_ci    fAllocationCount--;
109cb93a386Sopenharmony_ci#endif
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ci    SkBlockAllocator::Block* block = fAllocator.owningBlock<kAlignment>(header, header->fStart);
112cb93a386Sopenharmony_ci
113cb93a386Sopenharmony_ci#if defined(SK_DEBUG)
114cb93a386Sopenharmony_ci    // (p - block) matches the original alignedOffset value from SkBlockAllocator::allocate().
115cb93a386Sopenharmony_ci    intptr_t alignedOffset = (intptr_t)p - (intptr_t)block;
116cb93a386Sopenharmony_ci    SkASSERT(p == block->ptr(alignedOffset));
117cb93a386Sopenharmony_ci
118cb93a386Sopenharmony_ci    // Scrub the block contents to prevent use-after-free errors.
119cb93a386Sopenharmony_ci    memset(p, 0xDD, header->fEnd - alignedOffset);
120cb93a386Sopenharmony_ci#endif
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ci    int alive = block->metadata();
123cb93a386Sopenharmony_ci    if (alive == 1) {
124cb93a386Sopenharmony_ci        // This was last allocation in the block, so remove it
125cb93a386Sopenharmony_ci        fAllocator.releaseBlock(block);
126cb93a386Sopenharmony_ci    } else {
127cb93a386Sopenharmony_ci        // Update count and release storage of the allocation itself
128cb93a386Sopenharmony_ci        block->setMetadata(alive - 1);
129cb93a386Sopenharmony_ci        block->release(header->fStart, header->fEnd);
130cb93a386Sopenharmony_ci    }
131cb93a386Sopenharmony_ci}
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_ci#ifdef SK_DEBUG
134cb93a386Sopenharmony_civoid GrMemoryPool::validate() const {
135cb93a386Sopenharmony_ci    fAllocator.validate();
136cb93a386Sopenharmony_ci
137cb93a386Sopenharmony_ci    int allocCount = 0;
138cb93a386Sopenharmony_ci    for (const auto* b : fAllocator.blocks()) {
139cb93a386Sopenharmony_ci        allocCount += b->metadata();
140cb93a386Sopenharmony_ci    }
141cb93a386Sopenharmony_ci    SkASSERT(allocCount == fAllocationCount);
142cb93a386Sopenharmony_ci    SkASSERT(fAllocationCount == fAllocatedIDs.count());
143cb93a386Sopenharmony_ci    SkASSERT(allocCount > 0 || this->isEmpty());
144cb93a386Sopenharmony_ci}
145cb93a386Sopenharmony_ci#endif
146