• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <executorch/backends/vulkan/runtime/vk_api/memory/Allocation.h>
10 
11 #define PRINT_FIELD(struct, field) #field << ": " << struct.field << std::endl
12 
operator <<(std::ostream & out,VmaTotalStatistics stats)13 std::ostream& operator<<(std::ostream& out, VmaTotalStatistics stats) {
14   VmaDetailedStatistics total_stats = stats.total;
15   out << "VmaTotalStatistics: " << std::endl;
16   out << "  " << PRINT_FIELD(total_stats.statistics, blockCount);
17   out << "  " << PRINT_FIELD(total_stats.statistics, allocationCount);
18   out << "  " << PRINT_FIELD(total_stats.statistics, blockBytes);
19   out << "  " << PRINT_FIELD(total_stats.statistics, allocationBytes);
20   return out;
21 }
22 
23 #undef PRINT_FIELD
24 
25 namespace vkcompute {
26 namespace vkapi {
27 
Allocation()28 Allocation::Allocation()
29     : allocator(VK_NULL_HANDLE), allocation(VK_NULL_HANDLE), is_copy_(false) {}
30 
Allocation(VmaAllocator vma_allocator,const VkMemoryRequirements & mem_props,const VmaAllocationCreateInfo & create_info)31 Allocation::Allocation(
32     VmaAllocator vma_allocator,
33     const VkMemoryRequirements& mem_props,
34     const VmaAllocationCreateInfo& create_info)
35     : allocator(vma_allocator), allocation(VK_NULL_HANDLE), is_copy_(false) {
36   VK_CHECK(vmaAllocateMemory(
37       allocator, &mem_props, &create_info, &allocation, nullptr));
38 }
39 
Allocation(const Allocation & other)40 Allocation::Allocation(const Allocation& other) noexcept
41     : allocator(other.allocator),
42       allocation(other.allocation),
43       is_copy_(true) {}
44 
Allocation(Allocation && other)45 Allocation::Allocation(Allocation&& other) noexcept
46     : allocator(other.allocator),
47       allocation(other.allocation),
48       is_copy_(other.is_copy_) {
49   other.allocation = VK_NULL_HANDLE;
50 }
51 
operator =(Allocation && other)52 Allocation& Allocation::operator=(Allocation&& other) noexcept {
53   VmaAllocation tmp_allocation = allocation;
54 
55   allocator = other.allocator;
56   allocation = other.allocation;
57   is_copy_ = other.is_copy_;
58 
59   other.allocation = tmp_allocation;
60 
61   return *this;
62 }
63 
~Allocation()64 Allocation::~Allocation() {
65   // Do not destroy the VmaAllocation if this class instance is a copy of some
66   // other class instance, since this means that this class instance does not
67   // have ownership of the underlying resource.
68   if (allocation != VK_NULL_HANDLE && !is_copy_) {
69     vmaFreeMemory(allocator, allocation);
70   }
71 }
72 
73 } // namespace vkapi
74 } // namespace vkcompute
75