• 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 #ifndef DAWNNATIVE_POOLEDRESOURCEMEMORYALLOCATOR_H_
16 #define DAWNNATIVE_POOLEDRESOURCEMEMORYALLOCATOR_H_
17 
18 #include "common/SerialQueue.h"
19 #include "dawn_native/ResourceHeapAllocator.h"
20 
21 #include <deque>
22 
23 namespace dawn_native {
24 
25     class DeviceBase;
26 
27     // |PooledResourceMemoryAllocator| allocates a fixed-size resource memory from a resource memory
28     // pool. Internally, it manages a list of heaps using LIFO (newest heaps are recycled first).
29     // The heap is in one of two states: AVAILABLE or not. Upon de-allocate, the heap is returned
30     // the pool and made AVAILABLE.
31     class PooledResourceMemoryAllocator : public ResourceHeapAllocator {
32       public:
33         PooledResourceMemoryAllocator(ResourceHeapAllocator* heapAllocator);
34         ~PooledResourceMemoryAllocator() override = default;
35 
36         ResultOrError<std::unique_ptr<ResourceHeapBase>> AllocateResourceHeap(
37             uint64_t size) override;
38         void DeallocateResourceHeap(std::unique_ptr<ResourceHeapBase> allocation) override;
39 
40         void DestroyPool();
41 
42         // For testing purposes.
43         uint64_t GetPoolSizeForTesting() const;
44 
45       private:
46         ResourceHeapAllocator* mHeapAllocator = nullptr;
47 
48         std::deque<std::unique_ptr<ResourceHeapBase>> mPool;
49     };
50 
51 }  // namespace dawn_native
52 
53 #endif  // DAWNNATIVE_POOLEDRESOURCEMEMORYALLOCATOR_H_
54