• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dawn_native/PooledResourceMemoryAllocator.h"
16 #include "dawn_native/Device.h"
17 
18 namespace dawn_native {
19 
PooledResourceMemoryAllocator(ResourceHeapAllocator * heapAllocator)20     PooledResourceMemoryAllocator::PooledResourceMemoryAllocator(
21         ResourceHeapAllocator* heapAllocator)
22         : mHeapAllocator(heapAllocator) {
23     }
24 
DestroyPool()25     void PooledResourceMemoryAllocator::DestroyPool() {
26         for (auto& resourceHeap : mPool) {
27             ASSERT(resourceHeap != nullptr);
28             mHeapAllocator->DeallocateResourceHeap(std::move(resourceHeap));
29         }
30 
31         mPool.clear();
32     }
33 
34     ResultOrError<std::unique_ptr<ResourceHeapBase>>
AllocateResourceHeap(uint64_t size)35     PooledResourceMemoryAllocator::AllocateResourceHeap(uint64_t size) {
36         // Pooled memory is LIFO because memory can be evicted by LRU. However, this means
37         // pooling is disabled in-frame when the memory is still pending. For high in-frame
38         // memory users, FIFO might be preferable when memory consumption is a higher priority.
39         std::unique_ptr<ResourceHeapBase> memory;
40         if (!mPool.empty()) {
41             memory = std::move(mPool.front());
42             mPool.pop_front();
43         }
44 
45         if (memory == nullptr) {
46             DAWN_TRY_ASSIGN(memory, mHeapAllocator->AllocateResourceHeap(size));
47         }
48 
49         return std::move(memory);
50     }
51 
DeallocateResourceHeap(std::unique_ptr<ResourceHeapBase> allocation)52     void PooledResourceMemoryAllocator::DeallocateResourceHeap(
53         std::unique_ptr<ResourceHeapBase> allocation) {
54         mPool.push_front(std::move(allocation));
55     }
56 
GetPoolSizeForTesting() const57     uint64_t PooledResourceMemoryAllocator::GetPoolSizeForTesting() const {
58         return mPool.size();
59     }
60 }  // namespace dawn_native
61